Spaces:
Runtime error
Runtime error
File size: 1,090 Bytes
f151edb 52c7db2 f151edb 52c7db2 f151edb 52c7db2 a6ad2ae 52c7db2 f151edb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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}")
|