Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import whisper
|
| 2 |
+
from moviepy.editor import VideoFileClip
|
| 3 |
+
from transformers import BartForConditionalGeneration, BartTokenizer
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Load models
|
| 8 |
+
whisper_model = whisper.load_model("base")
|
| 9 |
+
tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
|
| 10 |
+
summarizer_model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
|
| 11 |
+
|
| 12 |
+
def transcribe_and_summarize(video_file):
|
| 13 |
+
try:
|
| 14 |
+
# Step 1: Process the video
|
| 15 |
+
clip = VideoFileClip(video_file)
|
| 16 |
+
audio_file = "audio.wav"
|
| 17 |
+
clip.audio.write_audiofile(audio_file)
|
| 18 |
+
|
| 19 |
+
# Step 2: Transcribe audio
|
| 20 |
+
result = whisper_model.transcribe(audio_file)
|
| 21 |
+
transcribed_text = result['text']
|
| 22 |
+
|
| 23 |
+
# Step 3: Summarize text
|
| 24 |
+
inputs = tokenizer(transcribed_text, return_tensors="pt", max_length=1024, truncation=True)
|
| 25 |
+
summary_ids = summarizer_model.generate(inputs["input_ids"], max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True)
|
| 26 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 27 |
+
|
| 28 |
+
# Clean up the audio file
|
| 29 |
+
os.remove(audio_file)
|
| 30 |
+
|
| 31 |
+
return transcribed_text, summary
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return str(e), "Error occurred during processing."
|
| 35 |
+
|
| 36 |
+
# Create Gradio interface
|
| 37 |
+
iface = gr.Interface(
|
| 38 |
+
fn=transcribe_and_summarize,
|
| 39 |
+
inputs=gr.Video(label="Upload Video"),
|
| 40 |
+
outputs=[
|
| 41 |
+
gr.Textbox(label="Transcribed Text", lines=10),
|
| 42 |
+
gr.Textbox(label="Summary", lines=5)
|
| 43 |
+
],
|
| 44 |
+
title="Video Transcription and Summarization",
|
| 45 |
+
description="Upload a video file to transcribe its audio and generate a summary of the transcribed text."
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Launch the interface
|
| 49 |
+
iface.launch()
|