TanishqO0F commited on
Commit
6c3cf43
·
verified ·
1 Parent(s): b81ebe4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+ import pandas as pd
5
+ from transformers import pipeline
6
+ import yfinance as yf
7
+ import plotly.graph_objects as go
8
+ from datetime import datetime, timedelta
9
+
10
+ # Sentiment Analysis Model
11
+ sentiment_model = pipeline(model="finiteautomata/bertweet-base-sentiment-analysis")
12
+
13
+ # Function to encode special characters in the search query
14
+ def encode_special_characters(text):
15
+ encoded_text = ''
16
+ special_characters = {'&': '%26', '=': '%3D', '+': '%2B', ' ': '%20'}
17
+ for char in text.lower():
18
+ encoded_text += special_characters.get(char, char)
19
+ return encoded_text
20
+
21
+ # Function to fetch news articles
22
+ def fetch_news(query, num_articles=10):
23
+ encoded_query = encode_special_characters(query)
24
+ url = f"https://news.google.com/search?q={encoded_query}&hl=en-US&gl=in&ceid=US%3Aen&num={num_articles}"
25
+
26
+ try:
27
+ response = requests.get(url, verify=False)
28
+ response.raise_for_status()
29
+ except requests.RequestException as e:
30
+ print(f"Error fetching news: {e}")
31
+ return pd.DataFrame()
32
+
33
+ soup = BeautifulSoup(response.text, 'html.parser')
34
+ articles = soup.find_all('article')
35
+
36
+ news_data = []
37
+ for article in articles[:num_articles]:
38
+ link = article.find('a')['href'].replace("./articles/", "https://news.google.com/articles/")
39
+ text_parts = article.get_text(separator='\n').split('\n')
40
+
41
+ news_data.append({
42
+ 'Title': text_parts[2] if len(text_parts) > 2 else 'Missing',
43
+ 'Source': text_parts[0] if len(text_parts) > 0 else 'Missing',
44
+ 'Time': text_parts[3] if len(text_parts) > 3 else 'Missing',
45
+ 'Author': text_parts[4].split('By ')[-1] if len(text_parts) > 4 else 'Missing',
46
+ 'Link': link
47
+ })
48
+
49
+ return pd.DataFrame(news_data)
50
+
51
+ # Function to perform sentiment analysis
52
+ def analyze_sentiment(text):
53
+ result = sentiment_model(text)[0]
54
+ return result['label'], result['score']
55
+
56
+ # Function to fetch stock data
57
+ def fetch_stock_data(symbol, start_date, end_date):
58
+ stock = yf.Ticker(symbol)
59
+ data = stock.history(start=start_date, end=end_date)
60
+ return data
61
+
62
+ # Main function to process news and perform analysis
63
+ def news_and_analysis(query, stock_symbol):
64
+ # Fetch news
65
+ news_df = fetch_news(query)
66
+
67
+ if news_df.empty:
68
+ return "No news articles found.", None, None
69
+
70
+ # Perform sentiment analysis
71
+ news_df['Sentiment'], news_df['Sentiment_Score'] = zip(*news_df['Title'].apply(analyze_sentiment))
72
+
73
+ # Fetch stock data (last 30 days)
74
+ end_date = datetime.now()
75
+ start_date = end_date - timedelta(days=30)
76
+ stock_data = fetch_stock_data(stock_symbol, start_date, end_date)
77
+
78
+ # Create sentiment plot
79
+ sentiment_fig = go.Figure(data=[go.Bar(
80
+ x=news_df['Time'],
81
+ y=news_df['Sentiment_Score'],
82
+ marker_color=news_df['Sentiment'].map({'positive': 'green', 'neutral': 'gray', 'negative': 'red'})
83
+ )])
84
+ sentiment_fig.update_layout(title='News Sentiment Over Time', xaxis_title='Time', yaxis_title='Sentiment Score')
85
+
86
+ # Create stock price plot
87
+ stock_fig = go.Figure(data=[go.Candlestick(
88
+ x=stock_data.index,
89
+ open=stock_data['Open'],
90
+ high=stock_data['High'],
91
+ low=stock_data['Low'],
92
+ close=stock_data['Close']
93
+ )])
94
+ stock_fig.update_layout(title=f'{stock_symbol} Stock Price', xaxis_title='Date', yaxis_title='Price')
95
+
96
+ return news_df, sentiment_fig, stock_fig
97
+
98
+ # Gradio interface
99
+ with gr.Blocks() as demo:
100
+ gr.Markdown("# Financial News Sentiment Analysis and Market Impact")
101
+
102
+ with gr.Row():
103
+ topic = gr.Textbox(label="Enter a financial topic or company name")
104
+ stock_symbol = gr.Textbox(label="Enter the stock symbol (e.g., RELIANCE.NS for Reliance Industries)")
105
+
106
+ analyze_btn = gr.Button(value="Analyze")
107
+
108
+ news_output = gr.DataFrame(label="News and Sentiment Analysis")
109
+ sentiment_plot = gr.Plot(label="Sentiment Analysis")
110
+ stock_plot = gr.Plot(label="Stock Price Movement")
111
+
112
+ analyze_btn.click(
113
+ news_and_analysis,
114
+ inputs=[topic, stock_symbol],
115
+ outputs=[news_output, sentiment_plot, stock_plot]
116
+ )
117
+
118
+
119
+ demo.launch()