CoolFace
Apppublic

RamAnanth1/Transcript_PDF

sourceHugging Faceupdated 4y agoView on Hugging Face
2likes
app.py169 linesDownload Raw Back to root
1import gradio as gr2import torch3import whisper4from whisper.utils import write_vtt5import requests6from pytube import YouTube7 8### ————————————————————————————————————————9 10title="Transcript PDF"11 12### ————————————————————————————————————————13 14whisper_model = whisper.load_model("medium")15 16device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")17 18def transcribe(audio):19    20    print("""2122    Sending audio to Whisper ...2324    """)25    26    #audio = whisper.load_audio(audio)27    #audio = whisper.pad_or_trim(audio)28    29    #mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device)30    31    #_, probs = whisper_model.detect_language(mel)32    33    #transcript_options = whisper.DecodingOptions(task="transcribe", fp16 = False)34    #translate_options = whisper.DecodingOptions(task="translate", fp16 = False)35    36    #transcription = whisper.decode(whisper_model, mel, transcript_options)37    #translation = whisper.decode(whisper_model, mel, translate_options)38    result = whisper_model.transcribe(audio)39    #print("Language Spoken: " + transcription.language)40    #print("Transcript: " + transcription.text)  41    #print("Translated: " + translation.text)42    with open('sub.vtt', "w") as f:43        write_vtt(result["segments"], file=f)44 45 46    return result["text"], "sub.vtt"47 48def transcribe_upload(audio):49    return transcribe(audio)50 51def transcribe_yt(link):52    yt = YouTube(link)53    path = yt.streams.filter(only_audio=True)[0].download(filename="audio.mp3")54    return transcribe(path)55 56css = """57        .gradio-container {58            font-family: 'IBM Plex Sans', sans-serif;59        }60        .gr-button {61            color: white;62            border-color: black;63            background: black;64        }65        input[type='range'] {66            accent-color: black;67        }68        .dark input[type='range'] {69            accent-color: #dfdfdf;70        }71        .container {72            max-width: 880px;73            margin: auto;74            padding-top: 1.5rem;75        }76        #gallery {77            min-height: 22rem;78            margin-bottom: 15px;79            margin-left: auto;80            margin-right: auto;81            border-bottom-right-radius: .5rem !important;82            border-bottom-left-radius: .5rem !important;83        }84        #gallery>div>.h-full {85            min-height: 20rem;86        }87        .details:hover {88            text-decoration: underline;89        }90        .gr-button {91            white-space: nowrap;92        }93        .gr-button:focus {94            border-color: rgb(147 197 253 / var(--tw-border-opacity));95            outline: none;96            box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);97            --tw-border-opacity: 1;98            --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);99            --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);100            --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));101            --tw-ring-opacity: .5;102        }103        #advanced-btn {104            font-size: .7rem !important;105            line-height: 19px;106            margin-top: 12px;107            margin-bottom: 12px;108            padding: 2px 8px;109            border-radius: 14px !important;110        }111        #advanced-options {112            display: none;113            margin-bottom: 20px;114        }115        .footer {116            margin-bottom: 45px;117            margin-top: 35px;118            text-align: center;119            border-bottom: 1px solid #e5e5e5;120        }121        .footer>p {122            font-size: .8rem;123            display: inline-block;124            padding: 0 10px;125            transform: translateY(10px);126            background: white;127        }128        .dark .footer {129            border-color: #303030;130        }131        .dark .footer>p {132            background: #0b0f19;133        }134"""135 136with gr.Blocks(css = css) as demo:137    gr.Markdown("""138                ## Transcript Generator139            """)140    gr.HTML('''141     <p style="margin-bottom: 10px">142                Save Transcripts of videos as PDF with the help of Whisper, which is a general-purpose speech recognition model released by OpenAI that can perform multilingual speech recognition as well as speech translation and language identification. 143              </p>144              ''')145    with gr.Column():146            #gr.Markdown(""" ### Record audio """)147        with gr.Tab("Youtube Link"):148            yt_input = gr.Textbox(label = 'Youtube Link')149            transcribe_audio_yt = gr.Button('Transcribe')150        151        with gr.Tab("Upload as File"):152            audio_input_u = gr.Audio(label = 'Upload Audio',source="upload",type="filepath")153            transcribe_audio_u = gr.Button('Transcribe')154 155        with gr.Row():156            transcript_output = gr.Textbox(label="Transcript", lines = 20)157            transcript_file = gr.File()158    159    transcribe_audio_yt.click(transcribe_yt, inputs = yt_input, outputs = [transcript_output, transcript_file])160    transcribe_audio_u.click(transcribe_upload, inputs = audio_input_u, outputs = [transcript_output, transcript_file])   161    gr.HTML('''162        <div class="footer">163                    <p>Whisper Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a>164                    </p>165        </div>166        ''')167        168demo.queue()    169demo.launch()