Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,31 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import BertTokenizer, BertForSequenceClassification
|
3 |
import torch
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
model =
|
12 |
-
model.load_state_dict(torch.load(MODEL_PATH, map_location=torch.device('cpu')))
|
13 |
model.eval()
|
14 |
|
15 |
-
|
16 |
-
inputs = tokenizer(sentence, return_tensors="pt", truncation=True, padding=True, max_length=MAX_LEN)
|
17 |
-
with torch.no_grad():
|
18 |
-
logits = model(**inputs).logits
|
19 |
-
predictions = torch.argmax(logits, dim=-1)
|
20 |
-
return ['negative', 'neutral', 'positive'][predictions[0].item()]
|
21 |
-
|
22 |
-
# Streamlit app
|
23 |
st.title("Financial Sentiment Analysis")
|
24 |
sentence = st.text_area("Enter a financial sentence:", "")
|
25 |
if st.button("Predict"):
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
st.write(f"The predicted sentiment is: {sentiment}")
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import torch
|
3 |
|
4 |
+
# Model class
|
5 |
+
class FinancialBERT(torch.nn.Module):
|
6 |
+
def __init__(self, model_path):
|
7 |
+
super(FinancialBERT, self).__init__()
|
8 |
+
self.bert = torch.load(model_path)
|
9 |
+
|
10 |
+
def forward(self, input_ids, attention_mask):
|
11 |
+
output = self.bert(input_ids, attention_mask=attention_mask)
|
12 |
+
return output.loss, output.logits
|
13 |
|
14 |
+
# Load model
|
15 |
+
MODEL_PATH = "Sandy0909/finance_sentiment"
|
16 |
+
model = FinancialBERT(MODEL_PATH)
|
|
|
17 |
model.eval()
|
18 |
|
19 |
+
# Streamlit App
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
st.title("Financial Sentiment Analysis")
|
21 |
sentence = st.text_area("Enter a financial sentence:", "")
|
22 |
if st.button("Predict"):
|
23 |
+
# Here, you'll need some way to tokenize the input sentence and turn it into tensors.
|
24 |
+
# This part has been removed in the provided code.
|
25 |
+
# inputs = ...
|
26 |
+
with torch.no_grad():
|
27 |
+
logits = model(**inputs)[1]
|
28 |
+
probs = torch.nn.functional.softmax(logits, dim=-1)
|
29 |
+
predictions = torch.argmax(probs, dim=-1)
|
30 |
+
sentiment = ['negative', 'neutral', 'positive'][predictions[0].item()]
|
31 |
st.write(f"The predicted sentiment is: {sentiment}")
|