File size: 1,865 Bytes
c042eff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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