Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
# Load the emotion recognition pipeline
|
6 |
+
emotion_pipeline = pipeline(model="j-hartmann/emotion-english-distilroberta-base", task="text-classification")
|
7 |
+
# Load the asr pipeline
|
8 |
+
asr = pipeline(task="automatic-speech-recognition",
|
9 |
+
model="distil-whisper/distil-small.en")
|
10 |
+
#Creating a function to detect emotion from a speech. First we recognise the speech and output as a text and then we feed that to the emotion detection pipeline.
|
11 |
+
def transcribe_speech(filepath):
|
12 |
+
if not filepath:
|
13 |
+
return "No audio found, please retry.", []
|
14 |
+
|
15 |
+
# Transcribe the speech
|
16 |
+
output = asr(filepath)
|
17 |
+
transcription = output["text"]
|
18 |
+
|
19 |
+
# Detect the emotion in the transcribed text
|
20 |
+
emotion_output = emotion_pipeline(transcription)
|
21 |
+
|
22 |
+
# Format emotion output
|
23 |
+
formatted_emotions = [
|
24 |
+
f"{emo['label']}: {emo['score']:.4f}" for emo in emotion_output
|
25 |
+
]
|
26 |
+
|
27 |
+
return transcription, formatted_emotions
|
28 |
+
# Gradio interfaces for microphone and file upload
|
29 |
+
mic_transcribe = gr.Interface(
|
30 |
+
fn=transcribe_speech,
|
31 |
+
inputs=gr.Audio(sources="microphone", type="filepath"),
|
32 |
+
outputs=[
|
33 |
+
gr.Textbox(label="Transcription", lines=3),
|
34 |
+
gr.Textbox(label="Emotions", lines=5)
|
35 |
+
],
|
36 |
+
allow_flagging="never"
|
37 |
+
)
|
38 |
+
|
39 |
+
file_transcribe = gr.Interface(
|
40 |
+
fn=transcribe_speech,
|
41 |
+
inputs=gr.Audio(sources="upload", type="filepath"),
|
42 |
+
outputs=[
|
43 |
+
gr.Textbox(label="Transcription", lines=3),
|
44 |
+
gr.Textbox(label="Emotions", lines=5)
|
45 |
+
],
|
46 |
+
allow_flagging="never"
|
47 |
+
)
|
48 |
+
# Create the demo with tabbed interfaces
|
49 |
+
demo = gr.Blocks()
|
50 |
+
|
51 |
+
with demo:
|
52 |
+
gr.TabbedInterface(
|
53 |
+
[mic_transcribe, file_transcribe],
|
54 |
+
["Transcribe Microphone", "Transcribe Audio File"],
|
55 |
+
)
|
56 |
+
|
57 |
+
# Launch the Gradio demo
|
58 |
+
if __name__ == "__main__":
|
59 |
+
demo.launch(debug=True)
|