Spaces:
Sleeping
Sleeping
Upload utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from newspaper import Article
|
| 5 |
+
from gtts import gTTS
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Function to fetch and parse news article
|
| 9 |
+
def fetch_article(url):
|
| 10 |
+
article = Article(url)
|
| 11 |
+
article.download()
|
| 12 |
+
article.parse()
|
| 13 |
+
return article.text
|
| 14 |
+
|
| 15 |
+
# Function to summarize text using Hugging Face model
|
| 16 |
+
def summarize_text(text):
|
| 17 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 18 |
+
summary = summarizer(text, max_length=150, min_length=50, do_sample=False)
|
| 19 |
+
return summary[0]['summary_text']
|
| 20 |
+
|
| 21 |
+
# Function to perform sentiment analysis on a text
|
| 22 |
+
def sentiment_analysis(text):
|
| 23 |
+
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 24 |
+
sentiment = sentiment_analyzer(text)[0]
|
| 25 |
+
return sentiment['label']
|
| 26 |
+
|
| 27 |
+
# Function to fetch news articles based on company name
|
| 28 |
+
def fetch_news(company_name, num_articles=10):
|
| 29 |
+
search_url = f"https://news.google.com/search?q={company_name}&hl=en-US&gl=US&ceid=US%3Aen"
|
| 30 |
+
response = requests.get(search_url)
|
| 31 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 32 |
+
|
| 33 |
+
articles = []
|
| 34 |
+
for item in soup.find_all('article')[:num_articles]:
|
| 35 |
+
title = item.find('h3').text
|
| 36 |
+
link = item.find('a')['href']
|
| 37 |
+
full_url = "https://news.google.com" + link[1:] if link.startswith('.') else link
|
| 38 |
+
text = fetch_article(full_url)
|
| 39 |
+
summary = summarize_text(text)
|
| 40 |
+
sentiment = sentiment_analysis(text)
|
| 41 |
+
articles.append({
|
| 42 |
+
'Title': title,
|
| 43 |
+
'Summary': summary,
|
| 44 |
+
'Sentiment': sentiment,
|
| 45 |
+
'Link': full_url
|
| 46 |
+
})
|
| 47 |
+
return articles
|
| 48 |
+
|
| 49 |
+
# Function to generate TTS in Hindi
|
| 50 |
+
def generate_tts_hindi(text, output_file='output.mp3'):
|
| 51 |
+
tts = gTTS(text, lang='hi')
|
| 52 |
+
tts.save(output_file)
|
| 53 |
+
return output_file
|