import streamlit as st import pandas as pd import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification import emoji model_path = "TBModel" tokenizer_path = "TBTokenizer" # Load the tokenizer and model model = AutoModelForSequenceClassification.from_pretrained(model_path) tokenizer = AutoTokenizer.from_pretrained(tokenizer_path) def predict(text): encoded_data = tokenizer.encode_plus(text, padding=True, truncation=True, return_tensors='pt') input_ids = encoded_data['input_ids'] attention_mask = encoded_data['attention_mask'] with torch.no_grad(): outputs = model(input_ids, attention_mask) logits = outputs.logits probabilities = torch.softmax(logits, dim=1) _, predicted = torch.max(probabilities, dim=1) # Create dictionary to map numerical labels to categories label_dict = {0: 'Positive', 1: 'Negative', 2: 'Neutral'} predicted_label = label_dict[predicted.item()] return predicted_label # Define examples as a list examples = [ "ChatGPT Plus uses cutting-edge AI technology to learn from customer conversations.", "ChatGPT can produce harmful and biased answers.", "Gpt dont have feelings or a personal identity, but it strive to provide informative responses.", ] # Create the Streamlit app emoji_dict = { "positive": "\U0001F60A", "negative": "\U0001F61E", "neutral": "\U0001F610" } st.title("#ChatGPT - Sentiment Analyzer") # Create the form to handle user inputs with st.form("sentiment_analysis_form"): # Add the dropdown list for examples selected_option = st.selectbox("Select an example to analyze", [""] + examples, index=0) # Add the text input for user input user_input = st.text_input("Enter your own text to analyze", "") # Define color codes for different sentiment classes positive_color = "#00C851" negative_color = "#ff4444" neutral_color = "#FFBB33" # Add the submit button to analyze the sentiment analyze_button = st.form_submit_button("Analyze") # Handle the form submission if analyze_button: if user_input.strip() != "": prediction = predict(user_input.strip()) if prediction == 'Positive': st.write(f"{emoji_dict['positive']} Positive", unsafe_allow_html=True) elif prediction == 'Negative': st.write(f"{emoji_dict['negative']} Negative", unsafe_allow_html=True) else: st.write(f"{emoji_dict['neutral']} Neutral", unsafe_allow_html=True) elif selected_option != "": prediction = predict(selected_option) if prediction == 'Positive': st.write(f"{emoji_dict['positive']} Positive", unsafe_allow_html=True) elif prediction == 'Negative': st.write(f"{emoji_dict['negative']} Negative", unsafe_allow_html=True) else: st.write(f"{emoji_dict['neutral']} Neutral", unsafe_allow_html=True) else: st.write("Please enter a text or select an example to predict")