Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
from scipy.io.wavfile import write
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Load Hugging Face pipelines
|
| 8 |
+
emotion_classifier = pipeline("text-classification", model="bhadresh-savani/bert-base-go-emotion")
|
| 9 |
+
feedback_generator = pipeline("text2text-generation", model="mrm8488/t5-small-finetuned-emotion")
|
| 10 |
+
text_to_audio = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 11 |
+
|
| 12 |
+
# Streamlit app UI
|
| 13 |
+
st.title("Emotion Detection and Feedback Generation")
|
| 14 |
+
st.markdown("""
|
| 15 |
+
This app detects emotions in a given comment, generates appropriate feedback, and reads it aloud.
|
| 16 |
+
""")
|
| 17 |
+
|
| 18 |
+
# Input text box for comments
|
| 19 |
+
comment_input = st.text_area("Enter your comment:", placeholder="Type your comment here...", height=200)
|
| 20 |
+
|
| 21 |
+
# Analyze button
|
| 22 |
+
if st.button("Analyze Comment"):
|
| 23 |
+
if not comment_input.strip():
|
| 24 |
+
st.error("Please provide a valid comment.")
|
| 25 |
+
else:
|
| 26 |
+
# Perform emotion classification
|
| 27 |
+
emotion_result = emotion_classifier(comment_input)[0]
|
| 28 |
+
emotion_label = emotion_result["label"]
|
| 29 |
+
emotion_score = round(emotion_result["score"], 4)
|
| 30 |
+
|
| 31 |
+
# Generate feedback based on emotion
|
| 32 |
+
feedback = feedback_generator(f"emotion: {emotion_label} text: {comment_input}", max_length=50)[0]["generated_text"]
|
| 33 |
+
|
| 34 |
+
# Convert feedback text to speech
|
| 35 |
+
audio_output = text_to_audio(feedback)
|
| 36 |
+
audio_array = np.array(audio_output["audio"]) * 32767 # Convert to int16 range
|
| 37 |
+
audio_path = "feedback_audio.wav"
|
| 38 |
+
write(audio_path, 22050, audio_array.astype(np.int16))
|
| 39 |
+
|
| 40 |
+
# Display results
|
| 41 |
+
st.subheader("Analysis Result")
|
| 42 |
+
st.write(f"### **Emotion:** {emotion_label} (Confidence: {emotion_score})")
|
| 43 |
+
st.write(f"### **Generated Feedback:** {feedback}")
|
| 44 |
+
|
| 45 |
+
# Audio output
|
| 46 |
+
st.audio(audio_path, format="audio/wav")
|