Added password
Browse files
app.py
CHANGED
@@ -1,57 +1,93 @@
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
instructions = '''
|
6 |
-
You write social media posts about sociology articles. When given an article abstract, you would aim to replicate these themes by identifying the core message or finding of the research, connecting it to broader social or political issues, simplifying complex ideas into an accessible format, and presenting it in a way that provokes thought or discussion.
|
7 |
-
Always give people at least *eight* different variations.
|
8 |
-
Most posts should be one or two sentences.
|
9 |
-
If you perform well, everyone gets a raise, while there are negative consequences if the posts don't get views.
|
10 |
-
|
11 |
-
Here are different styles you can use:
|
12 |
-
|
13 |
-
* Detailed: A concise version of the abstract in tweet format.
|
14 |
-
* Informative: Provide a concise summary of the article's main findings or arguments. This type of tweet should be straightforward and factual, ideally with a compelling fact or statistic to grab attention.
|
15 |
-
* Question: Pose a thought-provoking question related to the article's topic. This can engage your audience and encourage them to think more deeply about the subject, potentially prompting them to read the article for insights or answers.
|
16 |
-
* Quote: Share a notable or powerful quote from the article. Choose a quote that stands out and encapsulates a key point or is particularly thought-provoking.
|
17 |
-
* Simplifying Complex Ideas: Each tweet distills complex social science concepts into a simple, understandable statement. This makes the content accessible to a broad audience, not just experts.
|
18 |
-
* Use of Specific Examples or Case Studies: Some tweets mention specific cases or examples, such as the reference to emergency doctors and C-sections, which makes the abstract concept more concrete and relatable.
|
19 |
-
* Topical Relevance and Timeliness: The tweets address current issues or recent developments, such as the impact of AI technology, workplace dynamics, medical decision-making, extremism, social media's role in activism, political shifts, and changes in social movements. This relevance helps capture the attention of readers interested in current affairs.
|
20 |
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
**No hashtags.**
|
25 |
-
**Don't include the tweet type, just the content.***
|
26 |
|
27 |
-
|
|
|
28 |
|
29 |
-
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
{abstract}
|
38 |
-
''' }
|
39 |
-
]
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
client = OpenAI(
|
44 |
-
api_key=api_key,
|
45 |
max_retries=3,
|
46 |
-
|
|
|
|
|
|
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
completion = client.chat.completions.create(
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
|
57 |
with gr.Blocks() as app:
|
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
3 |
+
import json
|
4 |
+
import os
|
5 |
|
6 |
+
from pydantic import BaseModel, Field
|
7 |
+
from typing import Optional, List
|
8 |
+
from openai import OpenAI
|
9 |
+
import pandas as pd
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
class SummaryTweet(BaseModel):
|
13 |
+
content: str = Field(..., description="A concise version of the abstract in tweet format. No Hastags")
|
14 |
|
15 |
+
class AnnounceTweet(BaseModel):
|
16 |
+
content: str = Field(..., description="A tweet announcing a new article. Text must include the author and title. No Hastags")
|
|
|
|
|
17 |
|
18 |
+
class InsightTweet(BaseModel):
|
19 |
+
content: str = Field(..., description="Simple, understandable statement distilling a complex social science concept. No Hastags")
|
20 |
|
21 |
+
class ThreadTweet(BaseModel):
|
22 |
+
content: List[str] = Field(default=[], description="A list of tweets that together form a thread about the article, summarizing its key points and implications, engaging the audience, and encouraging further discussion or reading. No hashtags.")
|
23 |
|
24 |
+
class GenerateTweets(BaseModel):
|
25 |
+
summary_tweets: List[SummaryTweet] = Field(default=[], description="A list of SummaryTweet instances.")
|
26 |
+
insight_tweets: List[InsightTweet] = Field(default=[], description="A list of InsightTweet instances.")
|
27 |
+
announce_tweets: List[AnnounceTweet] = Field(default=[], description="A list of AnnounceTweet instances.")
|
28 |
+
thread_tweets: List[ThreadTweet] = Field(default=[], description="A list of ThreadTweet instances.")
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
def get_api_key(input_password):
|
31 |
+
# Retrieve the password and API key from environment variables
|
32 |
+
correct_password = os.environ.get("password")
|
33 |
+
api_key = os.environ.get("openai_key")
|
34 |
+
|
35 |
+
if input_password == correct_password:
|
36 |
+
return api_key
|
37 |
+
else:
|
38 |
+
return "Incorrect password."
|
39 |
+
|
40 |
|
41 |
|
42 |
+
def write_tweets(input_password, abstract_input):
|
43 |
+
|
44 |
+
api_key_input = get_api_key(input_password)
|
45 |
+
if api_key_input == "Incorrect password.":
|
46 |
+
return 'Incorrect password.'
|
47 |
+
|
48 |
client = OpenAI(
|
|
|
49 |
max_retries=3,
|
50 |
+
api_key=api_key_input,
|
51 |
+
timeout=90.0,
|
52 |
+
)
|
53 |
+
|
54 |
|
55 |
+
messages = [
|
56 |
+
{
|
57 |
+
"role": "system",
|
58 |
+
"content": ''' You write a variety of different types of social media posts about sociology articles.
|
59 |
+
If you perform well, everyone gets a raise, while there are negative consequences if the posts don't get views.
|
60 |
+
NEVER USE HASHTAGS.
|
61 |
+
''',
|
62 |
+
},
|
63 |
+
{
|
64 |
+
"role": "user",
|
65 |
+
"content": f"""Generate five tweets of each style plus a tweet thread about this sociology article:
|
66 |
+
|
67 |
+
|
68 |
+
{abstract_input}
|
69 |
+
|
70 |
+
""",
|
71 |
+
},
|
72 |
+
]
|
73 |
|
74 |
completion = client.chat.completions.create(
|
75 |
+
model = 'gpt-4-turbo-preview', # model="gpt-3.5-turbo" is 20x cheaper but isn't as insightful
|
76 |
+
functions=[
|
77 |
+
{
|
78 |
+
"name": "generate_tweets",
|
79 |
+
"description": "Create multiple social media posts, including a tweet thread, based on an article abstract.",
|
80 |
+
"parameters": GenerateTweets.model_json_schema(),
|
81 |
+
},
|
82 |
+
],
|
83 |
+
n=1,
|
84 |
+
messages=messages,
|
85 |
+
)
|
86 |
+
r = json.loads(completion.choices[0].message.function_call.arguments)
|
87 |
+
tweets = [{'type': tweet_type.split('_')[0], 'text': t['content']} for tweet_type in r for t in r[tweet_type]]
|
88 |
+
|
89 |
+
return pd.DataFrame(tweets)
|
90 |
+
|
91 |
|
92 |
|
93 |
with gr.Blocks() as app:
|