import streamlit as st from transformers import pipeline from datetime import datetime # Set up Streamlit title and description st.title("Sentiment Analysis , Audio Feedback, and Response Generation") st.write("Enter a review, guess its sentiment (positive/negative), and get audio feedback on your guess and improve your Enlish.") # Load the sentiment analysis model sentiment_analysis = pipeline("text-classification", model="JACOBBBB/CustomModel_JL") # Load the text-to-speech model text_to_audio = pipeline("text-to-audio", model="vbrydik/mms-tts-eng-finetune-v3-train") # Load the text generation model response_generator = pipeline("text-generation", model="openai-community/gpt2") # Text input for user to enter the review review = st.text_area("Enter the review to analyze", "") # Dropdown for user to guess the sentiment user_guess = st.selectbox("Guess the sentiment of your review:", ("Positive", "Negative")) # Perform operations when the user clicks the "Analyze and Respond" button if st.button("Analyze and Generate"): # Perform sentiment analysis on the input review sentiment_result = sentiment_analysis(review)[0] label = sentiment_result['label'].upper() score = sentiment_result['score'] # Check user's guess against the analysis guess_is_correct = (user_guess.upper() == label) guess_feedback = "Right" if guess_is_correct else "Wrong" if guess_feedback == "Right": feedback_text = f"Congratulations! Your guess was {guess_feedback}! The actual sentiment of the review is {label}." else: feedback_text = f"Sorry! Your guess was {guess_feedback}! The actual sentiment of the review is {label}." # Generate audio feedback for the guess feedback_audio = text_to_audio(feedback_text) st.audio(feedback_audio['audio'], format='audio/wav', sample_rate=feedback_audio['sampling_rate']) # Display the sentiment analysis result and feedback st.write("Predicted Sentiment:", label) st.write("Confidence Score:", f"{score * 100:.2f}%") st.write(feedback_text) # Generate audio of the review st.write("Audio of the review for practice your listening") review_audio = text_to_audio(f"Audio of the review for practice your listening: {review}") st.audio(review_audio['audio'], format='audio/wav', sample_rate=review_audio['sampling_rate']) # Generate a professional response based on the review and its sentiment if label == "POSITIVE": prompt = "Thank you for sharing your experience and choosing our hotel. We are glad that you had a wonderful stay with us." else: prompt = "Thank you for letting us know about the issues that you experienced during your recent stay at our hotel. Sorry for any inconvenience caused." professional_response = response_generator(prompt, max_length=50, num_return_sequences=1)[0]['generated_text'] # Display the generated professional response st.write("Generated Professional Response:", professional_response) st.title('Monthly Best Response Competition') # Simple form for input using Streamlit with st.form("user_input_form"): user_input = st.text_input("Enter your response:") submit_button = st.form_submit_button("Submit") if submit_button: # Current time when the input is received current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") # Format the entry to be saved entry = f"{current_time}: {user_input}\n" # Save the input to a local file with open("monthly_competition_entries.txt", "a") as file: file.write(entry) st.success("Your response has been saved and entered into the competition!") # Display outside the form context st.write("Feel free to submit more responses. Each unique response counts as a separate entry.")