TanishqO0F commited on
Commit
b25e082
·
verified ·
1 Parent(s): d3a1df6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -3
app.py CHANGED
@@ -52,13 +52,39 @@ def analyze_sentiment(text):
52
  result = sentiment_model(text)[0]
53
  return result['label'], result['score']
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  # Main function to process news and perform analysis
56
  def news_and_analysis(query):
57
  # Fetch news
58
  news_df = fetch_news(query)
59
 
60
  if news_df.empty:
61
- return "No news articles found.", None
62
 
63
  # Perform sentiment analysis
64
  news_df['Sentiment'], news_df['Sentiment_Score'] = zip(*news_df['Title'].apply(analyze_sentiment))
@@ -74,7 +100,19 @@ def news_and_analysis(query):
74
  labels={'Time': 'Publication Time', 'Sentiment_Score': 'Sentiment Score'}
75
  )
76
 
77
- return news_df, sentiment_fig
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  # Gradio interface
80
  with gr.Blocks() as demo:
@@ -95,11 +133,12 @@ with gr.Blocks() as demo:
95
  with gr.Column():
96
  news_output = gr.DataFrame(label="News and Sentiment Analysis")
97
  sentiment_plot = gr.Plot(label="Sentiment Analysis")
 
98
 
99
  analyze_btn.click(
100
  news_and_analysis,
101
  inputs=[topic],
102
- outputs=[news_output, sentiment_plot]
103
  )
104
 
105
  if __name__ == "__main__":
 
52
  result = sentiment_model(text)[0]
53
  return result['label'], result['score']
54
 
55
+ # Function to fetch stock data from Alpha Vantage API
56
+ def fetch_stock_data(company_name):
57
+ url = "https://alpha-vantage.p.rapidapi.com/query"
58
+ querystring = {"function": "TIME_SERIES_DAILY", "symbol": company_name, "outputsize": "compact", "datatype": "json"}
59
+ headers = {
60
+ "x-rapidapi-key": "e078dae417mshb13ddc2d8149768p1608e9jsn888ce49e8554",
61
+ "x-rapidapi-host": "alpha-vantage.p.rapidapi.com"
62
+ }
63
+
64
+ try:
65
+ response = requests.get(url, headers=headers, params=querystring)
66
+ response.raise_for_status()
67
+ data = response.json()
68
+ except requests.RequestException as e:
69
+ print(f"Error fetching stock data: {e}")
70
+ return None
71
+
72
+ if "Time Series (Daily)" not in data:
73
+ return None
74
+
75
+ stock_data = data["Time Series (Daily)"]
76
+ df = pd.DataFrame.from_dict(stock_data, orient='index')
77
+ df.index = pd.to_datetime(df.index)
78
+ df = df.astype(float)
79
+ return df
80
+
81
  # Main function to process news and perform analysis
82
  def news_and_analysis(query):
83
  # Fetch news
84
  news_df = fetch_news(query)
85
 
86
  if news_df.empty:
87
+ return "No news articles found.", None, None
88
 
89
  # Perform sentiment analysis
90
  news_df['Sentiment'], news_df['Sentiment_Score'] = zip(*news_df['Title'].apply(analyze_sentiment))
 
100
  labels={'Time': 'Publication Time', 'Sentiment_Score': 'Sentiment Score'}
101
  )
102
 
103
+ # Check if the input query is a company name (heuristic: if it's more than one word)
104
+ if len(query.split()) > 1:
105
+ stock_df = fetch_stock_data(query)
106
+ if stock_df is not None:
107
+ stock_fig = px.line(
108
+ stock_df,
109
+ x=stock_df.index,
110
+ y='4. close',
111
+ title=f'{query} Stock Price Over Time',
112
+ labels={'index': 'Date', '4. close': 'Closing Price'}
113
+ )
114
+ return news_df, sentiment_fig, stock_fig
115
+ return news_df, sentiment_fig, None
116
 
117
  # Gradio interface
118
  with gr.Blocks() as demo:
 
133
  with gr.Column():
134
  news_output = gr.DataFrame(label="News and Sentiment Analysis")
135
  sentiment_plot = gr.Plot(label="Sentiment Analysis")
136
+ stock_plot = gr.Plot(label="Stock Price Analysis")
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__":