Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import os,requests
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
|
6 |
+
API_URL = "https://api-inference.huggingface.co/models/ahmedrachid/FinancialBERT-Sentiment-Analysis"
|
7 |
+
API_TOKEN = os.environ['API_TOKEN']
|
8 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
9 |
+
|
10 |
+
def get_chart(score, color):
|
11 |
+
# Create figure and axis
|
12 |
+
fig, ax = plt.subplots(figsize=(3, 3), subplot_kw=dict(aspect="equal"))
|
13 |
+
|
14 |
+
# Create the pie chart, which looks like a donut
|
15 |
+
wedges, texts = ax.pie([score, 100-score], startangle=90, counterclock=False, colors=[color, '#dddddd'])
|
16 |
+
|
17 |
+
# Draw a white circle in the center
|
18 |
+
centre_circle = plt.Circle((0,0),0.85,fc='white')
|
19 |
+
fig.gca().add_artist(centre_circle)
|
20 |
+
|
21 |
+
# Equal aspect ratio ensures that pie is drawn as a circle.
|
22 |
+
ax.axis('equal')
|
23 |
+
|
24 |
+
# Add text in the center
|
25 |
+
plt.text(0, 0, f'{score}%', horizontalalignment='center', verticalalignment='center', fontsize=18, color=color)
|
26 |
+
return fig
|
27 |
+
|
28 |
+
def query(Statement):
|
29 |
+
response = requests.post(API_URL, headers=headers, json=Statement)
|
30 |
+
|
31 |
+
print(response.json())
|
32 |
+
response_json = response.json()[0]
|
33 |
+
positive_score = 0
|
34 |
+
neutral_score = 0
|
35 |
+
negative_score = 0
|
36 |
+
|
37 |
+
for entry in response_json:
|
38 |
+
if entry['label'] == 'positive':
|
39 |
+
positive_score = round(entry['score']*100,2)
|
40 |
+
elif entry['label'] == 'neutral':
|
41 |
+
neutral_score = round(entry['score']*100,2)
|
42 |
+
elif entry['label'] == 'negative':
|
43 |
+
negative_score = round(entry['score']*100,2)
|
44 |
+
|
45 |
+
|
46 |
+
labels = ['Negative', 'Neutral', 'Positive']
|
47 |
+
values = [negative_score, neutral_score, positive_score ]
|
48 |
+
|
49 |
+
max_score_dict = max(response_json, key=lambda x: x['score'])
|
50 |
+
|
51 |
+
max_label = max_score_dict['label'].capitalize()
|
52 |
+
|
53 |
+
positive_plot = get_chart(positive_score, '#32CD32')
|
54 |
+
negative_plot = get_chart(negative_score, '#CE2029')
|
55 |
+
neutral_plot = get_chart(neutral_score, '#ADD8E6')
|
56 |
+
return f"Overall sentiment is {max_label}", positive_plot, neutral_plot, negative_plot
|
57 |
+
|
58 |
+
with gr.Blocks() as financial_sentiment_interface:
|
59 |
+
gr.Markdown("# Financial Sentiment Analysis")
|
60 |
+
with gr.Row():
|
61 |
+
with gr.Column():
|
62 |
+
financial_content = gr.Textbox(lines=2, placeholder="Your Financial Content Here...", label="Financial News")
|
63 |
+
submit_btn = gr.Button(value="Submit")
|
64 |
+
sentiment = gr.Textbox(label="Sentiment")
|
65 |
+
with gr.Row():
|
66 |
+
positive_plot = gr.Plot(label="Positive Sentiment")
|
67 |
+
neutral_plot = gr.Plot(label="Neutral Sentiment")
|
68 |
+
negative_plot = gr.Plot(label="Negative Sentiment")
|
69 |
+
|
70 |
+
submit_btn.click(query, inputs=financial_content, outputs=[sentiment,positive_plot,neutral_plot,negative_plot], api_name="sentiment-analysis")
|
71 |
+
|
72 |
+
financial_sentiment_interface.launch()
|