|
|
|
import streamlit as st |
|
import pandas as pd |
|
|
|
from transformers import AutoTokenizer, AutoModel,AutoModelForSequenceClassification |
|
import torch |
|
|
|
|
|
|
|
num_classes = 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("KhaldiAbderrhmane/bert-emotion",trust_remote_code=True) |
|
|
|
|
|
|
|
model = AutoModelForSequenceClassification.from_pretrained("KhaldiAbderrhmane/bert-emotion",trust_remote_code=True) |
|
|
|
|
|
|
|
def prediction_sentiment(review): |
|
|
|
|
|
t= tokenizer(review, truncation=True, padding=True, max_length=128, return_tensors='pt') |
|
inpt = t['input_ids'] |
|
mask = t['attention_mask'] |
|
|
|
|
|
|
|
|
|
outputs = model(inpt,mask) |
|
|
|
|
|
outputs = outputs.logits |
|
|
|
|
|
|
|
predicted= torch.max(outputs, 1).indices |
|
|
|
if predicted == 0: |
|
sentiment = "Sadness" |
|
elif predicted == 1: |
|
sentiment = "Joy" |
|
elif predicted == 2: |
|
sentiment = "Love" |
|
elif predicted == 3: |
|
sentiment = "Anger" |
|
elif predicted == 4: |
|
sentiment = "Fear" |
|
else: |
|
sentiment = "Surprise" |
|
return sentiment |
|
|
|
|
|
|
|
|
|
|
|
users = {"abdelmalek": [["this movie was so nice", "positive"], ["what the hell was that", "negative"], ["man this was good", "positive"]]} |
|
columns = ["comment", "sentiment"] |
|
user_name = st.text_input("User Name") |
|
|
|
if user_name: |
|
if user_name in users: |
|
user_input = st.text_input("Enter your comment:") |
|
|
|
|
|
if user_input: |
|
|
|
sentiment = prediction_sentiment(user_input) |
|
st.write('Your sentiment is:', sentiment) |
|
users[user_name].append([user_input, sentiment]) |
|
else: |
|
users[user_name] = [] |
|
st.write("Your user name has been added.") |
|
user_input = st.text_input("Enter your comment:") |
|
if user_input: |
|
sentiment = prediction_sentiment(user_input) |
|
st.write('Your sentiment is:', sentiment) |
|
users[user_name].append([user_input, sentiment]) |
|
|
|
if st.button("Your comment:"): |
|
if user_name in users: |
|
df_t = pd.DataFrame(users[user_name], columns=columns) |
|
card_css = """ |
|
<style> |
|
.card { |
|
background-color: #1A2E4D; |
|
border-radius: 10px; |
|
padding: 20px; |
|
margin: 10px 0; |
|
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); |
|
} |
|
.card-title { |
|
font-size: 24px; |
|
font-weight: bold; |
|
color: #F4F6FF; |
|
} |
|
.card-content { |
|
font-size: 18px; |
|
color: #52709E; |
|
} |
|
.sentiment-circle { |
|
width: 15px; /* Adjust size as needed */ |
|
height: 15px; /* Adjust size as needed */ |
|
border-radius: 50%; |
|
display: inline-block; |
|
position: absolute; |
|
top: 50%; |
|
right: 0; |
|
transform: translateY(-50%); |
|
margin-right: 10px; /* Space between circle and text */ |
|
} |
|
.positive { |
|
|
|
background-color: #82D853; /* Green background for positive sentiment */ |
|
} |
|
.negative { |
|
|
|
background-color: #D85353; /* Red background for negative sentiment */ |
|
} |
|
</style> |
|
""" |
|
st.markdown(card_css, unsafe_allow_html=True) |
|
|
|
for comment, sentiment in df_t.values: |
|
sentiment_class = "positive" if sentiment == "positive" else "negative" |
|
sentiment_circle = f'<div class="sentiment-circle {sentiment_class}" style="background-color: {"#82D853" if sentiment == "positive" else "#D85353"};"></div>' |
|
border_color = "border: 2px solid #82D853;" if sentiment == "positive" else "border: 2px solid #D85353;" |
|
card_content = f""" |
|
<div class="card" style="{border_color}"> |
|
<div class="card-title">{user_name}</div> |
|
<div class="card-content"> |
|
{comment} |
|
{sentiment_circle} |
|
</div> |
|
</div> |
|
""" |
|
st.markdown(card_content, unsafe_allow_html=True) |
|
else: |
|
st.error("No history available for this user.") |
|
|