Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,27 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import BertTokenizer, BertForSequenceClassification
|
| 3 |
import torch
|
| 4 |
-
import torch.nn as nn
|
| 5 |
-
from torch.utils.data import Dataset, DataLoader
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
MODEL_PATH = "Sandy0909/finance_sentiment"
|
| 11 |
-
TRAIN_BATCH_SIZE = 32
|
| 12 |
-
VALID_BATCH_SIZE = 32
|
| 13 |
-
EPOCHS = 10
|
| 14 |
-
MAX_LEN = 512
|
| 15 |
-
TOKENIZER = BertTokenizer.from_pretrained(BERT_PATH)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
self.bert = BertForSequenceClassification.from_pretrained(Config.BERT_PATH, num_labels=3, hidden_dropout_prob=0.3)
|
| 22 |
-
|
| 23 |
-
def forward(self, input_ids, attention_mask, token_type_ids=None, labels=None):
|
| 24 |
-
output = self.bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids, labels=labels)
|
| 25 |
-
return output.loss, output.logits
|
| 26 |
-
|
| 27 |
-
# Load model
|
| 28 |
-
model = FinancialBERT()
|
| 29 |
-
model.load_state_dict(torch.load(Config.MODEL_PATH, map_location=torch.device('cpu')))
|
| 30 |
model.eval()
|
| 31 |
|
| 32 |
-
|
| 33 |
-
tokenizer =
|
| 34 |
-
|
| 35 |
-
def predict_sentiment(sentences):
|
| 36 |
-
inputs = tokenizer(sentences, return_tensors="pt", truncation=True, padding=True, max_length=Config.MAX_LEN)
|
| 37 |
with torch.no_grad():
|
| 38 |
-
logits = model(**inputs)
|
| 39 |
-
|
| 40 |
-
predictions = torch.argmax(probs, dim=-1)
|
| 41 |
return ['negative', 'neutral', 'positive'][predictions[0].item()]
|
| 42 |
|
| 43 |
# Streamlit app
|
| 44 |
st.title("Financial Sentiment Analysis")
|
| 45 |
sentence = st.text_area("Enter a financial sentence:", "")
|
| 46 |
if st.button("Predict"):
|
| 47 |
-
sentiment = predict_sentiment(
|
| 48 |
st.write(f"The predicted sentiment is: {sentiment}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
BERT_PATH = "ahmedrachid/FinancialBERT"
|
| 6 |
+
MODEL_PATH = "Sandy0909/finance_sentiment"
|
| 7 |
+
MAX_LEN = 512
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Initialize tokenizer and model
|
| 10 |
+
tokenizer = BertTokenizer.from_pretrained(BERT_PATH)
|
| 11 |
+
model = BertForSequenceClassification.from_pretrained(BERT_PATH, num_labels=3)
|
| 12 |
+
model.load_state_dict(torch.load(MODEL_PATH, map_location=torch.device('cpu')))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
model.eval()
|
| 14 |
|
| 15 |
+
def predict_sentiment(sentence):
|
| 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 |
+
sentiment = predict_sentiment(sentence)
|
| 27 |
st.write(f"The predicted sentiment is: {sentiment}")
|