2022-05-14 13:45:44 +08:00
|
|
|
from transformers import pipeline
|
2022-03-29 03:59:30 +08:00
|
|
|
|
|
|
|
import gradio as gr
|
|
|
|
|
2022-05-14 13:45:44 +08:00
|
|
|
asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
|
|
|
|
classifier = pipeline("text-classification")
|
2022-03-29 03:59:30 +08:00
|
|
|
|
2022-05-14 13:45:44 +08:00
|
|
|
def speech_to_text(speech):
|
2024-07-20 09:34:34 +08:00
|
|
|
text = asr(speech)["text"] # type: ignore
|
2022-05-14 13:45:44 +08:00
|
|
|
return text
|
2022-03-29 03:59:30 +08:00
|
|
|
|
2022-05-14 13:45:44 +08:00
|
|
|
def text_to_sentiment(text):
|
2024-07-20 09:34:34 +08:00
|
|
|
return classifier(text)[0]["label"] # type: ignore
|
2022-03-29 03:59:30 +08:00
|
|
|
|
|
|
|
demo = gr.Blocks()
|
|
|
|
|
|
|
|
with demo:
|
2022-05-14 10:48:46 +08:00
|
|
|
audio_file = gr.Audio(type="filepath")
|
|
|
|
text = gr.Textbox()
|
|
|
|
label = gr.Label()
|
2022-04-05 06:47:51 +08:00
|
|
|
|
2022-03-29 03:59:30 +08:00
|
|
|
b1 = gr.Button("Recognize Speech")
|
|
|
|
b2 = gr.Button("Classify Sentiment")
|
2022-04-05 06:47:51 +08:00
|
|
|
|
2022-05-14 13:45:44 +08:00
|
|
|
b1.click(speech_to_text, inputs=audio_file, outputs=text)
|
|
|
|
b2.click(text_to_sentiment, inputs=text, outputs=label)
|
2022-03-29 03:59:30 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-04-05 06:47:51 +08:00
|
|
|
demo.launch()
|