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