Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,12 +23,17 @@ LANGUAGES = {
|
|
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(
|
@@ -40,45 +45,53 @@ def scrape_and_summarize(url):
|
|
40 |
)
|
41 |
summary = response.choices[0].message.content.strip()
|
42 |
return title, summary
|
43 |
-
except
|
44 |
return "Error", f"Failed to scrape content from {url}: {str(e)}"
|
|
|
|
|
45 |
|
46 |
# Function to translate content using OpenAI
|
47 |
def translate_content(content, target_language):
|
48 |
if target_language == "en":
|
49 |
return content # No translation needed
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
60 |
|
61 |
# Function to create newsletter
|
62 |
def create_newsletter(contents, language):
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
76 |
|
77 |
# Function to process URLs and generate newsletter
|
78 |
def process_urls(url1, url2, url3, url4, url5, language):
|
79 |
urls = [url for url in [url1, url2, url3, url4, url5] if url]
|
80 |
if not urls:
|
81 |
-
return "No URLs provided."
|
82 |
|
83 |
with gr.update() as progress:
|
84 |
progress(0.2)
|
@@ -99,8 +112,11 @@ def process_urls(url1, url2, url3, url4, url5, language):
|
|
99 |
time.sleep(1)
|
100 |
|
101 |
file_path = "newsletter.txt"
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
104 |
|
105 |
return newsletter, file_path
|
106 |
|
@@ -120,4 +136,4 @@ iface = gr.Interface(
|
|
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()
|
|
|
23 |
# Function to scrape and summarize content from a URL
|
24 |
def scrape_and_summarize(url):
|
25 |
try:
|
26 |
+
response = requests.get(url, timeout=10) # Set timeout to 10 seconds
|
27 |
+
response.raise_for_status()
|
28 |
soup = BeautifulSoup(response.content, 'html.parser')
|
29 |
title = soup.find('title').get_text() if soup.find('title') else "No Title"
|
30 |
paragraphs = soup.find_all('p')
|
31 |
content = '\n'.join([para.get_text() for para in paragraphs])
|
32 |
|
33 |
+
# Truncate content if too large for API
|
34 |
+
if len(content) > 2000:
|
35 |
+
content = content[:2000] + "..."
|
36 |
+
|
37 |
# Summarize the content using OpenAI
|
38 |
prompt = f"Summarize the following content:\n\n{content}"
|
39 |
response = openai.chat.completions.create(
|
|
|
45 |
)
|
46 |
summary = response.choices[0].message.content.strip()
|
47 |
return title, summary
|
48 |
+
except requests.exceptions.RequestException as e:
|
49 |
return "Error", f"Failed to scrape content from {url}: {str(e)}"
|
50 |
+
except Exception as e:
|
51 |
+
return "Error", f"Failed to summarize content from {url}: {str(e)}"
|
52 |
|
53 |
# Function to translate content using OpenAI
|
54 |
def translate_content(content, target_language):
|
55 |
if target_language == "en":
|
56 |
return content # No translation needed
|
57 |
|
58 |
+
try:
|
59 |
+
prompt = f"Translate the following content to {target_language}:\n\n{content}"
|
60 |
+
response = openai.chat.completions.create(
|
61 |
+
model="gpt-4o-mini",
|
62 |
+
messages=[
|
63 |
+
{"role": "system", "content": "You are a multilingual translator."},
|
64 |
+
{"role": "user", "content": prompt}
|
65 |
+
]
|
66 |
+
)
|
67 |
+
return response.choices[0].message.content.strip()
|
68 |
+
except Exception as e:
|
69 |
+
return f"Translation failed: {str(e)}"
|
70 |
|
71 |
# Function to create newsletter
|
72 |
def create_newsletter(contents, language):
|
73 |
+
try:
|
74 |
+
prompt = "Create a newsletter with the following content:\n\n"
|
75 |
+
for title, summary, url in contents:
|
76 |
+
translated_summary = translate_content(summary, LANGUAGES[language])
|
77 |
+
prompt += f"Title: {title}\nURL: {url}\n\n{translated_summary}\n\n"
|
78 |
+
|
79 |
+
response = openai.chat.completions.create(
|
80 |
+
model="gpt-4o-mini",
|
81 |
+
messages=[
|
82 |
+
{"role": "system", "content": "You are a helpful assistant expert in making newsletters."},
|
83 |
+
{"role": "user", "content": prompt}
|
84 |
+
]
|
85 |
+
)
|
86 |
+
return response.choices[0].message.content.strip()
|
87 |
+
except Exception as e:
|
88 |
+
return f"Failed to create newsletter: {str(e)}"
|
89 |
|
90 |
# Function to process URLs and generate newsletter
|
91 |
def process_urls(url1, url2, url3, url4, url5, language):
|
92 |
urls = [url for url in [url1, url2, url3, url4, url5] if url]
|
93 |
if not urls:
|
94 |
+
return "No URLs provided.", None
|
95 |
|
96 |
with gr.update() as progress:
|
97 |
progress(0.2)
|
|
|
112 |
time.sleep(1)
|
113 |
|
114 |
file_path = "newsletter.txt"
|
115 |
+
try:
|
116 |
+
with open(file_path, "w", encoding="utf-8") as file:
|
117 |
+
file.write(newsletter)
|
118 |
+
except Exception as e:
|
119 |
+
return f"Failed to save newsletter: {str(e)}", None
|
120 |
|
121 |
return newsletter, file_path
|
122 |
|
|
|
136 |
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."
|
137 |
)
|
138 |
|
139 |
+
iface.launch()
|