aiwonewsletter / app.py
dindizz's picture
Update app.py
b053362 verified
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 newsletter meant for wellness and healthy living for AIWO, a global wellness and age reversal organization."},
{"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 AIWO AI Newsletter Generator",
description="Enter up to 5 URLs to generate an unique newsletter,copy paste from the right onto your notepad/cms tool to edit and work further."
)
iface.launch()