1-project / utils.py
devanshu1121's picture
Upload utils.py
c042eff verified
Raw
History Blame Contribute Delete
1.87 kB
import requests
from bs4 import BeautifulSoup
from transformers import pipeline
from newspaper import Article
from gtts import gTTS
import os
# Function to fetch and parse news article
def fetch_article(url):
article = Article(url)
article.download()
article.parse()
return article.text
# Function to summarize text using Hugging Face model
def summarize_text(text):
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
summary = summarizer(text, max_length=150, min_length=50, do_sample=False)
return summary[0]['summary_text']
# Function to perform sentiment analysis on a text
def sentiment_analysis(text):
sentiment_analyzer = pipeline("sentiment-analysis")
sentiment = sentiment_analyzer(text)[0]
return sentiment['label']
# Function to fetch news articles based on company name
def fetch_news(company_name, num_articles=10):
search_url = f"https://news.google.com/search?q={company_name}&hl=en-US&gl=US&ceid=US%3Aen"
response = requests.get(search_url)
soup = BeautifulSoup(response.content, 'html.parser')
articles = []
for item in soup.find_all('article')[:num_articles]:
title = item.find('h3').text
link = item.find('a')['href']
full_url = "https://news.google.com" + link[1:] if link.startswith('.') else link
text = fetch_article(full_url)
summary = summarize_text(text)
sentiment = sentiment_analysis(text)
articles.append({
'Title': title,
'Summary': summary,
'Sentiment': sentiment,
'Link': full_url
})
return articles
# Function to generate TTS in Hindi
def generate_tts_hindi(text, output_file='output.mp3'):
tts = gTTS(text, lang='hi')
tts.save(output_file)
return output_file