Spaces:
Sleeping
Sleeping
import requests | |
from bs4 import BeautifulSoup | |
import openai | |
import gradio as gr | |
import os | |
from dotenv import load_dotenv | |
import time | |
# Load environment variables | |
load_dotenv() | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
# Supported languages and their codes | |
LANGUAGES = { | |
"English": "en", | |
"Hindi": "hi", | |
"Telugu": "te", | |
"Kannada": "kn", | |
"Malayalam": "ml", | |
"Tamil": "ta" | |
} | |
# Function to scrape and summarize content from a URL | |
def scrape_and_summarize(url): | |
try: | |
response = requests.get(url, timeout=10) # Set timeout to 10 seconds | |
response.raise_for_status() | |
soup = BeautifulSoup(response.content, 'html.parser') | |
title = soup.find('title').get_text() if soup.find('title') else "No Title" | |
paragraphs = soup.find_all('p') | |
content = '\n'.join([para.get_text() for para in paragraphs]) | |
# Truncate content if too large for API | |
if len(content) > 2000: | |
content = content[:2000] + "..." | |
# Summarize the content using OpenAI | |
prompt = f"Summarize the following content:\n\n{content}" | |
response = openai.chat.completions.create( | |
model="gpt-4o-mini", | |
messages=[ | |
{"role": "system", "content": "You are an expert summarizer."}, | |
{"role": "user", "content": prompt} | |
] | |
) | |
summary = response.choices[0].message.content.strip() | |
return title, summary | |
except requests.exceptions.RequestException as e: | |
return "Error", f"Failed to scrape content from {url}: {str(e)}" | |
except Exception as e: | |
return "Error", f"Failed to summarize content from {url}: {str(e)}" | |
# Function to translate content using OpenAI | |
def translate_content(content, target_language): | |
if target_language == "en": | |
return content # No translation needed | |
try: | |
prompt = f"Translate the following content to {target_language}:\n\n{content}" | |
response = openai.chat.completions.create( | |
model="gpt-4o-mini", | |
messages=[ | |
{"role": "system", "content": "You are a multilingual translator."}, | |
{"role": "user", "content": prompt} | |
] | |
) | |
return response.choices[0].message.content.strip() | |
except Exception as e: | |
return f"Translation failed: {str(e)}" | |
# Function to create newsletter | |
def create_newsletter(contents, language): | |
try: | |
prompt = "Create a newsletter with the following content:\n\n" | |
for title, summary, url in contents: | |
translated_summary = translate_content(summary, LANGUAGES[language]) | |
prompt += f"Title: {title}\nURL: {url}\n\n{translated_summary}\n\n" | |
response = openai.chat.completions.create( | |
model="gpt-4o-mini", | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant expert in making newsletters."}, | |
{"role": "user", "content": prompt} | |
] | |
) | |
return response.choices[0].message.content.strip() | |
except Exception as e: | |
return f"Failed to create newsletter: {str(e)}" | |
# Function to process URLs and generate newsletter | |
def process_urls(url1, url2, url3, url4, url5, language): | |
urls = [url for url in [url1, url2, url3, url4, url5] if url] | |
if not urls: | |
return "No URLs provided.", None | |
with gr.update() as progress: | |
progress(0.2) | |
time.sleep(1) | |
contents = [] | |
for url in urls: | |
title, summary = scrape_and_summarize(url) | |
contents.append((title, summary, url)) | |
with gr.update() as progress: | |
progress(0.6) | |
time.sleep(1) | |
newsletter = create_newsletter(contents, language) | |
with gr.update() as progress: | |
progress(1.0) | |
time.sleep(1) | |
file_path = "newsletter.txt" | |
try: | |
with open(file_path, "w", encoding="utf-8") as file: | |
file.write(newsletter) | |
except Exception as e: | |
return f"Failed to save newsletter: {str(e)}", None | |
return newsletter, file_path | |
# Gradio interface | |
iface = gr.Interface( | |
fn=process_urls, | |
inputs=[ | |
gr.Textbox(label="URL 1"), | |
gr.Textbox(label="URL 2"), | |
gr.Textbox(label="URL 3"), | |
gr.Textbox(label="URL 4"), | |
gr.Textbox(label="URL 5"), | |
gr.Dropdown(choices=list(LANGUAGES.keys()), label="Select Language", value="English") | |
], | |
outputs=["html", gr.File(label="Download Newsletter")], | |
title="Multilingual AI Newsletter Generator", | |
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." | |
) | |
iface.launch() | |