dindizz commited on
Commit
c0c350b
·
verified ·
1 Parent(s): 72a791c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -51
app.py CHANGED
@@ -1,55 +1,59 @@
1
- from openai import OpenAI;
 
 
2
 
3
- const openai = new OpenAI({
4
- apiKey: import.meta.env.VITE_OPENAI_API_KEY,
5
- dangerouslyAllowBrowser: true
6
- });
7
 
8
- export async function generateNewsletter(data: {
9
- orgName: string;
10
- newsletterName: string;
11
- urls: string[];
12
- }): Promise<{ content: string; summaries: string[] }> {
13
- const prompt = `Create a professional newsletter for ${data.orgName} titled "${data.newsletterName}".
14
- Include engaging summaries for the following URLs that spark curiosity to read more: ${data.urls.join(', ')}.
15
- Format the newsletter with HTML tags for structure. Make it engaging and professional.`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- const response = await openai.chat.completions.create({
18
- model: "gpt-4o-mini",
19
- messages: [
20
- {
21
- role: "system",
22
- content: "You are a professional newsletter writer who creates engaging content with perfect formatting and enticing summaries."
23
- },
24
- {
25
- role: "user",
26
- content: prompt
27
- }
28
- ]
29
- });
 
 
 
 
 
 
 
30
 
31
- // Generate separate summaries for each URL
32
- const summaryPromises = data.urls.map(url =>
33
- openai.chat.completions.create({
34
- model: "gpt-4",
35
- messages: [
36
- {
37
- role: "system",
38
- content: "Create a brief, engaging summary that sparks curiosity to read more. Keep it concise and compelling."
39
- },
40
- {
41
- role: "user",
42
- content: `Create an enticing summary for this URL: ${url}`
43
- }
44
- ]
45
- })
46
- );
47
-
48
- const summaryResponses = await Promise.all(summaryPromises);
49
- const summaries = summaryResponses.map(resp => resp.choices[0].message.content);
50
-
51
- return {
52
- content: response.choices[0].message.content,
53
- summaries
54
- };
55
- }
 
1
+ import gradio as gr
2
+ import openai
3
+ import os
4
 
5
+ # Load OpenAI API Key from environment variable
6
+ openai.api_key = os.getenv("OPENAI_API_KEY")
 
 
7
 
8
+ def generate_newsletter(org_name, newsletter_name, urls):
9
+ """Generates a professional newsletter."""
10
+ prompt = (f"Create a professional newsletter for {org_name} titled '{newsletter_name}'. "
11
+ f"Include engaging summaries for the following URLs that spark curiosity to read more: {', '.join(urls)}. "
12
+ "Format the newsletter with HTML tags for structure. Make it engaging and professional.")
13
+
14
+ response = openai.ChatCompletion.create(
15
+ model="gpt-4",
16
+ messages=[
17
+ {"role": "system", "content": "You are a professional newsletter writer who creates engaging content with perfect formatting and enticing summaries."},
18
+ {"role": "user", "content": prompt}
19
+ ]
20
+ )
21
+
22
+ # Generate separate summaries for each URL
23
+ summaries = []
24
+ for url in urls:
25
+ summary_prompt = f"Create an enticing summary for this URL: {url}"
26
+ summary_response = openai.ChatCompletion.create(
27
+ model="gpt-4",
28
+ messages=[
29
+ {"role": "system", "content": "Create a brief, engaging summary that sparks curiosity to read more. Keep it concise and compelling."},
30
+ {"role": "user", "content": summary_prompt}
31
+ ]
32
+ )
33
+ summaries.append(summary_response["choices"][0]["message"]["content"].strip())
34
+
35
+ return response["choices"][0]["message"]["content"].strip(), summaries
36
 
37
+ # Gradio Interface
38
+ def main():
39
+ with gr.Blocks() as demo:
40
+ gr.Markdown("# 📩 Newsletter Generator\nGenerate professional newsletters effortlessly!")
41
+
42
+ org_name = gr.Textbox(label="Organization Name")
43
+ newsletter_name = gr.Textbox(label="Newsletter Title")
44
+ urls = gr.Textbox(label="URLs (comma-separated)")
45
+ generate_btn = gr.Button("Generate Newsletter")
46
+ output = gr.Textbox(label="Generated Newsletter", lines=10)
47
+ summaries_output = gr.Textbox(label="Summaries", lines=10)
48
+
49
+ def process_input(org, title, url_text):
50
+ url_list = [url.strip() for url in url_text.split(',') if url.strip()]
51
+ content, summaries = generate_newsletter(org, title, url_list)
52
+ return content, '\n'.join(summaries)
53
+
54
+ generate_btn.click(process_input, inputs=[org_name, newsletter_name, urls], outputs=[output, summaries_output])
55
+
56
+ demo.launch()
57
 
58
+ if __name__ == "__main__":
59
+ main()