Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import whisper
|
| 3 |
+
from pytube import YouTube
|
| 4 |
+
import os
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
# Load Whisper model
|
| 8 |
+
model = whisper.load_model("base")
|
| 9 |
+
|
| 10 |
+
# Load summarization model
|
| 11 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 12 |
+
|
| 13 |
+
def download_youtube_audio(url):
|
| 14 |
+
yt = YouTube(url)
|
| 15 |
+
audio = yt.streams.filter(only_audio=True).first()
|
| 16 |
+
out_file = audio.download(filename="temp_audio")
|
| 17 |
+
base, ext = os.path.splitext(out_file)
|
| 18 |
+
audio_file = base + '.mp3'
|
| 19 |
+
os.rename(out_file, audio_file)
|
| 20 |
+
return audio_file
|
| 21 |
+
|
| 22 |
+
def transcribe_and_summarize(youtube_url=None, video_file=None):
|
| 23 |
+
try:
|
| 24 |
+
# Get audio file
|
| 25 |
+
if youtube_url:
|
| 26 |
+
audio_path = download_youtube_audio(youtube_url)
|
| 27 |
+
elif video_file:
|
| 28 |
+
audio_path = video_file
|
| 29 |
+
else:
|
| 30 |
+
return "Please provide a YouTube URL or upload a video file."
|
| 31 |
+
|
| 32 |
+
# Transcribe
|
| 33 |
+
result = model.transcribe(audio_path)
|
| 34 |
+
transcription = result["text"]
|
| 35 |
+
|
| 36 |
+
# Summarize (split into chunks if too long)
|
| 37 |
+
max_chunk = 1024
|
| 38 |
+
text_chunks = [transcription[i:i+max_chunk] for i in range(0, len(transcription), max_chunk)]
|
| 39 |
+
summaries = []
|
| 40 |
+
|
| 41 |
+
for chunk in text_chunks[:3]: # Limit to first 3 chunks
|
| 42 |
+
summary = summarizer(chunk, max_length=130, min_length=30, do_sample=False)
|
| 43 |
+
summaries.append(summary[0]['summary_text'])
|
| 44 |
+
|
| 45 |
+
summary = " ".join(summaries)
|
| 46 |
+
|
| 47 |
+
# Create downloadable text file
|
| 48 |
+
output_text = f"TRANSCRIPTION:\n{'='*50}\n\n{transcription}\n\n\nSUMMARY:\n{'='*50}\n\n{summary}"
|
| 49 |
+
|
| 50 |
+
# Clean up
|
| 51 |
+
if youtube_url and os.path.exists(audio_path):
|
| 52 |
+
os.remove(audio_path)
|
| 53 |
+
|
| 54 |
+
return transcription, summary, output_text
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Error: {str(e)}", "", ""
|
| 58 |
+
|
| 59 |
+
# Create Gradio interface
|
| 60 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 61 |
+
gr.Markdown("# 🎥 Video Transcription & Summary Generator")
|
| 62 |
+
gr.Markdown("Upload a video or paste a YouTube link to get AI-powered transcription and summary")
|
| 63 |
+
|
| 64 |
+
with gr.Tab("YouTube Link"):
|
| 65 |
+
youtube_input = gr.Textbox(label="YouTube URL", placeholder="https://www.youtube.com/watch?v=...")
|
| 66 |
+
youtube_btn = gr.Button("Process YouTube Video", variant="primary")
|
| 67 |
+
|
| 68 |
+
with gr.Tab("Upload Video"):
|
| 69 |
+
video_input = gr.Video(label="Upload Video File")
|
| 70 |
+
upload_btn = gr.Button("Process Uploaded Video", variant="primary")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
with gr.Column():
|
| 74 |
+
transcription_output = gr.Textbox(label="Full Transcription", lines=10)
|
| 75 |
+
with gr.Column():
|
| 76 |
+
summary_output = gr.Textbox(label="AI Summary", lines=10)
|
| 77 |
+
|
| 78 |
+
download_output = gr.File(label="Download Transcript")
|
| 79 |
+
|
| 80 |
+
# Event handlers
|
| 81 |
+
youtube_btn.click(
|
| 82 |
+
fn=lambda url: transcribe_and_summarize(youtube_url=url),
|
| 83 |
+
inputs=youtube_input,
|
| 84 |
+
outputs=[transcription_output, summary_output, download_output]
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
upload_btn.click(
|
| 88 |
+
fn=lambda video: transcribe_and_summarize(video_file=video),
|
| 89 |
+
inputs=video_input,
|
| 90 |
+
outputs=[transcription_output, summary_output, download_output]
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
demo.launch()
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
**`requirements.txt`**:
|
| 97 |
+
```
|
| 98 |
+
gradio
|
| 99 |
+
openai-whisper
|
| 100 |
+
pytube
|
| 101 |
+
transformers
|
| 102 |
+
torch
|