mshabir commited on
Commit
ce04aea
·
verified ·
1 Parent(s): cd576d1

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +80 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import feedparser
2
+ import snscrape.modules.twitter as sntwitter
3
+ from transformers import pipeline
4
+ import gradio as gr
5
+ from collections import Counter
6
+
7
+ # Load sentiment pipeline once
8
+ sentiment_model = pipeline("sentiment-analysis")
9
+
10
+ def fetch_google_news(max_items=10):
11
+ url = "https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en"
12
+ feed = feedparser.parse(url)
13
+ news = []
14
+ for entry in feed.entries[:max_items]:
15
+ title = entry.title
16
+ # Sometimes source is in 'source' key, else fallback to 'Unknown Source'
17
+ source = entry.get('source', {}).get('title', 'Unknown Source')
18
+ news.append({'text': title, 'source': source})
19
+ return news
20
+
21
+ def fetch_twitter(max_items=10):
22
+ tweets = []
23
+ try:
24
+ scraper = sntwitter.TwitterHashtagScraper('mentalhealth')
25
+ for i, tweet in enumerate(scraper.get_items()):
26
+ if i >= max_items:
27
+ break
28
+ tweets.append({'text': tweet.content, 'source': 'Twitter'})
29
+ except Exception as e:
30
+ # Return empty list if scraping fails
31
+ print(f"Error fetching tweets: {e}")
32
+ return tweets
33
+
34
+ def analyze_headlines():
35
+ news = fetch_google_news()
36
+ tweets = fetch_twitter()
37
+
38
+ combined = news + tweets
39
+ texts = [item['text'] for item in combined]
40
+
41
+ # Run sentiment analysis (batch)
42
+ results = sentiment_model(texts)
43
+
44
+ # Attach sentiment back
45
+ for item, res in zip(combined, results):
46
+ item['sentiment'] = res['label']
47
+ item['score'] = res['score']
48
+
49
+ pos_count = sum(1 for x in combined if x['sentiment'] == 'POSITIVE')
50
+ neg_count = sum(1 for x in combined if x['sentiment'] == 'NEGATIVE')
51
+ total = pos_count + neg_count
52
+ risk_ratio = neg_count / total if total > 0 else 0
53
+
54
+ # Count negative news by source
55
+ neg_sources = [x['source'] for x in combined if x['sentiment'] == 'NEGATIVE']
56
+ source_counts = Counter(neg_sources)
57
+
58
+ # Prepare output
59
+ lines = [
60
+ f"Positive News Count: {pos_count}",
61
+ f"Negative News Count: {neg_count}",
62
+ f"Risk Ratio (Negative / Total): {risk_ratio:.2f}",
63
+ "",
64
+ "Negative News by Source / Region:"
65
+ ]
66
+ for source, count in source_counts.most_common():
67
+ lines.append(f" - {source}: {count}")
68
+
69
+ return "\n".join(lines)
70
+
71
+ app = gr.Interface(
72
+ fn=analyze_headlines,
73
+ inputs=[],
74
+ outputs="textbox",
75
+ title="🧠 Live Headline Sentiment Risk & Region Analyzer",
76
+ description="Fetches latest news and tweets, analyzes sentiment, counts risk, and shows areas with negative news."
77
+ )
78
+
79
+ if __name__ == "__main__":
80
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ feedparser
3
+ transformers
4
+ snscrape
5
+ torch