File size: 2,320 Bytes
146f8eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
632277d
146f8eb
44f5c76
146f8eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210f846
05efac8
146f8eb
 
 
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
import requests
from bs4 import BeautifulSoup
import openai
import gradio as gr
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")


# Function to scrape content from a URL
def scrape_content(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')

    # Example of extracting title and body content - modify based on actual structure of the websites
    title = soup.find('title').get_text()
    paragraphs = soup.find_all('p')
    content = '\n'.join([para.get_text() for para in paragraphs])

    return title, content


# Function to create newsletter using OpenAI
def create_newsletter(contents):
    prompt = "Create a newsletter with the following content:\n\n"
    for content in contents:
        title, body, url = content
        prompt += f"Title: {title}\nURL: {url}\n\n{body}\n\n"

    response = openai.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "You are a helpful assistant and en expert in making Substack Newsletters for the Electric Vehicle Community AmpSphere."},
            {"role": "user", "content": prompt}
        ]
    )

    # newsletter = response['choices'][0]['message']['content'].strip()
    newsletter = response.choices[0].message.content.strip()
    return newsletter


# Function to process URLs and generate the newsletter
def process_urls(url1, url2, url3, url4, url5):
    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)
    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")
    ],
    outputs="html",
    title="The AmpSphere Newsletter Generator",
    description="Enter up to 5 URLs to generate a newsletter,copy paste from the right onto your notepad/cms tool to edit and work further."
)

iface.launch()