TanishqO0F commited on
Commit
3a1866d
·
verified ·
1 Parent(s): 499c10f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -20
app.py CHANGED
@@ -6,21 +6,17 @@ from transformers import pipeline
6
  import plotly.express as px
7
  from datetime import datetime, timedelta
8
 
9
- # Load the Excel file with company names and symbols
10
  file_path = '/home/user/app/Top 2000 Valued Companies with Ticker Symbols.xlsx'
11
  companies_df = pd.read_excel(file_path)
12
 
13
- # Function to get stock symbol for a company name
14
  def get_stock_symbol(company_name):
15
  match = companies_df[companies_df['Name'].str.contains(company_name, case=False, na=False)]
16
  if not match.empty:
17
  return match.iloc[0]['Symbol']
18
  return None
19
 
20
- # Sentiment Analysis Model
21
  sentiment_model = pipeline(model="finiteautomata/bertweet-base-sentiment-analysis")
22
 
23
- # Function to encode special characters in the search query
24
  def encode_special_characters(text):
25
  encoded_text = ''
26
  special_characters = {'&': '%26', '=': '%3D', '+': '%2B', ' ': '%20'}
@@ -28,7 +24,6 @@ def encode_special_characters(text):
28
  encoded_text += special_characters.get(char, char)
29
  return encoded_text
30
 
31
- # Function to fetch news articles
32
  def fetch_news(query, num_articles=10):
33
  encoded_query = encode_special_characters(query)
34
  url = f"https://news.google.com/search?q={encoded_query}&hl=en-US&gl=in&ceid=US%3Aen&num={num_articles}"
@@ -58,12 +53,10 @@ def fetch_news(query, num_articles=10):
58
 
59
  return pd.DataFrame(news_data)
60
 
61
- # Function to perform sentiment analysis
62
  def analyze_sentiment(text):
63
  result = sentiment_model(text)[0]
64
  return result['label'], result['score']
65
 
66
- # Function to fetch stock data
67
  def fetch_stock_data(symbol):
68
  url = "https://alpha-vantage.p.rapidapi.com/query"
69
  querystring = {"function":"TIME_SERIES_DAILY", "symbol":symbol, "outputsize":"compact", "datatype":"json"}
@@ -82,18 +75,14 @@ def fetch_stock_data(symbol):
82
  stock_data.columns = ["Open", "High", "Low", "Close", "Volume"]
83
  return stock_data
84
 
85
- # Main function to process news and perform analysis
86
  def news_and_analysis(query):
87
- # Fetch news
88
  news_df = fetch_news(query)
89
 
90
  if news_df.empty:
91
  return "No news articles found.", None, None
92
 
93
- # Perform sentiment analysis
94
  news_df['Sentiment'], news_df['Sentiment_Score'] = zip(*news_df['Title'].apply(analyze_sentiment))
95
 
96
- # Create sentiment plot
97
  sentiment_fig = px.bar(
98
  news_df,
99
  x='Time',
@@ -104,7 +93,6 @@ def news_and_analysis(query):
104
  labels={'Time': 'Publication Time', 'Sentiment_Score': 'Sentiment Score'}
105
  )
106
 
107
- # Check if query is a company name and fetch stock data
108
  stock_symbol = get_stock_symbol(query)
109
  if stock_symbol:
110
  stock_data = fetch_stock_data(stock_symbol)
@@ -114,7 +102,6 @@ def news_and_analysis(query):
114
 
115
  return news_df, sentiment_fig, None
116
 
117
- # Gradio interface
118
  with gr.Blocks() as demo:
119
  gr.Markdown(
120
  """
@@ -136,10 +123,4 @@ with gr.Blocks() as demo:
136
  stock_plot = gr.Plot(label="Stock Price Movement")
137
 
138
  analyze_btn.click(
139
- news_and_analysis,
140
- inputs=[topic],
141
- outputs=[news_output, sentiment_plot, stock_plot]
142
- )
143
-
144
- if __name__ == "__main__":
145
- demo.launch()
 
6
  import plotly.express as px
7
  from datetime import datetime, timedelta
8
 
 
9
  file_path = '/home/user/app/Top 2000 Valued Companies with Ticker Symbols.xlsx'
10
  companies_df = pd.read_excel(file_path)
11
 
 
12
  def get_stock_symbol(company_name):
13
  match = companies_df[companies_df['Name'].str.contains(company_name, case=False, na=False)]
14
  if not match.empty:
15
  return match.iloc[0]['Symbol']
16
  return None
17
 
 
18
  sentiment_model = pipeline(model="finiteautomata/bertweet-base-sentiment-analysis")
19
 
 
20
  def encode_special_characters(text):
21
  encoded_text = ''
22
  special_characters = {'&': '%26', '=': '%3D', '+': '%2B', ' ': '%20'}
 
24
  encoded_text += special_characters.get(char, char)
25
  return encoded_text
26
 
 
27
  def fetch_news(query, num_articles=10):
28
  encoded_query = encode_special_characters(query)
29
  url = f"https://news.google.com/search?q={encoded_query}&hl=en-US&gl=in&ceid=US%3Aen&num={num_articles}"
 
53
 
54
  return pd.DataFrame(news_data)
55
 
 
56
  def analyze_sentiment(text):
57
  result = sentiment_model(text)[0]
58
  return result['label'], result['score']
59
 
 
60
  def fetch_stock_data(symbol):
61
  url = "https://alpha-vantage.p.rapidapi.com/query"
62
  querystring = {"function":"TIME_SERIES_DAILY", "symbol":symbol, "outputsize":"compact", "datatype":"json"}
 
75
  stock_data.columns = ["Open", "High", "Low", "Close", "Volume"]
76
  return stock_data
77
 
 
78
  def news_and_analysis(query):
 
79
  news_df = fetch_news(query)
80
 
81
  if news_df.empty:
82
  return "No news articles found.", None, None
83
 
 
84
  news_df['Sentiment'], news_df['Sentiment_Score'] = zip(*news_df['Title'].apply(analyze_sentiment))
85
 
 
86
  sentiment_fig = px.bar(
87
  news_df,
88
  x='Time',
 
93
  labels={'Time': 'Publication Time', 'Sentiment_Score': 'Sentiment Score'}
94
  )
95
 
 
96
  stock_symbol = get_stock_symbol(query)
97
  if stock_symbol:
98
  stock_data = fetch_stock_data(stock_symbol)
 
102
 
103
  return news_df, sentiment_fig, None
104
 
 
105
  with gr.Blocks() as demo:
106
  gr.Markdown(
107
  """
 
123
  stock_plot = gr.Plot(label="Stock Price Movement")
124
 
125
  analyze_btn.click(
126
+ news_and_analysi