import whisper
import yt_dlp
import gradio as gr
import os
import re
import logging

logging.basicConfig(level=logging.INFO)
model = whisper.load_model("medium")
def get_text(url):
    #try:
    if url != '':
        output_text_transcribe = ''

    with yt_dlp.YoutubeDL({'format': 'bestaudio', 'audio-format': 'wav', 'outtmpl': '%(id)s.%(ext)s'}) as ydl:
        # Extract information from the given YouTube URL and download the best audio format available
        info_dict = ydl.extract_info(url, download=True)
        # Prepare the filename of the downloaded audio file
        audio_file = ydl.prepare_filename(info_dict)    #finally:
    #    raise gr.Error("Exception: There was a problem transcribing the audio.")
        result = model.transcribe(audio_file, task="transcribe")
        return result['text'].strip()

with gr.Blocks() as demo:
    gr.Markdown("<h1><center>YouTube Video-to-Text using Whisper</center></h1>")
    gr.Markdown("<center>Enter the link of any YouTube video to generate a text transcript of the video.</center>")
    
    input_text_url = gr.Textbox(placeholder='Youtube video URL', label='YouTube URL')
    result_button_transcribe = gr.Button('Transcribe')
    output_text_transcribe = gr.Textbox(placeholder='Transcript of the YouTube video.', label='Transcript')
    
    result_button_transcribe.click(get_text, inputs = input_text_url, outputs = output_text_transcribe)
demo.launch()