Abdullahrasheed45 commited on
Commit
1f2644d
·
verified ·
1 Parent(s): a54aae7

Create app1.py

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