File size: 3,133 Bytes
83cfe1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34b429d
 
83cfe1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34b429d
83cfe1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import requests
from bs4 import BeautifulSoup
import openai
import gradio as gr
import os
from dotenv import load_dotenv

# 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 content from a URL
def scrape_content(url):
    try:
        response = requests.get(url)
        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])
        return title, content
    except Exception as e:
        return "Error", f"Failed to scrape 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
    
    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()

# Function to create newsletter
def create_newsletter(contents, language):
    prompt = "Create a newsletter with the following content:\n\n"
    for title, body, url in contents:
        translated_body = translate_content(body, LANGUAGES[language])
        prompt += f"Title: {title}\nURL: {url}\n\n{translated_body}\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()

# 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."
    
    contents = []
    for url in urls:
        title, content = scrape_content(url)
        contents.append((title, content, url))
    
    newsletter = create_newsletter(contents, language)
    return newsletter

# 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",
    title="Multilingual AI Newsletter Generator",
    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."
)

iface.launch()