Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,15 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
import requests
|
| 5 |
-
from googlesearch import search
|
| 6 |
import logging
|
|
|
|
| 7 |
|
| 8 |
# Configure logging
|
| 9 |
logging.basicConfig(level=logging.DEBUG)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
def summarize_news(query, num_results=3):
|
| 12 |
logging.debug(f"Query received: {query}")
|
| 13 |
logging.debug(f"Number of results requested: {num_results}")
|
|
@@ -18,15 +21,18 @@ def summarize_news(query, num_results=3):
|
|
| 18 |
|
| 19 |
# Search for news articles
|
| 20 |
logging.debug("Searching for news articles...")
|
| 21 |
-
search_results = list(search(query, num_results=num_results))
|
| 22 |
articles = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
logging.debug(f"Search results: {search_results}")
|
| 25 |
-
|
| 26 |
-
for url in search_results:
|
| 27 |
-
try:
|
| 28 |
-
logging.debug(f"Fetching content from URL: {url}")
|
| 29 |
# Fetch the content of the news article
|
|
|
|
| 30 |
r = requests.get(url)
|
| 31 |
soup = BeautifulSoup(r.text, 'html.parser')
|
| 32 |
results = soup.find_all(['h1', 'p'])
|
|
@@ -65,9 +71,8 @@ def summarize_news(query, num_results=3):
|
|
| 65 |
articles.append((url, summary_text))
|
| 66 |
|
| 67 |
logging.debug(f"Summary for URL {url}: {summary_text}")
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
continue
|
| 71 |
|
| 72 |
logging.debug(f"Final summarized articles: {articles}")
|
| 73 |
return format_output(articles)
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
import requests
|
|
|
|
| 5 |
import logging
|
| 6 |
+
from newsapi import NewsApiClient
|
| 7 |
|
| 8 |
# Configure logging
|
| 9 |
logging.basicConfig(level=logging.DEBUG)
|
| 10 |
|
| 11 |
+
# Initialize the News API client
|
| 12 |
+
newsapi = NewsApiClient(api_key='5ab7bb1aaceb41b8993db03477098aad')
|
| 13 |
+
|
| 14 |
def summarize_news(query, num_results=3):
|
| 15 |
logging.debug(f"Query received: {query}")
|
| 16 |
logging.debug(f"Number of results requested: {num_results}")
|
|
|
|
| 21 |
|
| 22 |
# Search for news articles
|
| 23 |
logging.debug("Searching for news articles...")
|
|
|
|
| 24 |
articles = []
|
| 25 |
+
try:
|
| 26 |
+
news_results = newsapi.get_everything(q=query, language='en', page_size=num_results)
|
| 27 |
+
logging.debug(f"Search results: {news_results}")
|
| 28 |
+
|
| 29 |
+
for article in news_results['articles']:
|
| 30 |
+
url = article['url']
|
| 31 |
+
title = article['title']
|
| 32 |
+
description = article['description']
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Fetch the content of the news article
|
| 35 |
+
logging.debug(f"Fetching content from URL: {url}")
|
| 36 |
r = requests.get(url)
|
| 37 |
soup = BeautifulSoup(r.text, 'html.parser')
|
| 38 |
results = soup.find_all(['h1', 'p'])
|
|
|
|
| 71 |
articles.append((url, summary_text))
|
| 72 |
|
| 73 |
logging.debug(f"Summary for URL {url}: {summary_text}")
|
| 74 |
+
except Exception as e:
|
| 75 |
+
logging.error(f"Error fetching news articles: {e}")
|
|
|
|
| 76 |
|
| 77 |
logging.debug(f"Final summarized articles: {articles}")
|
| 78 |
return format_output(articles)
|