Spaces:
Runtime error
Runtime error
Add application file
Browse files- app.py +56 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
|
7 |
+
classifier = pipeline("text-classification")
|
8 |
+
|
9 |
+
def speech_to_text(mic=None, file=None):
|
10 |
+
if mic is not None:
|
11 |
+
audio = mic
|
12 |
+
elif file is not None:
|
13 |
+
audio = file
|
14 |
+
else:
|
15 |
+
return "You must either provide a mic recording or a file"
|
16 |
+
text = asr(audio)["text"]
|
17 |
+
return text
|
18 |
+
|
19 |
+
|
20 |
+
def text_to_sentiment(text):
|
21 |
+
return classifier(text)[0]["label"]
|
22 |
+
|
23 |
+
title = "Speech-Text-Sentiment"
|
24 |
+
description = """
|
25 |
+
Task: Speech to Text to Sentiment\n
|
26 |
+
Model: \n
|
27 |
+
speech to text (Wav2Vec2ForCTC)\n
|
28 |
+
text to sentiment (DistilBertForSequenceClassification)\n
|
29 |
+
"""
|
30 |
+
theme="freddyaboulton/dracula_revamped"
|
31 |
+
|
32 |
+
demo = gr.Blocks(
|
33 |
+
title=title,
|
34 |
+
description=description,
|
35 |
+
theme=theme
|
36 |
+
)
|
37 |
+
|
38 |
+
with demo:
|
39 |
+
audio_file = [
|
40 |
+
gr.Audio(source="microphone",
|
41 |
+
type="filepath",
|
42 |
+
optional=True),
|
43 |
+
gr.Audio(source="upload",
|
44 |
+
type="filepath",
|
45 |
+
optional=True),
|
46 |
+
]
|
47 |
+
text = gr.Textbox()
|
48 |
+
label = gr.Label()
|
49 |
+
|
50 |
+
b1 = gr.Button("Recognize Speech")
|
51 |
+
b2 = gr.Button("Classify Sentiment")
|
52 |
+
|
53 |
+
b1.click(speech_to_text, inputs=audio_file, outputs=text)
|
54 |
+
b2.click(text_to_sentiment, inputs=text, outputs=label)
|
55 |
+
|
56 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.36.1
|
2 |
+
transformers==4.30.2
|
3 |
+
torch==2.0.1
|