Spaces:
Runtime error
Runtime error
Jaichandran1984
commited on
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from newspaper import Article
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
load_dotenv()
|
5 |
+
import google.generativeai as genai
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Configure Google Generative AI with API key
|
9 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
10 |
+
|
11 |
+
# Function to get a response from the Gemini AI model
|
12 |
+
def get_gemini_response(input):
|
13 |
+
model = genai.GenerativeModel("gemini-pro")
|
14 |
+
response = model.generate_content(input)
|
15 |
+
return response.text
|
16 |
+
|
17 |
+
# Function to scrape content from the given URL using newspaper3k
|
18 |
+
def scrape_content(url):
|
19 |
+
try:
|
20 |
+
article = Article(url, language='en') # Specify language for better parsing
|
21 |
+
article.download()
|
22 |
+
article.parse()
|
23 |
+
return article.text
|
24 |
+
except Exception as e:
|
25 |
+
st.error(f"Error fetching the content from the website: {e}")
|
26 |
+
return None
|
27 |
+
|
28 |
+
# Streamlit app configuration
|
29 |
+
st.set_page_config(page_title="News Summarizer")
|
30 |
+
st.markdown("<h1 style='text-align: center;'>News Summarizer</h1>", unsafe_allow_html=True)
|
31 |
+
st.header("Summarize News Articles from News Paper")
|
32 |
+
|
33 |
+
# Input field for the news URL
|
34 |
+
url = st.text_input("Enter the URL of the news article", "")
|
35 |
+
|
36 |
+
# Button to trigger summarization
|
37 |
+
summarize_news = st.button("Summarize News")
|
38 |
+
|
39 |
+
# If the button is pressed, scrape the content and generate the summary
|
40 |
+
if summarize_news:
|
41 |
+
if url:
|
42 |
+
st.write("Fetching content from the website...")
|
43 |
+
content = scrape_content(url)
|
44 |
+
|
45 |
+
if content:
|
46 |
+
st.write("Generating summary...")
|
47 |
+
input_prompt = """
|
48 |
+
You are an expert news summarizer. Summarize the following text from a news website:
|
49 |
+
Text: {text}
|
50 |
+
"""
|
51 |
+
summary = get_gemini_response(input_prompt.format(text=content))
|
52 |
+
st.subheader("News Summary")
|
53 |
+
st.write(summary)
|
54 |
+
else:
|
55 |
+
st.write("Failed to retrieve content from the provided URL.")
|
56 |
+
else:
|
57 |
+
st.write("Please enter a valid URL from The Hindu.")
|