Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,15 +20,26 @@ LANGUAGES = {
|
|
20 |
"Tamil": "ta"
|
21 |
}
|
22 |
|
23 |
-
# Function to scrape content from a URL
|
24 |
-
def
|
25 |
try:
|
26 |
response = requests.get(url)
|
27 |
soup = BeautifulSoup(response.content, 'html.parser')
|
28 |
title = soup.find('title').get_text() if soup.find('title') else "No Title"
|
29 |
paragraphs = soup.find_all('p')
|
30 |
content = '\n'.join([para.get_text() for para in paragraphs])
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
except Exception as e:
|
33 |
return "Error", f"Failed to scrape content from {url}: {str(e)}"
|
34 |
|
@@ -50,9 +61,9 @@ def translate_content(content, target_language):
|
|
50 |
# Function to create newsletter
|
51 |
def create_newsletter(contents, language):
|
52 |
prompt = "Create a newsletter with the following content:\n\n"
|
53 |
-
for title,
|
54 |
-
|
55 |
-
prompt += f"Title: {title}\nURL: {url}\n\n{
|
56 |
|
57 |
response = openai.chat.completions.create(
|
58 |
model="gpt-4o-mini",
|
@@ -75,8 +86,8 @@ def process_urls(url1, url2, url3, url4, url5, language):
|
|
75 |
|
76 |
contents = []
|
77 |
for url in urls:
|
78 |
-
title,
|
79 |
-
contents.append((title,
|
80 |
|
81 |
with gr.update() as progress:
|
82 |
progress(0.6)
|
@@ -106,7 +117,7 @@ iface = gr.Interface(
|
|
106 |
],
|
107 |
outputs=["html", gr.File(label="Download Newsletter")],
|
108 |
title="Multilingual AI Newsletter Generator",
|
109 |
-
description="Enter up to 5 URLs to generate a newsletter in your preferred language. Copy and paste the output into your CMS tool for further editing. A progress indicator will show the process, and you can download the newsletter as a text file."
|
110 |
)
|
111 |
|
112 |
-
iface.launch()
|
|
|
20 |
"Tamil": "ta"
|
21 |
}
|
22 |
|
23 |
+
# Function to scrape and summarize content from a URL
|
24 |
+
def scrape_and_summarize(url):
|
25 |
try:
|
26 |
response = requests.get(url)
|
27 |
soup = BeautifulSoup(response.content, 'html.parser')
|
28 |
title = soup.find('title').get_text() if soup.find('title') else "No Title"
|
29 |
paragraphs = soup.find_all('p')
|
30 |
content = '\n'.join([para.get_text() for para in paragraphs])
|
31 |
+
|
32 |
+
# Summarize the content using OpenAI
|
33 |
+
prompt = f"Summarize the following content:\n\n{content}"
|
34 |
+
response = openai.chat.completions.create(
|
35 |
+
model="gpt-4o-mini",
|
36 |
+
messages=[
|
37 |
+
{"role": "system", "content": "You are an expert summarizer."},
|
38 |
+
{"role": "user", "content": prompt}
|
39 |
+
]
|
40 |
+
)
|
41 |
+
summary = response.choices[0].message.content.strip()
|
42 |
+
return title, summary
|
43 |
except Exception as e:
|
44 |
return "Error", f"Failed to scrape content from {url}: {str(e)}"
|
45 |
|
|
|
61 |
# Function to create newsletter
|
62 |
def create_newsletter(contents, language):
|
63 |
prompt = "Create a newsletter with the following content:\n\n"
|
64 |
+
for title, summary, url in contents:
|
65 |
+
translated_summary = translate_content(summary, LANGUAGES[language])
|
66 |
+
prompt += f"Title: {title}\nURL: {url}\n\n{translated_summary}\n\n"
|
67 |
|
68 |
response = openai.chat.completions.create(
|
69 |
model="gpt-4o-mini",
|
|
|
86 |
|
87 |
contents = []
|
88 |
for url in urls:
|
89 |
+
title, summary = scrape_and_summarize(url)
|
90 |
+
contents.append((title, summary, url))
|
91 |
|
92 |
with gr.update() as progress:
|
93 |
progress(0.6)
|
|
|
117 |
],
|
118 |
outputs=["html", gr.File(label="Download Newsletter")],
|
119 |
title="Multilingual AI Newsletter Generator",
|
120 |
+
description="Enter up to 5 URLs to generate a summarized newsletter in your preferred language. Copy and paste the output into your CMS tool for further editing. A progress indicator will show the process, and you can download the newsletter as a text file."
|
121 |
)
|
122 |
|
123 |
+
iface.launch()
|