Group31 / app.py
Thea231's picture
Update app.py
f024bd3 verified
import streamlit as st
from transformers import pipeline
import numpy as np
# Load Hugging Face pipelines
emotion_classifier = pipeline("text-classification", model="bhadresh-savani/bert-base-go-emotion")
feedback_generator = pipeline("text2text-generation", model="facebook/blenderbot-3B")
text_to_audio = pipeline("text-to-speech", model="facebook/mms-tts-eng")
# Streamlit app UI
st.title("Emotion Detection and Feedback Generation")
st.markdown("""
This app detects emotions in a given comment, generates appropriate feedback, and reads it aloud.
""")
# Input text box for comments
comment_input = st.text_area("Enter your comment:", placeholder="Type your comment here...", height=200)
# Analyze button
if st.button("Analyze Comment"):
if not comment_input.strip():
st.error("Please provide a valid comment.")
else:
# Perform emotion classification
emotion_result = emotion_classifier(comment_input)[0]
emotion_label = emotion_result["label"]
emotion_score = round(emotion_result["score"], 4)
# Generate feedback based on emotion
feedback = feedback_generator(f"emotion: {emotion_label} text: {comment_input}", max_length=50)[0]["generated_text"]
# Convert feedback text to speech
st.text('Generating audio data...')
audio_data = text_to_audio(feedback)
# Display results
st.subheader("Analysis Result")
st.write(f"### **Emotion:** {emotion_label} (Confidence: {emotion_score})")
st.write(f"### **Generated Feedback:** {feedback}")
# Play button
if st.button("Play Audio"):
st.audio(audio_data['audio'],
format="audio/wav",
start_time=0,
sample_rate = audio_data['sampling_rate'])