Spaces:
Runtime error
Runtime error
import streamlit as st | |
import torch | |
# Model class | |
class FinancialBERT(torch.nn.Module): | |
def __init__(self, model_path): | |
super(FinancialBERT, self).__init__() | |
self.bert = torch.load(model_path) | |
def forward(self, input_ids, attention_mask): | |
output = self.bert(input_ids, attention_mask=attention_mask) | |
return output.loss, output.logits | |
# Load model | |
MODEL_PATH = "Sandy0909/finance_sentiment" | |
model = FinancialBERT(MODEL_PATH) | |
model.eval() | |
# Streamlit App | |
st.title("Financial Sentiment Analysis") | |
sentence = st.text_area("Enter a financial sentence:", "") | |
if st.button("Predict"): | |
# Here, you'll need some way to tokenize the input sentence and turn it into tensors. | |
# This part has been removed in the provided code. | |
# inputs = ... | |
with torch.no_grad(): | |
logits = model(**inputs)[1] | |
probs = torch.nn.functional.softmax(logits, dim=-1) | |
predictions = torch.argmax(probs, dim=-1) | |
sentiment = ['negative', 'neutral', 'positive'][predictions[0].item()] | |
st.write(f"The predicted sentiment is: {sentiment}") | |