File size: 3,912 Bytes
96d35b0 148b546 96d35b0 148b546 96d35b0 2d35ff0 148b546 2d35ff0 148b546 2d35ff0 148b546 2d35ff0 148b546 2d35ff0 148b546 2d35ff0 148b546 2d35ff0 148b546 2d35ff0 148b546 2d35ff0 148b546 c9aecbe 148b546 2d35ff0 f4f2da6 148b546 9e020a8 148b546 2d35ff0 198d54c 2d35ff0 9e020a8 2d35ff0 9e020a8 2d35ff0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import gradio as gr
from openai import OpenAI
import json
import os
from pydantic import BaseModel, Field
from typing import Optional, List
from openai import OpenAI
import pandas as pd
class SummaryTweet(BaseModel):
content: str = Field(..., description="A concise version of the abstract in tweet format. No Hastags")
class AnnounceTweet(BaseModel):
content: str = Field(..., description="A tweet announcing a new article. Text must include the author and title. No Hastags")
class InsightTweet(BaseModel):
content: str = Field(..., description="Simple, understandable statement distilling a complex social science concept. No Hastags")
class ThreadTweet(BaseModel):
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.")
class GenerateTweets(BaseModel):
summary_tweets: List[SummaryTweet] = Field(default=[], description="A list of SummaryTweet instances.")
insight_tweets: List[InsightTweet] = Field(default=[], description="A list of InsightTweet instances.")
announce_tweets: List[AnnounceTweet] = Field(default=[], description="A list of AnnounceTweet instances.")
def get_api_key(input_password):
# Retrieve the password and API key from environment variables
correct_password = os.environ.get("password")
api_key = os.environ.get("openai_key")
if input_password == correct_password:
return api_key
else:
return "Incorrect password."
def write_tweets(input_password, abstract_input):
api_key_input = get_api_key(input_password)
if api_key_input == "Incorrect password.":
return 'Incorrect password.'
client = OpenAI(
max_retries=3,
api_key=api_key_input,
timeout=90.0,
)
messages = [
{
"role": "system",
"content": ''' You write a variety of different types of social media posts about sociology articles.
If you perform well, everyone gets a raise, while there are negative consequences if the posts don't get views.
NEVER USE HASHTAGS.
''',
},
{
"role": "user",
"content": f"""Generate five different tweets of each style about this sociology article:
{abstract_input}
""",
},
]
completion = client.chat.completions.create(
model = 'gpt-4o', # model="gpt-3.5-turbo" is 20x cheaper but isn't as insightful
functions=[
{
"name": "generate_tweets",
"description": "Create multiple social media posts, including a tweet thread, based on an article abstract.",
"parameters": GenerateTweets.model_json_schema(),
},
],
n=1,
messages=messages,
)
r = json.loads(completion.choices[0].message.function_call.arguments)
tweets = [{'type': tweet_type.split('_')[0], 'text': t['content']} for tweet_type in r for t in r[tweet_type]]
df = pd.DataFrame(tweets)
grouped = df.groupby('type')['text'].apply(list).reset_index()
markdown = ""
for _, row in grouped.iterrows():
markdown += f"- **{row['type']}**\n"
for text in row['text']:
markdown += f" - {text}\n"
return markdown
with gr.Blocks() as app:
gr.Markdown("### Social Media Post Generator")
api_key_input = gr.Textbox(label="Password", placeholder="Enter the super secret password")
abstract_input = gr.Textbox(label="Article Abstract", placeholder="Enter the article abstract here")
generate_button = gr.Button("Generate Tweets")
output_markdown = gr.Markdown()
generate_button.click(fn=write_tweets, inputs=[api_key_input, abstract_input], outputs=output_markdown)
app.launch() |