File size: 1,826 Bytes
abbd335
 
 
 
 
 
bbfb109
abbd335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f024bd3
 
 
abbd335
 
 
 
 
 
f024bd3
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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'])