File size: 1,431 Bytes
0ad74ed
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: blocks_speech_text_sentiment"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio torch transformers"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["from transformers import pipeline\n", "\n", "import gradio as gr\n", "\n", "asr = pipeline(\"automatic-speech-recognition\", \"facebook/wav2vec2-base-960h\")\n", "classifier = pipeline(\"text-classification\")\n", "\n", "def speech_to_text(speech):\n", "    text = asr(speech)[\"text\"]  # type: ignore\n", "    return text\n", "\n", "def text_to_sentiment(text):\n", "    return classifier(text)[0][\"label\"]  # type: ignore\n", "\n", "demo = gr.Blocks()\n", "\n", "with demo:\n", "    audio_file = gr.Audio(type=\"filepath\")\n", "    text = gr.Textbox()\n", "    label = gr.Label()\n", "\n", "    b1 = gr.Button(\"Recognize Speech\")\n", "    b2 = gr.Button(\"Classify Sentiment\")\n", "\n", "    b1.click(speech_to_text, inputs=audio_file, outputs=text)\n", "    b2.click(text_to_sentiment, inputs=text, outputs=label)\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}