khandelwalkishna15 commited on
Commit
6d2354e
0 Parent(s):

first commit

Browse files
Files changed (3) hide show
  1. app.py +31 -0
  2. requirements.txt +3 -0
  3. sentiment_analysis.py +17 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+
4
+ # Load pre-trained model and tokenizer
5
+ model_name = "mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis" # Replace with your chosen model
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
+
9
+ def predict_sentiment(text):
10
+ inputs = tokenizer(text, return_tensors="pt")
11
+ outputs = model(**inputs)
12
+ sentiment_class = outputs.logits.argmax(dim=1).item()
13
+ sentiment_mapping = {0: 'Negative', 1: 'Neutral', 2: 'Positive'}
14
+ predicted_sentiment = sentiment_mapping.get(sentiment_class, 'Unknown')
15
+ return predicted_sentiment
16
+
17
+ def main():
18
+ st.title("Financial Sentiment Analysis")
19
+
20
+ # Get user input
21
+ text = st.text_area("Enter financial content:")
22
+
23
+ if st.button("Classify Sentiment"):
24
+ if text:
25
+ predicted_sentiment = predict_sentiment(text)
26
+ st.success(f"Predicted sentiment: {predicted_sentiment}")
27
+ else:
28
+ st.warning("Please enter some text.")
29
+
30
+ if __name__ == "__main__":
31
+ main()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ torch
sentiment_analysis.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
2
+
3
+ # Load pre-trained model and tokenizer
4
+ #model_name = "ProsusAI/finbert"
5
+ #model_name = "ahmedrachid/FinancialBERT-Sentiment-Analysis"
6
+ model_name="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
+
10
+ # Example: Classify a financial text
11
+ text = "With the launch of Apple Silicon, Apple shares have increased"
12
+ inputs = tokenizer(text, return_tensors="pt")
13
+ outputs = model(**inputs)
14
+ predictions = outputs.logits.argmax(dim=1).item()
15
+
16
+ # 'predictions' will contain the sentiment class (e.g., 0 for negative, 1 for neutral, 2 for positive)
17
+ print("Predicted sentiment class:", predictions)