Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
import requests | |
from bs4 import BeautifulSoup | |
from dotenv import load_dotenv | |
import openai | |
API_KEY = os.getenv('OPENAI_API_KEY') | |
# Initialize OpenAI API | |
if not API_KEY: | |
st.error("API key not found. Please add it to the env.") | |
else: | |
openai.api_key = API_KEY | |
# Function to fetch the web content | |
# Define function to fetch webpage content | |
def fetch_web_content(url): | |
try: | |
response = requests.get(url) | |
response.raise_for_status() | |
soup = BeautifulSoup(response.text, 'html.parser') | |
# Step 1: Find all paragraph tags | |
paragraph_elements = soup.find_all('p') | |
# Step 2: Extract text content from each paragraph tag | |
paragraphs = [p.get_text() for p in paragraph_elements] | |
return '\n'.join(paragraphs) | |
except requests.RequestException as e: | |
st.error(f"Error fetching URL: {e}") | |
return "" | |
# Function to generate the messages for the OpenAI API request | |
def messages_for(web_content): | |
system_prompt = "You are an assistant that analyzes the contents of a website \ | |
and provides a short summary, ignoring text that might be navigation related. \ | |
Respond in markdown." | |
user_prompt = "The contents of this website are as follows; please provide a short summary of this website in markdown. \ | |
If it includes news or announcements, then summarize these too.\n\n" | |
user_prompt += web_content | |
return [ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": user_prompt} | |
] | |
# summarize the text | |
def summarize_text(text): | |
if not text: | |
return "no content to summarize" | |
try: | |
response = openai.chat.completions.create( | |
model = "gpt-4o-mini", | |
messages = messages_for(text) | |
) | |
summary = response.choices[0].message.content.strip() | |
return summary | |
except Exception as e: | |
st.error(f"Error with OpenAI API: {e}") | |
return "Error generating summary." | |
# Streamlit App UI | |
st.title("Web Content Summarizer") | |
st.write("Enter a URL to get a concise summary of the content.") | |
# User input for URL | |
url = st.text_input("Enter the URL:") | |
# Submit button | |
if st.button("Summarize"): | |
if url: | |
st.info("Fetching content...") | |
web_content = fetch_web_content(url) | |
if web_content: | |
st.success("Content fetched successfully!") | |
st.info("Generating summary...") | |
summary = summarize_text(web_content) | |
st.subheader("Summary:") | |
st.write(summary) | |
else: | |
st.warning("Please enter a valid URL.") | |
# Run this app with: streamlit run app.py | |