Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import tempfile
|
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
import ffmpeg
|
|
|
|
| 7 |
|
| 8 |
# Cache the model with CPU optimization
|
| 9 |
def load_model():
|
|
@@ -29,22 +30,31 @@ def extract_audio(video_path):
|
|
| 29 |
)
|
| 30 |
return audio_path
|
| 31 |
|
| 32 |
-
def transcribe_video(
|
| 33 |
"""Process video and return transcript"""
|
| 34 |
start_time = time.time()
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Get file size
|
| 37 |
-
file_size = os.path.getsize(
|
| 38 |
|
| 39 |
# Extract audio
|
| 40 |
-
audio_path = extract_audio(
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Transcribe
|
| 43 |
result = model(audio_path)
|
| 44 |
transcript = result["text"]
|
| 45 |
|
| 46 |
-
# Clean up
|
| 47 |
os.unlink(audio_path)
|
|
|
|
| 48 |
process_time = time.time() - start_time
|
| 49 |
|
| 50 |
return transcript, f"✅ Processed {file_size:.1f}MB video in {process_time:.1f} seconds"
|
|
@@ -56,7 +66,7 @@ with gr.Blocks(title="Free Video Transcriber", theme=gr.themes.Soft()) as demo:
|
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
with gr.Column():
|
| 59 |
-
video_input = gr.
|
| 60 |
transcribe_btn = gr.Button("Transcribe Video", variant="primary")
|
| 61 |
|
| 62 |
with gr.Column():
|
|
@@ -65,11 +75,15 @@ with gr.Blocks(title="Free Video Transcriber", theme=gr.themes.Soft()) as demo:
|
|
| 65 |
download_btn = gr.DownloadButton(label="Download Transcript")
|
| 66 |
|
| 67 |
# Processing function
|
| 68 |
-
def process_video(
|
| 69 |
-
if
|
| 70 |
return "", "Please upload a video file first", None
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return transcript, status, transcript
|
| 74 |
|
| 75 |
# Set up button actions
|
|
|
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
import ffmpeg
|
| 7 |
+
import numpy as np
|
| 8 |
|
| 9 |
# Cache the model with CPU optimization
|
| 10 |
def load_model():
|
|
|
|
| 30 |
)
|
| 31 |
return audio_path
|
| 32 |
|
| 33 |
+
def transcribe_video(video_file):
|
| 34 |
"""Process video and return transcript"""
|
| 35 |
start_time = time.time()
|
| 36 |
|
| 37 |
+
# Save video to temp file
|
| 38 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_video:
|
| 39 |
+
tmp_video.write(video_file)
|
| 40 |
+
video_path = tmp_video.name
|
| 41 |
+
|
| 42 |
# Get file size
|
| 43 |
+
file_size = os.path.getsize(video_path) / (1024 * 1024) # in MB
|
| 44 |
|
| 45 |
# Extract audio
|
| 46 |
+
audio_path = extract_audio(video_path)
|
| 47 |
+
|
| 48 |
+
# Clean up video file
|
| 49 |
+
os.unlink(video_path)
|
| 50 |
|
| 51 |
# Transcribe
|
| 52 |
result = model(audio_path)
|
| 53 |
transcript = result["text"]
|
| 54 |
|
| 55 |
+
# Clean up audio file
|
| 56 |
os.unlink(audio_path)
|
| 57 |
+
|
| 58 |
process_time = time.time() - start_time
|
| 59 |
|
| 60 |
return transcript, f"✅ Processed {file_size:.1f}MB video in {process_time:.1f} seconds"
|
|
|
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
with gr.Column():
|
| 69 |
+
video_input = gr.File(label="Upload Video", file_types=["video"])
|
| 70 |
transcribe_btn = gr.Button("Transcribe Video", variant="primary")
|
| 71 |
|
| 72 |
with gr.Column():
|
|
|
|
| 75 |
download_btn = gr.DownloadButton(label="Download Transcript")
|
| 76 |
|
| 77 |
# Processing function
|
| 78 |
+
def process_video(video_file):
|
| 79 |
+
if video_file is None:
|
| 80 |
return "", "Please upload a video file first", None
|
| 81 |
|
| 82 |
+
# Read file content
|
| 83 |
+
with open(video_file.name, "rb") as f:
|
| 84 |
+
video_bytes = f.read()
|
| 85 |
+
|
| 86 |
+
transcript, status = transcribe_video(video_bytes)
|
| 87 |
return transcript, status, transcript
|
| 88 |
|
| 89 |
# Set up button actions
|