Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
import openai
|
4 |
+
import gradio as gr
|
5 |
+
import os
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
# Load environment variables
|
9 |
+
load_dotenv()
|
10 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
11 |
+
|
12 |
+
# Supported languages and their codes
|
13 |
+
LANGUAGES = {
|
14 |
+
"English": "en",
|
15 |
+
"Hindi": "hi",
|
16 |
+
"Telugu": "te",
|
17 |
+
"Kannada": "kn",
|
18 |
+
"Malayalam": "ml"
|
19 |
+
}
|
20 |
+
|
21 |
+
# Function to scrape content from a URL
|
22 |
+
def scrape_content(url):
|
23 |
+
try:
|
24 |
+
response = requests.get(url)
|
25 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
26 |
+
title = soup.find('title').get_text() if soup.find('title') else "No Title"
|
27 |
+
paragraphs = soup.find_all('p')
|
28 |
+
content = '\n'.join([para.get_text() for para in paragraphs])
|
29 |
+
return title, content
|
30 |
+
except Exception as e:
|
31 |
+
return "Error", f"Failed to scrape content from {url}: {str(e)}"
|
32 |
+
|
33 |
+
# Function to translate content using OpenAI
|
34 |
+
def translate_content(content, target_language):
|
35 |
+
if target_language == "en":
|
36 |
+
return content # No translation needed
|
37 |
+
|
38 |
+
prompt = f"Translate the following content to {target_language}:
|
39 |
+
|
40 |
+
{content}"
|
41 |
+
response = openai.chat.completions.create(
|
42 |
+
model="gpt-4o-mini",
|
43 |
+
messages=[
|
44 |
+
{"role": "system", "content": "You are a multilingual translator."},
|
45 |
+
{"role": "user", "content": prompt}
|
46 |
+
]
|
47 |
+
)
|
48 |
+
return response.choices[0].message.content.strip()
|
49 |
+
|
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, body, url in contents:
|
54 |
+
translated_body = translate_content(body, LANGUAGES[language])
|
55 |
+
prompt += f"Title: {title}\nURL: {url}\n\n{translated_body}\n\n"
|
56 |
+
|
57 |
+
response = openai.chat.completions.create(
|
58 |
+
model="gpt-4o-mini",
|
59 |
+
messages=[
|
60 |
+
{"role": "system", "content": "You are a helpful assistant expert in making newsletters."},
|
61 |
+
{"role": "user", "content": prompt}
|
62 |
+
]
|
63 |
+
)
|
64 |
+
return response.choices[0].message.content.strip()
|
65 |
+
|
66 |
+
# Function to process URLs and generate newsletter
|
67 |
+
def process_urls(url1, url2, url3, url4, url5, language):
|
68 |
+
urls = [url for url in [url1, url2, url3, url4, url5] if url]
|
69 |
+
if not urls:
|
70 |
+
return "No URLs provided."
|
71 |
+
|
72 |
+
contents = []
|
73 |
+
for url in urls:
|
74 |
+
title, content = scrape_content(url)
|
75 |
+
contents.append((title, content, url))
|
76 |
+
|
77 |
+
newsletter = create_newsletter(contents, language)
|
78 |
+
return newsletter
|
79 |
+
|
80 |
+
# Gradio interface
|
81 |
+
iface = gr.Interface(
|
82 |
+
fn=process_urls,
|
83 |
+
inputs=[
|
84 |
+
gr.Textbox(label="URL 1"),
|
85 |
+
gr.Textbox(label="URL 2"),
|
86 |
+
gr.Textbox(label="URL 3"),
|
87 |
+
gr.Textbox(label="URL 4"),
|
88 |
+
gr.Textbox(label="URL 5"),
|
89 |
+
gr.Dropdown(choices=list(LANGUAGES.keys()), label="Select Language", value="English")
|
90 |
+
],
|
91 |
+
outputs="html",
|
92 |
+
title="Multilingual AI Newsletter Generator",
|
93 |
+
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."
|
94 |
+
)
|
95 |
+
|
96 |
+
iface.launch()
|