Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
def sentiment_analysis_generate_text(text):
|
8 |
+
# Define the model
|
9 |
+
model_name = "yiyanghkust/finbert-tone"
|
10 |
+
|
11 |
+
# for faster, less model size use this model
|
12 |
+
# model_name = "mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
|
13 |
+
|
14 |
+
# Create the pipeline
|
15 |
+
nlp = pipeline("sentiment-analysis", model=model_name)
|
16 |
+
# Split the input text into individual sentences
|
17 |
+
sentences = text.split('|')
|
18 |
+
# Run the pipeline on each sentence and collect the results
|
19 |
+
results = nlp(sentences)
|
20 |
+
output = []
|
21 |
+
for sentence, result in zip(sentences, results):
|
22 |
+
output.append(f"Text: {sentence.strip()}\nSentiment: {result['label']}, Score: {result['score']:.4f}\n")
|
23 |
+
|
24 |
+
# Join the results into a single string to return
|
25 |
+
return "\n".join(output)
|
26 |
+
|
27 |
+
|
28 |
+
def sentiment_analysis_generate_table(text):
|
29 |
+
# Define the model
|
30 |
+
model_name = "yiyanghkust/finbert-tone"
|
31 |
+
# Create the pipeline
|
32 |
+
nlp = pipeline("sentiment-analysis", model=model_name)
|
33 |
+
# Split the input text into individual sentences
|
34 |
+
sentences = text.split('|')
|
35 |
+
|
36 |
+
# Generate the HTML table with enhanced colors and bold headers
|
37 |
+
html = """
|
38 |
+
<html>
|
39 |
+
<head>
|
40 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/css/bootstrap.min.css">
|
41 |
+
<style>
|
42 |
+
.label {
|
43 |
+
transition: .15s;
|
44 |
+
border-radius: 8px;
|
45 |
+
padding: 5px 10px;
|
46 |
+
font-size: 14px;
|
47 |
+
text-transform: uppercase;
|
48 |
+
}
|
49 |
+
.positive {
|
50 |
+
background-color: rgb(54, 176, 75);
|
51 |
+
color: white;
|
52 |
+
}
|
53 |
+
.negative {
|
54 |
+
background-color: rgb(237, 83, 80);
|
55 |
+
color: white;
|
56 |
+
}
|
57 |
+
.neutral {
|
58 |
+
background-color: rgb(52, 152, 219);
|
59 |
+
color: white;
|
60 |
+
}
|
61 |
+
th {
|
62 |
+
font-weight: bold;
|
63 |
+
color: rgb(106, 38, 198);
|
64 |
+
}
|
65 |
+
</style>
|
66 |
+
</head>
|
67 |
+
<body>
|
68 |
+
<table class="table table-striped">
|
69 |
+
<thead>
|
70 |
+
<tr>
|
71 |
+
<th scope="col">Text</th>
|
72 |
+
<th scope="col">Score</th>
|
73 |
+
<th scope="col">Sentiment</th>
|
74 |
+
</tr>
|
75 |
+
</thead>
|
76 |
+
<tbody>
|
77 |
+
"""
|
78 |
+
for sentence in sentences:
|
79 |
+
result = nlp(sentence.strip())[0]
|
80 |
+
text = sentence.strip()
|
81 |
+
score = f"{result['score']:.4f}"
|
82 |
+
sentiment = result['label']
|
83 |
+
|
84 |
+
# Determine the sentiment class
|
85 |
+
if sentiment == "Positive":
|
86 |
+
sentiment_class = "positive"
|
87 |
+
elif sentiment == "Negative":
|
88 |
+
sentiment_class = "negative"
|
89 |
+
else:
|
90 |
+
sentiment_class = "neutral"
|
91 |
+
|
92 |
+
# Generate table rows
|
93 |
+
html += f'<tr><td>{text}</td><td>{score}</td><td><span class="label {sentiment_class}">{sentiment}</span></td></tr>'
|
94 |
+
|
95 |
+
html += """
|
96 |
+
</tbody>
|
97 |
+
</table>
|
98 |
+
</body>
|
99 |
+
</html>
|
100 |
+
"""
|
101 |
+
|
102 |
+
return html
|
103 |
+
|
104 |
+
|
105 |
+
if __name__ == "__main__":
|
106 |
+
# uncomment below code for using the code in text results
|
107 |
+
# iface = gr.Interface(
|
108 |
+
# fn=sentiment_analysis_generate_text,
|
109 |
+
# inputs="text",
|
110 |
+
# outputs="text",
|
111 |
+
# title="Financial Sentiment Analysis",
|
112 |
+
# description="<p>A sentiment analysis model fine-tuned on financial news.</p>"
|
113 |
+
# "<p>Enter some financial text to see whether the sentiment is positive, neutral or negative.</p>"
|
114 |
+
# "<p><strong>Note:</strong> Separate multiple sentences with a '|'.",
|
115 |
+
# )
|
116 |
+
|
117 |
+
# generate the result in html format
|
118 |
+
iface = gr.Interface(
|
119 |
+
sentiment_analysis_generate_table,
|
120 |
+
gr.Textbox(placeholder="Enter sentence here..."),
|
121 |
+
["html"],
|
122 |
+
title="Financial Sentiment Analysis",
|
123 |
+
description="<p>A sentiment analysis model fine-tuned on financial news.</p>"
|
124 |
+
"<p>Enter some financial text to see whether the sentiment is positive, neutral or negative.</p>"
|
125 |
+
"<p><strong>Note:</strong> Separate multiple sentences with a '|'.",
|
126 |
+
examples=[
|
127 |
+
['growth is strong and we have plenty of liquidity.'],
|
128 |
+
['there is a shortage of capital, and we need extra financing.'],
|
129 |
+
['formulation patents might protect Vasotec to a limited extent.'],
|
130 |
+
["growth is strong and we have plenty of liquidity.|there is a shortage of capital"]
|
131 |
+
],
|
132 |
+
allow_flagging=False,
|
133 |
+
examples_per_page=2,
|
134 |
+
)
|
135 |
+
|
136 |
+
iface.launch()
|