File size: 2,724 Bytes
eb8a615
 
 
 
 
 
9d581a4
 
 
eb95c01
9d581a4
 
 
 
 
eb95c01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d581a4
a122300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb95c01
 
a122300
eb95c01
 
 
9e3a423
 
2c5387f
eb95c01
9e3a423
eb95c01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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