Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,110 +2,74 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
"""
|
| 7 |
try:
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Load Whisper for speech recognition
|
| 12 |
-
transcriber = pipeline(
|
| 13 |
"automatic-speech-recognition",
|
| 14 |
-
model="openai/whisper-tiny",
|
| 15 |
-
|
| 16 |
)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
"text-classification",
|
| 21 |
-
model="
|
| 22 |
-
|
| 23 |
)
|
| 24 |
|
| 25 |
-
return
|
| 26 |
except Exception as e:
|
| 27 |
-
print(f"
|
| 28 |
return None, None
|
| 29 |
|
| 30 |
-
def
|
| 31 |
-
"""
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return "Please provide audio", "No audio detected"
|
| 36 |
-
|
| 37 |
try:
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
if not text.strip():
|
| 48 |
-
return "No speech detected", "Empty transcription"
|
| 49 |
-
print(f"Transcribed text: {text}") # Debug output
|
| 50 |
-
except Exception as e:
|
| 51 |
-
return f"Transcription error: {str(e)}", "Failed to process audio"
|
| 52 |
|
|
|
|
|
|
|
| 53 |
# Analyze emotion
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
emotion_mapping = {
|
| 61 |
-
"Joy": "Happy/Joyful",
|
| 62 |
-
"Sadness": "Sad/Melancholic",
|
| 63 |
-
"Anger": "Angry/Frustrated",
|
| 64 |
-
"Fear": "Anxious/Fearful",
|
| 65 |
-
"Surprise": "Surprised/Astonished",
|
| 66 |
-
"Love": "Warm/Affectionate",
|
| 67 |
-
"Neutral": "Neutral/Calm"
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
display_emotion = emotion_mapping.get(emotion, emotion)
|
| 71 |
-
return display_emotion, confidence
|
| 72 |
-
|
| 73 |
-
except Exception as e:
|
| 74 |
-
return f"Emotion analysis error: {str(e)}", "Analysis failed"
|
| 75 |
-
|
| 76 |
except Exception as e:
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
# Create interface
|
| 80 |
interface = gr.Interface(
|
| 81 |
-
fn=
|
| 82 |
-
inputs=
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
outputs=[
|
| 88 |
-
gr.Textbox(label="
|
| 89 |
-
gr.Textbox(label="Confidence
|
| 90 |
],
|
| 91 |
title="Speech Emotion Analyzer",
|
| 92 |
-
description=""
|
| 93 |
-
This tool analyzes the emotional tone of speech, detecting emotions like:
|
| 94 |
-
- Happy/Joyful
|
| 95 |
-
- Sad/Melancholic
|
| 96 |
-
- Angry/Frustrated
|
| 97 |
-
- Anxious/Fearful
|
| 98 |
-
- Surprised/Astonished
|
| 99 |
-
- Warm/Affectionate
|
| 100 |
-
- Neutral/Calm
|
| 101 |
-
""",
|
| 102 |
-
theme=gr.themes.Base()
|
| 103 |
)
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
| 106 |
-
interface.launch(
|
| 107 |
-
debug=True,
|
| 108 |
-
server_name="0.0.0.0",
|
| 109 |
-
server_port=7860,
|
| 110 |
-
share=True
|
| 111 |
-
)
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
def create_analyzers():
|
| 6 |
+
"""Initialize speech and emotion analyzers"""
|
| 7 |
try:
|
| 8 |
+
# Use tiny whisper model for speed and reliability
|
| 9 |
+
speech_recognizer = pipeline(
|
|
|
|
|
|
|
|
|
|
| 10 |
"automatic-speech-recognition",
|
| 11 |
+
model="openai/whisper-tiny.en",
|
| 12 |
+
chunk_length_s=30
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# Use smaller emotion classifier
|
| 16 |
+
emotion_classifier = pipeline(
|
| 17 |
"text-classification",
|
| 18 |
+
model="SamLowe/roberta-base-go_emotions",
|
| 19 |
+
top_k=1
|
| 20 |
)
|
| 21 |
|
| 22 |
+
return speech_recognizer, emotion_classifier
|
| 23 |
except Exception as e:
|
| 24 |
+
print(f"Model loading error: {e}")
|
| 25 |
return None, None
|
| 26 |
|
| 27 |
+
def analyze_tone(audio_file):
|
| 28 |
+
"""Analyze the emotional tone of speech"""
|
| 29 |
+
if audio_file is None:
|
| 30 |
+
return "No input", "N/A"
|
| 31 |
+
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
+
# Get models
|
| 34 |
+
speech_recognizer, emotion_classifier = create_analyzers()
|
| 35 |
+
|
| 36 |
+
# Transcribe audio
|
| 37 |
+
transcription = speech_recognizer(audio_file)
|
| 38 |
+
text = transcription["text"]
|
| 39 |
+
|
| 40 |
+
if not text.strip():
|
| 41 |
+
return "No speech detected", "N/A"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
print(f"Transcribed text: {text}") # For debugging
|
| 44 |
+
|
| 45 |
# Analyze emotion
|
| 46 |
+
result = emotion_classifier(text)[0][0]
|
| 47 |
+
emotion = result['label'].replace('_', ' ').title()
|
| 48 |
+
confidence = f"{result['score']:.1%}"
|
| 49 |
+
|
| 50 |
+
return emotion, confidence
|
| 51 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
+
print(f"Analysis error: {e}")
|
| 54 |
+
return f"Error: {str(e)}", "N/A"
|
| 55 |
|
| 56 |
+
# Create minimal interface
|
| 57 |
interface = gr.Interface(
|
| 58 |
+
fn=analyze_tone,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.Audio(
|
| 61 |
+
sources=["microphone", "upload"],
|
| 62 |
+
type="filepath",
|
| 63 |
+
label="Audio Input"
|
| 64 |
+
)
|
| 65 |
+
],
|
| 66 |
outputs=[
|
| 67 |
+
gr.Textbox(label="Emotion"),
|
| 68 |
+
gr.Textbox(label="Confidence")
|
| 69 |
],
|
| 70 |
title="Speech Emotion Analyzer",
|
| 71 |
+
description="Record or upload audio to detect the emotional tone.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
interface.launch(server_name="0.0.0.0", share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|