Spaces:
Sleeping
Sleeping
File size: 2,573 Bytes
c0c350b 8d56e2e c0c350b 8d56e2e c0c350b 8d56e2e c0c350b a764ec4 8d56e2e c0c350b a764ec4 |
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 |
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 |