|
|
import requests |
|
|
from bs4 import BeautifulSoup |
|
|
import openai |
|
|
import gradio as gr |
|
|
import os |
|
|
from dotenv import load_dotenv |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
openai.api_key = os.getenv("OPENAI_API_KEY") |
|
|
|
|
|
|
|
|
|
|
|
def scrape_content(url): |
|
|
response = requests.get(url) |
|
|
soup = BeautifulSoup(response.content, 'html.parser') |
|
|
|
|
|
|
|
|
title = soup.find('title').get_text() |
|
|
paragraphs = soup.find_all('p') |
|
|
content = '\n'.join([para.get_text() for para in paragraphs]) |
|
|
|
|
|
return title, content |
|
|
|
|
|
|
|
|
|
|
|
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() |
|
|
return 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 |
|
|
|
|
|
|
|
|
|
|
|
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() |
|
|
|