JayLacoma commited on
Commit
ff008e1
·
verified ·
1 Parent(s): 425b485

Upload News_Sentiment_Analysis.py

Browse files
Files changed (1) hide show
  1. News_Sentiment_Analysis.py +92 -0
News_Sentiment_Analysis.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # %%
2
+ import torch
3
+ import numpy as np
4
+ import pandas as pd
5
+ from datetime import datetime
6
+ from newsapi import NewsApiClient
7
+ from transformers import pipeline
8
+ import plotly.graph_objects as go
9
+ import gradio as gr
10
+
11
+ # Initialize News API client with your API key
12
+ newsapi = NewsApiClient(api_key='381793b3d6834758918838bca0cf52ee')
13
+
14
+ # Define sentiment analyzer using FinBERT
15
+ sentiment_analyzer = pipeline("text-classification", model="ProsusAI/finbert")
16
+
17
+ # Function to fetch news, analyze sentiment, and create an interactive Plotly plot
18
+ def analyze_news_sentiment(company_name):
19
+ # Fetch news articles related to the company
20
+ news = newsapi.get_everything(q=company_name, language='en', sort_by='publishedAt')
21
+
22
+ # Extract headlines from news articles
23
+ headlines = [article['title'] for article in news['articles']]
24
+
25
+ # Perform sentiment analysis on the headlines
26
+ result = sentiment_analyzer(headlines)
27
+ df = pd.DataFrame(result)
28
+
29
+ # Map labels to numeric values
30
+ label_mapping = {'positive': 1, 'neutral': 0, 'negative': -1}
31
+ df['sentiment'] = df['label'].map(label_mapping)
32
+
33
+ # Drop the 'label' column
34
+ df.drop(columns=['label'], inplace=True)
35
+
36
+ # Filter out neutral sentiment values
37
+ positive_sentiment = df[df['sentiment'] == 1]['sentiment']
38
+ negative_sentiment = df[df['sentiment'] == -1]['sentiment']
39
+
40
+ # Create the interactive Plotly histogram
41
+ fig = go.Figure()
42
+
43
+ # Add Positive sentiment histogram
44
+ fig.add_trace(go.Histogram(
45
+ x=positive_sentiment,
46
+ nbinsx=1,
47
+ name='Positive',
48
+ marker_color='purple',
49
+ opacity=0.75
50
+ ))
51
+
52
+ # Add Negative sentiment histogram
53
+ fig.add_trace(go.Histogram(
54
+ x=negative_sentiment,
55
+ nbinsx=1,
56
+ name='Negative',
57
+ marker_color='skyblue',
58
+ opacity=0.75
59
+ ))
60
+
61
+ # Update layout for better visualization
62
+ fig.update_layout(
63
+ title=f'Sentiment Distribution for {company_name}',
64
+ xaxis_title='Sentiment',
65
+ yaxis_title='Count',
66
+ barmode='overlay',
67
+ plot_bgcolor='black',
68
+ paper_bgcolor='black',
69
+ font=dict(color='white'),
70
+ xaxis=dict(tickvals=[-1, 1], ticktext=['Negative', 'Positive']),
71
+ bargap=0.2,
72
+ )
73
+
74
+ return fig
75
+
76
+
77
+
78
+
79
+ # %%
80
+ # Create a Gradio interface
81
+ interface = gr.Interface(
82
+ fn=analyze_news_sentiment, # Function to run
83
+ inputs=gr.Textbox(label="Enter Company Name"), # Input: company name
84
+ outputs=gr.Plot(label="Sentiment Distribution"), # Output: Interactive Plotly chart
85
+ title="Sentiment Analysis on News Headlines",
86
+ description="Enter a company name to analyze the sentiment of the latest news related to that company."
87
+ )
88
+
89
+ # Launch the Gradio app
90
+ interface.launch()
91
+
92
+