project / app.py
dtkne's picture
Update app.py
566dcba verified
raw
history blame
1.96 kB
import gradio as gr
import torch
import os
import subprocess
from pytubefix import YouTube
from moviepy.editor import VideoFileClip
from transformers import pipeline
import subprocess
import sys
# Ensure moviepy is installed
try:
import moviepy.editor
except ImportError:
subprocess.run([sys.executable, "-m", "pip", "install", "moviepy"], check=True)
import moviepy.editor # Retry import after installation
# Ensure required packages are installed inside Hugging Face Spaces
subprocess.run(["pip", "install", "pytubefix", "moviepy", "transformers", "torchaudio"], check=True)
# Load Whisper model for transcription
asr = pipeline("automatic-speech-recognition", model="distil-whisper/distil-small.en")
# Load Summarization model
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
def process_youtube_link(youtube_url):
try:
# Download YouTube Video
yt = YouTube(youtube_url)
video_stream = yt.streams.filter(only_audio=True).first()
video_path = video_stream.download(filename="video.mp4")
# Extract Audio
audio_path = "audio.wav"
video = VideoFileClip(video_path)
video.audio.write_audiofile(audio_path)
# Transcribe Audio
transcription = asr(audio_path)
transcribed_text = transcription["text"]
# Summarize Transcription
summary = summarizer(transcribed_text, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
return transcribed_text, summary
except Exception as e:
return f"Error: {str(e)}", ""
# Create Gradio Interface
iface = gr.Interface(
fn=process_youtube_link,
inputs=gr.Textbox(label="Enter YouTube URL"),
outputs=[gr.Textbox(label="Transcription"), gr.Textbox(label="Summary")],
title="YouTube Video Transcriber & Summarizer",
description="Enter a YouTube link, and this app will transcribe and summarize the audio.",
)
iface.launch()