Spaces:
Sleeping
Sleeping
Ahmad-Moiz
commited on
Commit
•
96b7659
1
Parent(s):
37dc33e
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the tokenizer and model
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained("mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
|
8 |
+
|
9 |
+
# Set the model to evaluation mode
|
10 |
+
model.eval()
|
11 |
+
|
12 |
+
def predict_sentiment(text):
|
13 |
+
# Tokenize the input text and prepare it to be used as model input
|
14 |
+
inputs = tokenizer.encode_plus(
|
15 |
+
text,
|
16 |
+
add_special_tokens=True,
|
17 |
+
return_tensors="pt",
|
18 |
+
max_length=512, # You can adjust the max length based on your requirements
|
19 |
+
truncation=True
|
20 |
+
)
|
21 |
+
|
22 |
+
# Perform the prediction
|
23 |
+
with torch.no_grad():
|
24 |
+
outputs = model(**inputs)
|
25 |
+
predicted_class = torch.argmax(outputs.logits, dim=1).item()
|
26 |
+
|
27 |
+
# Determine the sentiment based on the predicted class
|
28 |
+
if predicted_class == 0:
|
29 |
+
return "Negative sentiment"
|
30 |
+
elif predicted_class == 1:
|
31 |
+
return "Neutral sentiment"
|
32 |
+
elif predicted_class == 2:
|
33 |
+
return "Positive sentiment"
|
34 |
+
|
35 |
+
# Streamlit interface
|
36 |
+
st.title("Financial News Sentiment Analysis")
|
37 |
+
|
38 |
+
user_input = st.text_area("Enter the financial news text:")
|
39 |
+
if st.button("Analyze"):
|
40 |
+
if user_input:
|
41 |
+
# Get the sentiment prediction
|
42 |
+
sentiment = predict_sentiment(user_input)
|
43 |
+
st.write("Predicted sentiment:", sentiment)
|
44 |
+
else:
|
45 |
+
st.write("Please enter some text.")
|