Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import re
|
| 5 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 6 |
+
import torch
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
# Load the model and tokenizer
|
| 11 |
+
@st.cache(allow_output_mutation=True)
|
| 12 |
+
def load_model():
|
| 13 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
| 14 |
+
model = BertForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=8)
|
| 15 |
+
return tokenizer, model
|
| 16 |
+
|
| 17 |
+
tokenizer, model = load_model()
|
| 18 |
+
|
| 19 |
+
# Custom sentiments
|
| 20 |
+
sentiments = ["happy", "motivated", "growth", "optimistic", "jealousy", "frustrated", "decline", "angry"]
|
| 21 |
+
|
| 22 |
+
# Define the preprocessing function
|
| 23 |
+
def preprocess_text(text):
|
| 24 |
+
text = re.sub(r'[^\w\s]', '', text.lower()) # Remove punctuation and lowercase
|
| 25 |
+
text = re.sub(r'\d+', '', text) # Remove numbers
|
| 26 |
+
return text
|
| 27 |
+
|
| 28 |
+
# Title and instructions
|
| 29 |
+
st.title("Sentiment Analysis of Financial News")
|
| 30 |
+
st.write("Enter a sentence to analyze its sentiment across predefined categories.")
|
| 31 |
+
|
| 32 |
+
# Input text from user
|
| 33 |
+
text = st.text_input("Enter a sentence:", "")
|
| 34 |
+
|
| 35 |
+
if text:
|
| 36 |
+
# Preprocess and tokenize
|
| 37 |
+
cleaned_text = preprocess_text(text)
|
| 38 |
+
inputs = tokenizer(cleaned_text, return_tensors="pt", truncation=True, padding=True)
|
| 39 |
+
|
| 40 |
+
# Get model predictions
|
| 41 |
+
with torch.no_grad():
|
| 42 |
+
outputs = model(**inputs)
|
| 43 |
+
sentiment_score = outputs.logits.softmax(dim=1)
|
| 44 |
+
|
| 45 |
+
# Convert tensor to list for plotting
|
| 46 |
+
score_list = sentiment_score.tolist()[0]
|
| 47 |
+
|
| 48 |
+
# Display sentiment scores as a table
|
| 49 |
+
st.subheader("Sentiment Scores")
|
| 50 |
+
score_df = pd.DataFrame({"Sentiment": sentiments, "Score": score_list})
|
| 51 |
+
st.dataframe(score_df)
|
| 52 |
+
|
| 53 |
+
# Plot the sentiment scores
|
| 54 |
+
st.subheader("Sentiment Score Chart")
|
| 55 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 56 |
+
mustard_yellow = "#FFDB58"
|
| 57 |
+
|
| 58 |
+
# Plot bars with spacing and color
|
| 59 |
+
ax.bar(np.arange(len(sentiments)) * 1.5, score_list, color=mustard_yellow, edgecolor="black", width=0.8)
|
| 60 |
+
|
| 61 |
+
# Customize the plot
|
| 62 |
+
ax.set_xlabel("Sentiments", color="black", fontsize=12)
|
| 63 |
+
ax.set_ylabel("Scores", color="black", fontsize=12)
|
| 64 |
+
ax.set_title("Sentiment Analysis of Financial News", color="black", fontsize=14)
|
| 65 |
+
ax.set_xticks(np.arange(len(sentiments)) * 1.5)
|
| 66 |
+
ax.set_xticklabels(sentiments, color="black", fontsize=10, rotation=45)
|
| 67 |
+
ax.tick_params(axis="y", colors="black")
|
| 68 |
+
|
| 69 |
+
# Display the plot in Streamlit
|
| 70 |
+
st.pyplot(fig)
|