import gradio as gr from pytube import YouTube from moviepy.editor import AudioFileClip from deepgram import DeepgramClient, PrerecordedOptions, FileSource def download_video(video_url, path='./'): yt = YouTube(video_url) video = yt.streams.filter(only_audio=True).first() out_file = video.download(output_path=path) return out_file def extract_audio(video_path): audio_path = video_path.replace(".mp4", ".wav") clip = AudioFileClip(video_path) clip.write_audiofile(audio_path) return audio_path def transcribe_audio(audio_path, api_key): deepgram = DeepgramClient(api_key) with open(audio_path, 'rb') as file: audio_data = file.read() options = PrerecordedOptions( model="latest", punctuate=True, diarize=True ) response = deepgram.transcription.prerecorded(FileSource(file=audio_data), options) transcripts = response['results']['channels'][0]['alternatives'][0]['transcript'] return transcripts def process_video(video_url, api_key): video_path = download_video(video_url) audio_path = extract_audio(video_path) transcription = transcribe_audio(audio_path, api_key) return transcription # Define Gradio interface iface = gr.Interface( fn=process_video, inputs=["text", "text"], # Accept video URL and API key as inputs outputs="text", examples=[["https://www.youtube.com/watch?v=jwjjWHzEaJc", "Deepgram API Key"]], title="Video to Text Transcription", description="Enter a YouTube video URL and your Deepgram API key to extract audio and transcribe it." ) if __name__ == "__main__": iface.launch()