{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "kPCLdTfJyktF" }, "outputs": [], "source": [ "import torch\n", "\n", "import gradio as gr\n", "import pytube as pt\n", "from transformers import pipeline\n", "\n", "asr = pipeline(\n", " task=\"automatic-speech-recognition\",\n", " model=\"Yasaman/whisper_fa\",\n", " chunk_length_s=30,\n", " device=\"cpu\",\n", ")\n", "\n", "summarizer = pipeline(\n", " \"summarization\",\n", " model=\"alireza7/PEGASUS-persian-base-PN-summary\",\n", ")\n", "\n", "translator = pipeline(\n", " \"translation\", \n", " model=\"Helsinki-NLP/opus-mt-iir-en\")\n", "\n", "def transcribe(microphone, file_upload):\n", " warn_output = \"\"\n", " if (microphone is not None) and (file_upload is not None):\n", " warn_output = (\n", " \"WARNING: You've uploaded an audio file and used the microphone. \"\n", " \"The recorded file from the microphone will be used and the uploaded audio will be discarded.\\n\"\n", " )\n", "\n", " elif (microphone is None) and (file_upload is None):\n", " return \"ERROR: You have to either use the microphone or upload an audio file\"\n", "\n", " file = microphone if microphone is not None else file_upload\n", "\n", " text = asr(file)[\"text\"]\n", "\n", " translate = translator(text)\n", " translate = translate[0][\"translation_text\"]\n", "\n", " return warn_output + text, translate\n", "\n", "def _return_yt_html_embed(yt_url):\n", " video_id = yt_url.split(\"?v=\")[-1]\n", " HTML_str = (\n", " f'
'\n", " \"
\"\n", " )\n", " return HTML_str\n", "\n", "\n", "def yt_transcribe(yt_url):\n", " yt = pt.YouTube(yt_url)\n", " html_embed_str = _return_yt_html_embed(yt_url)\n", " stream = yt.streams.filter(only_audio=True)[0]\n", " stream.download(filename=\"audio.mp3\")\n", "\n", " text = asr(\"audio.mp3\")[\"text\"]\n", "\n", " summary = summarizer(text)\n", " summary = summary[0][\"summary_text\"]\n", " \n", " translate = translator(summary)\n", " translate = translate[0][\"translation_text\"]\n", "\n", " return html_embed_str, text, summary, translate\n", "\n", "demo = gr.Blocks()\n", "\n", "mf_transcribe = gr.Interface(\n", " fn=transcribe,\n", " inputs=[\n", " gr.inputs.Audio(source=\"microphone\", type=\"filepath\", optional=True),\n", " gr.inputs.Audio(source=\"upload\", type=\"filepath\", optional=True),\n", " ],\n", " outputs=[\n", " gr.Textbox(label=\"Transcribed text\"),\n", " gr.Textbox(label=\"Translated text\"),\n", " ],\n", " layout=\"horizontal\",\n", " theme=\"huggingface\",\n", " title=\"Whisper Demo: Transcribe and Translate Persian Audio\",\n", " description=(\n", " \"Transcribe and Translate long-form microphone or audio inputs with the click of a button! Demo uses the the fine-tuned\"\n", " f\" [Yasaman/whisper_fa](https://huggingface.co/Yasaman/whisper_fa) and 🤗 Transformers to transcribe audio files\"\n", " \" of arbitrary length. It also uses another model for the translation.\"\n", " ),\n", " allow_flagging=\"never\",\n", ")\n", "\n", "yt_transcribe = gr.Interface(\n", " fn=yt_transcribe,\n", " inputs=[gr.inputs.Textbox(lines=1, placeholder=\"Paste the URL to a YouTube video here\", label=\"YouTube URL\")],\n", " outputs=[\"html\",\n", " gr.Textbox(label=\"Transcribed text\"),\n", " gr.Textbox(label=\"Summarized text\"),\n", " gr.Textbox(label=\"Translated text\"),\n", " ],\n", " layout=\"horizontal\",\n", " theme=\"huggingface\",\n", " title=\"Whisper Demo: Transcribe, Summarize and Translate YouTube\",\n", " description=(\n", " \"Transcribe, Summarize and Translate long-form YouTube videos with the click of a button! Demo uses the the fine-tuned \"\n", " f\" [Yasaman/whisper_fa](https://huggingface.co/Yasaman/whisper_fa) and 🤗 Transformers to transcribe audio files of\"\n", " \" arbitrary length. It also uses other two models to first summarize and then translate the text input. You can try with the following example: \" \n", " f\" [Video1](https://www.youtube.com/watch?v=qtRzP3KvQZk)\"\n", " ),\n", " allow_flagging=\"never\",\n", ")\n", "\n", "with demo:\n", " gr.TabbedInterface([mf_transcribe, yt_transcribe], [\"Transcribe and Translate Audio\", \"Transcribe, Summarize and Translate YouTube\"])\n", "\n", "demo.launch(enable_queue=True)" ] } ] }