Spaces:
Sleeping
Sleeping
d.vien
commited on
Commit
·
a9f3ca7
1
Parent(s):
eff645b
Added app.py and requirements.txt
Browse files- app.py +35 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
3 |
+
import torch
|
4 |
+
|
5 |
+
model_name = "yiyanghkust/finbert-tone"
|
6 |
+
tokenizer = BertTokenizer.from_pretrained(model_name)
|
7 |
+
model = BertForSequenceClassification.from_pretrained(model_name)
|
8 |
+
|
9 |
+
def analyze_sentiment(text):
|
10 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
11 |
+
|
12 |
+
with torch.no_grad():
|
13 |
+
outputs = model(**inputs)
|
14 |
+
logits = outputs.logits
|
15 |
+
|
16 |
+
sentiment = torch.argmax(logits, dim=1).item()
|
17 |
+
|
18 |
+
if sentiment == 0:
|
19 |
+
return "Negative"
|
20 |
+
elif sentiment == 1:
|
21 |
+
return "Neutral"
|
22 |
+
else:
|
23 |
+
return "Positive"
|
24 |
+
|
25 |
+
st.title("FinBERT Sentiment Analysis")
|
26 |
+
st.write(
|
27 |
+
"This app uses FinBERT model to analyze sentiment of financial texts. "
|
28 |
+
"Enter text below to get its sentiment classification."
|
29 |
+
)
|
30 |
+
|
31 |
+
text_input = st.text_area("Enter your text here:")
|
32 |
+
|
33 |
+
if text_input:
|
34 |
+
sentiment = analyze_sentiment(text_input)
|
35 |
+
st.write(f"Sentiment: {sentiment}")
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
transformers>=4.0.0
|
2 |
+
torch>=1.10.0
|
3 |
+
streamlit>=1.0.0
|