import streamlit as st import pandas as pd from transformers import AutoTokenizer, AutoModel,AutoModelForSequenceClassification import torch num_classes = 6 #tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") 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 = """ """ 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'
' border_color = "border: 2px solid #82D853;" if sentiment == "positive" else "border: 2px solid #D85353;" card_content = f"""