Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,38 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import torch
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
class FinancialBERT(torch.nn.Module):
|
| 6 |
-
def __init__(self
|
| 7 |
super(FinancialBERT, self).__init__()
|
| 8 |
-
self.bert =
|
| 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 |
-
|
| 16 |
-
|
| 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 |
-
|
| 24 |
-
|
| 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 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Config class
|
| 6 |
+
class Config:
|
| 7 |
+
TOKENIZER_PATH = "ahmedrachid/FinancialBERT" # Use tokenizer from the original model
|
| 8 |
+
MODEL_PATH = "Sandy0909/finance_sentiment"
|
| 9 |
+
MAX_LEN = 512
|
| 10 |
+
TOKENIZER = BertTokenizer.from_pretrained(TOKENIZER_PATH)
|
| 11 |
+
|
| 12 |
class FinancialBERT(torch.nn.Module):
|
| 13 |
+
def __init__(self):
|
| 14 |
super(FinancialBERT, self).__init__()
|
| 15 |
+
self.bert = BertForSequenceClassification.from_pretrained(Config.MODEL_PATH, num_labels=3, hidden_dropout_prob=0.5)
|
| 16 |
+
|
| 17 |
+
def forward(self, input_ids, attention_mask, labels=None):
|
| 18 |
+
output = self.bert(input_ids, attention_mask=attention_mask, labels=labels)
|
| 19 |
return output.loss, output.logits
|
| 20 |
+
|
| 21 |
# Load model
|
| 22 |
+
model = FinancialBERT()
|
| 23 |
+
|
| 24 |
model.eval()
|
| 25 |
+
|
| 26 |
# Streamlit App
|
| 27 |
st.title("Financial Sentiment Analysis")
|
| 28 |
sentence = st.text_area("Enter a financial sentence:", "")
|
| 29 |
if st.button("Predict"):
|
| 30 |
+
tokenizer = Config.TOKENIZER
|
| 31 |
+
inputs = tokenizer([sentence], return_tensors="pt", truncation=True, padding=True, max_length=Config.MAX_LEN)
|
|
|
|
| 32 |
with torch.no_grad():
|
| 33 |
logits = model(**inputs)[1]
|
| 34 |
probs = torch.nn.functional.softmax(logits, dim=-1)
|
| 35 |
predictions = torch.argmax(probs, dim=-1)
|
| 36 |
sentiment = ['negative', 'neutral', 'positive'][predictions[0].item()]
|
| 37 |
+
|
| 38 |
+
st.write(f"The predicted sentiment is: {sentiment}")
|