|
|
|
import torch |
|
import numpy as np |
|
import pandas as pd |
|
from datetime import datetime |
|
from newsapi import NewsApiClient |
|
from transformers import pipeline |
|
import plotly.graph_objects as go |
|
import gradio as gr |
|
|
|
|
|
newsapi = NewsApiClient(api_key='381793b3d6834758918838bca0cf52ee') |
|
|
|
|
|
sentiment_analyzer = pipeline("text-classification", model="ProsusAI/finbert") |
|
|
|
|
|
def analyze_news_sentiment(company_name): |
|
|
|
news = newsapi.get_everything(q=company_name, language='en', sort_by='publishedAt') |
|
|
|
|
|
headlines = [article['title'] for article in news['articles']] |
|
|
|
|
|
result = sentiment_analyzer(headlines) |
|
df = pd.DataFrame(result) |
|
|
|
|
|
label_mapping = {'positive': 1, 'neutral': 0, 'negative': -1} |
|
df['sentiment'] = df['label'].map(label_mapping) |
|
|
|
|
|
df.drop(columns=['label'], inplace=True) |
|
|
|
|
|
positive_sentiment = df[df['sentiment'] == 1]['sentiment'] |
|
negative_sentiment = df[df['sentiment'] == -1]['sentiment'] |
|
|
|
|
|
fig = go.Figure() |
|
|
|
|
|
fig.add_trace(go.Histogram( |
|
x=positive_sentiment, |
|
nbinsx=1, |
|
name='Positive', |
|
marker_color='purple', |
|
opacity=0.75 |
|
)) |
|
|
|
|
|
fig.add_trace(go.Histogram( |
|
x=negative_sentiment, |
|
nbinsx=1, |
|
name='Negative', |
|
marker_color='skyblue', |
|
opacity=0.75 |
|
)) |
|
|
|
|
|
fig.update_layout( |
|
title=f'Sentiment Distribution for {company_name}', |
|
xaxis_title='Sentiment', |
|
yaxis_title='Count', |
|
barmode='overlay', |
|
plot_bgcolor='black', |
|
paper_bgcolor='black', |
|
font=dict(color='white'), |
|
xaxis=dict(tickvals=[-1, 1], ticktext=['Negative', 'Positive']), |
|
bargap=0.2, |
|
) |
|
|
|
return fig |
|
|
|
|
|
|
|
|
|
|
|
|
|
interface = gr.Interface( |
|
fn=analyze_news_sentiment, |
|
inputs=gr.Textbox(label="Enter Company Name"), |
|
outputs=gr.Plot(label="Sentiment Distribution"), |
|
title="Sentiment Analysis on News Headlines", |
|
description="Enter a company name to analyze the sentiment of the latest news related to that company." |
|
) |
|
|
|
|
|
interface.launch() |
|
|
|
|
|
|