Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,75 +3,91 @@ from transformers import pipeline
|
|
| 3 |
import tempfile
|
| 4 |
import torch
|
| 5 |
import os
|
|
|
|
| 6 |
from moviepy.editor import VideoFileClip
|
| 7 |
import srt
|
| 8 |
import datetime
|
| 9 |
|
| 10 |
-
#
|
| 11 |
device = 0 if torch.cuda.is_available() else -1
|
|
|
|
|
|
|
| 12 |
whisper = pipeline("automatic-speech-recognition", model="openai/whisper-large", device=device)
|
| 13 |
punctuate = pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
|
| 14 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
def extract_audio(video_path):
|
| 18 |
video = VideoFileClip(video_path)
|
| 19 |
audio_path = tempfile.mktemp(suffix=".wav")
|
| 20 |
video.audio.write_audiofile(audio_path, verbose=False, logger=None)
|
| 21 |
return audio_path
|
| 22 |
|
| 23 |
-
#
|
| 24 |
def generate_srt(transcript_text):
|
| 25 |
lines = transcript_text.strip().split(". ")
|
| 26 |
subs = []
|
| 27 |
for i, line in enumerate(lines):
|
| 28 |
-
start = datetime.timedelta(seconds=i*2)
|
| 29 |
-
end = datetime.timedelta(seconds=(i+1)*2)
|
| 30 |
-
subs.append(srt.Subtitle(index=i+1, start=start, end=end, content=line.strip()))
|
| 31 |
srt_data = srt.compose(subs)
|
| 32 |
srt_path = tempfile.mktemp(suffix=".srt")
|
| 33 |
with open(srt_path, "w") as f:
|
| 34 |
f.write(srt_data)
|
| 35 |
return srt_path
|
| 36 |
|
| 37 |
-
#
|
| 38 |
def transcribe_pipeline(video_file):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
video_path =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
raw_text = result["text"]
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
srt_path = generate_srt(punctuated)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
punct_txt_path = tempfile.mktemp(suffix=".txt")
|
| 54 |
-
summary_txt_path = tempfile.mktemp(suffix=".txt")
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
with open(summary_txt_path, "w") as f:
|
| 61 |
-
f.write(summary)
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# Gradio UI
|
| 66 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
| 67 |
gr.Markdown("# 🎥 EchoScribe: Smart Video Transcriber")
|
| 68 |
-
gr.Markdown("Upload a video to extract transcript
|
| 69 |
|
| 70 |
with gr.Row():
|
| 71 |
video_input = gr.Video(label="🎬 Upload your video")
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
-
raw_output = gr.Textbox(label="🧾 Raw Transcript
|
| 75 |
punct_output = gr.Textbox(label="📄 Punctuated Transcript", lines=6)
|
| 76 |
|
| 77 |
summary_output = gr.Textbox(label="📝 Summary", lines=4)
|
|
@@ -83,14 +99,20 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 83 |
|
| 84 |
submit_btn = gr.Button("🚀 Transcribe & Summarize")
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
gr.Markdown("---")
|
| 94 |
-
gr.Markdown("
|
| 95 |
|
| 96 |
-
iface.launch()
|
|
|
|
| 3 |
import tempfile
|
| 4 |
import torch
|
| 5 |
import os
|
| 6 |
+
import shutil
|
| 7 |
from moviepy.editor import VideoFileClip
|
| 8 |
import srt
|
| 9 |
import datetime
|
| 10 |
|
| 11 |
+
# Select CPU or GPU
|
| 12 |
device = 0 if torch.cuda.is_available() else -1
|
| 13 |
+
|
| 14 |
+
# Load Hugging Face pipelines
|
| 15 |
whisper = pipeline("automatic-speech-recognition", model="openai/whisper-large", device=device)
|
| 16 |
punctuate = pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
|
| 17 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 18 |
|
| 19 |
+
# Extract audio from uploaded video file
|
| 20 |
def extract_audio(video_path):
|
| 21 |
video = VideoFileClip(video_path)
|
| 22 |
audio_path = tempfile.mktemp(suffix=".wav")
|
| 23 |
video.audio.write_audiofile(audio_path, verbose=False, logger=None)
|
| 24 |
return audio_path
|
| 25 |
|
| 26 |
+
# Generate basic subtitle file
|
| 27 |
def generate_srt(transcript_text):
|
| 28 |
lines = transcript_text.strip().split(". ")
|
| 29 |
subs = []
|
| 30 |
for i, line in enumerate(lines):
|
| 31 |
+
start = datetime.timedelta(seconds=i * 2)
|
| 32 |
+
end = datetime.timedelta(seconds=(i + 1) * 2)
|
| 33 |
+
subs.append(srt.Subtitle(index=i + 1, start=start, end=end, content=line.strip()))
|
| 34 |
srt_data = srt.compose(subs)
|
| 35 |
srt_path = tempfile.mktemp(suffix=".srt")
|
| 36 |
with open(srt_path, "w") as f:
|
| 37 |
f.write(srt_data)
|
| 38 |
return srt_path
|
| 39 |
|
| 40 |
+
# Main pipeline
|
| 41 |
def transcribe_pipeline(video_file):
|
| 42 |
+
try:
|
| 43 |
+
# Copy the uploaded file path to a temp location
|
| 44 |
+
video_path = tempfile.mktemp(suffix=".mp4")
|
| 45 |
+
shutil.copy(video_file, video_path)
|
| 46 |
+
|
| 47 |
+
# Extract audio from video
|
| 48 |
+
audio_path = extract_audio(video_path)
|
| 49 |
+
|
| 50 |
+
# Transcribe with Whisper
|
| 51 |
+
result = whisper(audio_path)
|
| 52 |
+
raw_text = result["text"]
|
| 53 |
|
| 54 |
+
# Add punctuation
|
| 55 |
+
punctuated = punctuate(raw_text)[0]["generated_text"]
|
|
|
|
| 56 |
|
| 57 |
+
# Summarize
|
| 58 |
+
summary = summarizer(punctuated, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
|
|
|
|
| 59 |
|
| 60 |
+
# Generate subtitle file
|
| 61 |
+
srt_path = generate_srt(punctuated)
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# Save files for download
|
| 64 |
+
raw_txt_path = tempfile.mktemp(suffix=".txt")
|
| 65 |
+
punct_txt_path = tempfile.mktemp(suffix=".txt")
|
| 66 |
+
summary_txt_path = tempfile.mktemp(suffix=".txt")
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
with open(raw_txt_path, "w") as f:
|
| 69 |
+
f.write(raw_text)
|
| 70 |
+
with open(punct_txt_path, "w") as f:
|
| 71 |
+
f.write(punctuated)
|
| 72 |
+
with open(summary_txt_path, "w") as f:
|
| 73 |
+
f.write(summary)
|
| 74 |
+
|
| 75 |
+
return raw_text, punctuated, summary, punct_txt_path, summary_txt_path, srt_path
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
print("❌ Pipeline Error:", e)
|
| 79 |
+
return "Error", "Error", "Error", None, None, None
|
| 80 |
|
| 81 |
# Gradio UI
|
| 82 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
| 83 |
gr.Markdown("# 🎥 EchoScribe: Smart Video Transcriber")
|
| 84 |
+
gr.Markdown("Upload a video to extract transcript, add punctuation, and generate a summary. You can also download the .srt subtitle file.")
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
video_input = gr.Video(label="🎬 Upload your video")
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
+
raw_output = gr.Textbox(label="🧾 Raw Transcript", lines=6)
|
| 91 |
punct_output = gr.Textbox(label="📄 Punctuated Transcript", lines=6)
|
| 92 |
|
| 93 |
summary_output = gr.Textbox(label="📝 Summary", lines=4)
|
|
|
|
| 99 |
|
| 100 |
submit_btn = gr.Button("🚀 Transcribe & Summarize")
|
| 101 |
|
| 102 |
+
submit_btn.click(
|
| 103 |
+
fn=transcribe_pipeline,
|
| 104 |
+
inputs=video_input,
|
| 105 |
+
outputs=[
|
| 106 |
+
raw_output,
|
| 107 |
+
punct_output,
|
| 108 |
+
summary_output,
|
| 109 |
+
download_transcript,
|
| 110 |
+
download_summary,
|
| 111 |
+
download_srt,
|
| 112 |
+
],
|
| 113 |
+
)
|
| 114 |
|
| 115 |
gr.Markdown("---")
|
| 116 |
+
gr.Markdown("Built with ❤️ by Snigdha’s AI Lab")
|
| 117 |
|
| 118 |
+
iface.launch()
|