Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ class Config:
|
|
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__()
|
@@ -17,21 +17,44 @@ class FinancialBERT(torch.nn.Module):
|
|
17 |
def forward(self, input_ids, attention_mask, token_type_ids, labels=None):
|
18 |
output = self.bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids, labels=labels)
|
19 |
return output.loss, output.logits
|
20 |
-
|
21 |
# Load model
|
22 |
model = FinancialBERT()
|
23 |
model.eval()
|
24 |
-
|
25 |
# Streamlit App
|
|
|
26 |
st.title("Financial Sentiment Analysis")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
sentence = st.text_area("Enter a financial sentence:", "")
|
|
|
28 |
if st.button("Predict"):
|
29 |
tokenizer = Config.TOKENIZER
|
30 |
inputs = tokenizer([sentence], return_tensors="pt", truncation=True, padding=True, max_length=Config.MAX_LEN)
|
|
|
31 |
with torch.no_grad():
|
32 |
logits = model(input_ids=inputs['input_ids'], attention_mask=inputs['attention_mask'], token_type_ids=inputs.get('token_type_ids'))[1]
|
|
|
33 |
probs = torch.nn.functional.softmax(logits, dim=-1)
|
34 |
predictions = torch.argmax(probs, dim=-1)
|
35 |
sentiment = ['negative', 'neutral', 'positive'][predictions[0].item()]
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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__()
|
|
|
17 |
def forward(self, input_ids, attention_mask, token_type_ids, labels=None):
|
18 |
output = self.bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids, labels=labels)
|
19 |
return output.loss, output.logits
|
20 |
+
|
21 |
# Load model
|
22 |
model = FinancialBERT()
|
23 |
model.eval()
|
24 |
+
|
25 |
# Streamlit App
|
26 |
+
# Set title and an image/banner if you have one
|
27 |
st.title("Financial Sentiment Analysis")
|
28 |
+
# st.image("path_to_your_image.jpg", use_column_width=True)
|
29 |
+
|
30 |
+
# Description
|
31 |
+
st.write("""
|
32 |
+
This application predicts the sentiment of financial sentences using a state-of-the-art model. Enter a financial sentence below and click 'Predict' to get its sentiment.
|
33 |
+
""")
|
34 |
+
|
35 |
sentence = st.text_area("Enter a financial sentence:", "")
|
36 |
+
|
37 |
if st.button("Predict"):
|
38 |
tokenizer = Config.TOKENIZER
|
39 |
inputs = tokenizer([sentence], return_tensors="pt", truncation=True, padding=True, max_length=Config.MAX_LEN)
|
40 |
+
|
41 |
with torch.no_grad():
|
42 |
logits = model(input_ids=inputs['input_ids'], attention_mask=inputs['attention_mask'], token_type_ids=inputs.get('token_type_ids'))[1]
|
43 |
+
|
44 |
probs = torch.nn.functional.softmax(logits, dim=-1)
|
45 |
predictions = torch.argmax(probs, dim=-1)
|
46 |
sentiment = ['negative', 'neutral', 'positive'][predictions[0].item()]
|
47 |
|
48 |
+
# Output visualization
|
49 |
+
st.subheader('Predicted Sentiment:')
|
50 |
+
st.write(f"The sentiment is: **{sentiment.capitalize()}**")
|
51 |
+
|
52 |
+
# Show Confidence levels as a bar chart
|
53 |
+
st.subheader('Model Confidence Levels:')
|
54 |
+
st.bar_chart(probs[0].numpy(), use_container_width=True)
|
55 |
+
|
56 |
+
# Sidebar: Documentation/Help
|
57 |
+
st.sidebar.header('About')
|
58 |
+
st.sidebar.text("""
|
59 |
+
This application uses a BERT-based model trained specifically for financial sentences. The model can predict if the sentiment of a sentence is positive, negative, or neutral.
|
60 |
+
""")
|