devis2 commited on
Commit
4f4ee2f
1 Parent(s): af3031c

Update app.py with new dependencies and refactor code for Hugging Face spaces

Browse files
Files changed (2) hide show
  1. app.py +40 -22
  2. requirements.txt +4 -1
app.py CHANGED
@@ -3,28 +3,31 @@ from gnews import GNews
3
  import pandas as pd
4
  from transformers import pipeline
5
  from datetime import datetime, timedelta
 
 
6
 
7
 
8
- def discard_old_rows(df):
9
- # Convert the 'published date' column to datetime
10
- df['published date'] = pd.to_datetime(df['published date'], format='%a, %d %b %Y %H:%M:%S %Z')
11
 
12
- # Get the current date
13
- current_date = datetime.utcnow()
14
 
15
- # Calculate the date two months ago
16
- two_months_ago = current_date - timedelta(days=60)
17
 
18
- # Filter the DataFrame to keep only the rows with 'published date' within the last two months
19
- df_filtered = df[df['published date'] >= two_months_ago]
20
 
21
- return df_filtered
22
 
23
 
24
  def extract_and_clean_titles(df):
25
  # Initialize an empty list to store the cleaned titles
26
  values_list = []
27
-
 
28
  # Iterate over each value in the 'title' column of the DataFrame
29
  for value in df['title']:
30
  # Find the position of the first hyphen in the title
@@ -82,7 +85,7 @@ def calculate_weighted_average(predictions):
82
  return weighted_avg
83
 
84
 
85
- def sentiment_pie_chart(predictions):
86
  """
87
  Generates a pie chart for sentiment distribution.
88
  """
@@ -108,8 +111,13 @@ def sentiment_pie_chart(predictions):
108
  center_circle = plt.Circle((0, 0), 0.70, fc='white')
109
  fig.gca().add_artist(center_circle)
110
  ax.axis('equal')
111
- plt.title('Sentiment Analysis Results')
112
- return fig
 
 
 
 
 
113
 
114
 
115
  def main(stock):
@@ -117,16 +125,18 @@ def main(stock):
117
  model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
118
 
119
  #Scraping top data from google news
120
- google_news = GNews()
121
- Company_news=google_news.get_news(stock + "share")
122
  df=pd.DataFrame(Company_news)
123
-
124
  #Discarding old rows
125
- df=discard_old_rows(df)
126
-
 
127
  #Cleaning the titles for sentiment analysis
128
  values_list=extract_and_clean_titles(df)
129
 
 
130
  #Sentiment Analysis
131
  sentiment_analysis = pipeline(model=model)
132
 
@@ -137,13 +147,21 @@ def main(stock):
137
  weighted_avg=calculate_weighted_average(predictions)
138
 
139
  #Pie-Chart
140
- pie_chart = sentiment_pie_chart(predictions)
 
 
 
 
 
 
 
 
 
141
 
142
- return f'Weighted Sentiment Score: {weighted_avg:.2f}', pie_chart
143
 
144
  iface = gr.Interface(
145
  fn=main,
146
- inputs="textbox",
147
  outputs=["textbox","image"]
148
  )
149
 
 
3
  import pandas as pd
4
  from transformers import pipeline
5
  from datetime import datetime, timedelta
6
+ import matplotlib.pyplot as plt
7
+ import tensorflow as tf
8
 
9
 
10
+ # def discard_old_rows(df):
11
+ # # Convert the 'published date' column to datetime
12
+ # df['published date'] = pd.to_datetime(df['published date'], format='%a, %d %b %Y %H:%M:%S %Z')
13
 
14
+ # # Get the current date
15
+ # current_date = datetime.utcnow()
16
 
17
+ # # Calculate the date two months ago
18
+ # two_months_ago = current_date - timedelta(days=60)
19
 
20
+ # # Filter the DataFrame to keep only the rows with 'published date' within the last two months
21
+ # df_filtered = df[df['published date'] >= two_months_ago]
22
 
23
+ # return df_filtered
24
 
25
 
26
  def extract_and_clean_titles(df):
27
  # Initialize an empty list to store the cleaned titles
28
  values_list = []
29
+ if(df.empty):
30
+ return values_list
31
  # Iterate over each value in the 'title' column of the DataFrame
32
  for value in df['title']:
33
  # Find the position of the first hyphen in the title
 
85
  return weighted_avg
86
 
87
 
88
+ def sentiment_pie_chart(predictions, stock ,output_path='sentiment_pie_chart.png'):
89
  """
90
  Generates a pie chart for sentiment distribution.
91
  """
 
111
  center_circle = plt.Circle((0, 0), 0.70, fc='white')
112
  fig.gca().add_artist(center_circle)
113
  ax.axis('equal')
114
+ plt.title('Sentiment Analysis Results for ' + stock + ' Stock')
115
+
116
+ # Save the plot as an image file
117
+ plt.savefig(output_path)
118
+ plt.close(fig)
119
+ return output_path
120
+
121
 
122
 
123
  def main(stock):
 
125
  model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
126
 
127
  #Scraping top data from google news
128
+ google_news = GNews(max_results=50, period='30d')
129
+ Company_news=google_news.get_news(stock + "stock")
130
  df=pd.DataFrame(Company_news)
131
+ print(df)
132
  #Discarding old rows
133
+ # df=discard_old_rows(df)
134
+ if(df.empty):
135
+ return "Not enough data, please increase timeframe", None
136
  #Cleaning the titles for sentiment analysis
137
  values_list=extract_and_clean_titles(df)
138
 
139
+
140
  #Sentiment Analysis
141
  sentiment_analysis = pipeline(model=model)
142
 
 
147
  weighted_avg=calculate_weighted_average(predictions)
148
 
149
  #Pie-Chart
150
+ pie_chart_path = sentiment_pie_chart(predictions, stock)
151
+
152
+ if(weighted_avg>=-0.10 and weighted_avg<=0.10):
153
+ return f'{weighted_avg:.2f} (Stagnant)', pie_chart_path
154
+ elif(weighted_avg>0.1):
155
+ return f'{weighted_avg:.2f} (Positive)', pie_chart_path
156
+ else:
157
+ return f'{weighted_avg:.2f} (Negative)', pie_chart_path
158
+
159
+
160
 
 
161
 
162
  iface = gr.Interface(
163
  fn=main,
164
+ inputs=["textbox"],
165
  outputs=["textbox","image"]
166
  )
167
 
requirements.txt CHANGED
@@ -3,4 +3,7 @@ transformers
3
  pandas
4
  numpy
5
  gradio
6
- datetime
 
 
 
 
3
  pandas
4
  numpy
5
  gradio
6
+ datetime
7
+ matplotlib
8
+ tensorflow
9
+ tf-keras