Walid-Ahmed's picture
Update app.py
5e7503b verified
import gradio as gr
import yt_dlp
import whisper
import os
# Load Whisper model
model = whisper.load_model("base")
def download_audio(url):
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': '/tmp/audio.%(ext)s', # Use /tmp directory for outputs
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=True)
audio_file = ydl.prepare_filename(info_dict).replace('.webm', '.mp3').replace('.m4a', '.mp3')
return audio_file
except yt_dlp.utils.DownloadError as e:
return f"Download Error: {str(e)}"
except Exception as e:
return f"An unexpected error occurred: {str(e)}"
def transcribe_audio(audio_file):
result = model.transcribe(audio_file)
return result["text"]
def process_video(url):
try:
audio_file = download_audio(url)
if "Error" in audio_file:
return audio_file # Return error message if download failed
transcript = transcribe_audio(audio_file)
os.remove(audio_file) # Clean up the audio file
return transcript
except Exception as e:
return f"An error occurred: {str(e)}"
# Gradio Interface
iface = gr.Interface(
fn=process_video,
inputs=gr.Textbox(value="https://www.youtube.com/watch?v=RiI1NkaDXlQ", label="YouTube Video URL"),
outputs=gr.Textbox(label="Transcript"),
title="YouTube Video Transcriber",
description="Enter a YouTube video URL to get a transcript of its content."
)
if __name__ == "__main__":
iface.launch()