pgupta212-llm commited on
Commit
eb95c01
·
verified ·
1 Parent(s): af4cfc0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py CHANGED
@@ -7,9 +7,73 @@ import openai
7
 
8
  API_KEY = os.getenv('OPENAI_API_KEY')
9
 
 
10
  if not API_KEY:
11
  st.error("API key not found. Please add it to the env.")
12
  else:
13
  openai.api_key = API_KEY
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  API_KEY = os.getenv('OPENAI_API_KEY')
9
 
10
+ # Initialize OpenAI API
11
  if not API_KEY:
12
  st.error("API key not found. Please add it to the env.")
13
  else:
14
  openai.api_key = API_KEY
15
 
16
+ # Function to fetch the web content
17
+ # Define function to fetch webpage content
18
+ def fetch_web_content(url):
19
+ try:
20
+ response = requests.get(url)
21
+ response.raise_for_status()
22
+ soup = BeautifulSoup(response.text, 'html.parser')
23
+
24
+ # Step 1: Find all paragraph tags
25
+ paragraph_elements = soup.find_all('p')
26
+
27
+ # Step 2: Extract text content from each paragraph tag
28
+ paragraphs = [p.get_text() for p in paragraph_elements]
29
+
30
+ return '\n'.join(paragraphs)
31
+ except requests.RequestException as e:
32
+ st.error(f"Error fetching URL: {e}")
33
+ return ""
34
 
35
+ # summarize the text
36
+ def summarize_text(text):
37
+ if not text:
38
+ return "no content to summarize"
39
+
40
+ try:
41
+ response = openai.Completion.create(
42
+ engine="text-davinci-003",
43
+ prompt=f"Summarize the following web content:\n{text}",
44
+ max_tokens=200,
45
+ temperature=0.5,
46
+ )
47
+ summary = response['choices'][0]['text'].strip()
48
+ return summary
49
+ except Exception as e:
50
+ st.error(f"Error with OpenAI API: {e}")
51
+ return "Error generating summary."
52
+
53
+ # Streamlit App UI
54
+ st.title("Web Content Summarizer")
55
+ st.write("Enter a URL to get a concise summary of the content.")
56
+
57
+ # User input for URL
58
+ url = st.text_input("Enter the URL:")
59
+
60
+ # Submit button
61
+ if st.button("Summarize"):
62
+ if url:
63
+ st.info("Fetching content...")
64
+ web_content = fetch_web_content(url)
65
+ if web_content:
66
+ st.success("Content fetched successfully!")
67
+ st.info("Generating summary...")
68
+ summary = summarize_text(web_content)
69
+ st.subheader("Summary:")
70
+ st.write(summary)
71
+ else:
72
+ st.warning("Please enter a valid URL.")
73
+
74
+ # Run this app with: streamlit run app.py
75
+
76
+
77
+
78
+
79
+