Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
import os | |
# Load OpenAI API Key from environment variable | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
def generate_newsletter(org_name, newsletter_name, urls): | |
"""Generates a professional newsletter.""" | |
prompt = (f"Create a professional newsletter for {org_name} titled '{newsletter_name}'. " | |
f"Include engaging summaries for the following URLs that spark curiosity to read more: {', '.join(urls)}. " | |
"Format the newsletter with HTML tags for structure. Make it engaging and professional.") | |
response = openai.ChatCompletion.create( | |
model="gpt-4", | |
messages=[ | |
{"role": "system", "content": "You are a professional newsletter writer who creates engaging content with perfect formatting and enticing summaries."}, | |
{"role": "user", "content": prompt} | |
] | |
) | |
# Generate separate summaries for each URL | |
summaries = [] | |
for url in urls: | |
summary_prompt = f"Create an enticing summary for this URL: {url}" | |
summary_response = openai.ChatCompletion.create( | |
model="gpt-4", | |
messages=[ | |
{"role": "system", "content": "Create a brief, engaging summary that sparks curiosity to read more. Keep it concise and compelling."}, | |
{"role": "user", "content": summary_prompt} | |
] | |
) | |
summaries.append(summary_response["choices"][0]["message"]["content"].strip()) | |
return response["choices"][0]["message"]["content"].strip(), summaries | |
# Gradio Interface | |
def main(): | |
with gr.Blocks() as demo: | |
gr.Markdown("# π© Newsletter Generator\nGenerate professional newsletters effortlessly!") | |
org_name = gr.Textbox(label="Organization Name") | |
newsletter_name = gr.Textbox(label="Newsletter Title") | |
urls = gr.Textbox(label="URLs (comma-separated)") | |
generate_btn = gr.Button("Generate Newsletter") | |
output = gr.Textbox(label="Generated Newsletter", lines=10) | |
summaries_output = gr.Textbox(label="Summaries", lines=10) | |
def process_input(org, title, url_text): | |
url_list = [url.strip() for url in url_text.split(',') if url.strip()] | |
content, summaries = generate_newsletter(org, title, url_list) | |
return content, '\n'.join(summaries) | |
generate_btn.click(process_input, inputs=[org_name, newsletter_name, urls], outputs=[output, summaries_output]) | |
demo.launch() | |
if __name__ == "__main__": | |
main() | |
openai | |
gradio |