Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,43 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import requests
|
| 3 |
from datetime import timedelta
|
|
|
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
-
# ASR
|
| 7 |
# -----------------------------
|
| 8 |
ASR_URL = "https://api-inference.huggingface.co/models/facebook/wav2vec2-large-960h"
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def transcribe_audio(audio_path):
|
| 12 |
try:
|
| 13 |
with open(audio_path, "rb") as f:
|
|
@@ -30,74 +60,81 @@ def transcribe_audio(audio_path):
|
|
| 30 |
|
| 31 |
|
| 32 |
# -----------------------------
|
| 33 |
-
#
|
| 34 |
# -----------------------------
|
| 35 |
def make_srt(text):
|
| 36 |
try:
|
| 37 |
words = text.split()
|
| 38 |
lines = []
|
| 39 |
-
|
| 40 |
|
| 41 |
for w in words:
|
| 42 |
-
if len(
|
| 43 |
-
|
| 44 |
else:
|
| 45 |
-
lines.append(
|
| 46 |
-
|
| 47 |
|
| 48 |
-
if
|
| 49 |
-
lines.append(
|
| 50 |
|
| 51 |
-
|
| 52 |
-
srt_output = ""
|
| 53 |
for i, caption in enumerate(lines, start=1):
|
| 54 |
start = timedelta(seconds=(i - 1) * 3)
|
| 55 |
end = timedelta(seconds=i * 3)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
out_file = "subtitles.srt"
|
| 62 |
-
with open(out_file, "w", encoding="utf-8") as f:
|
| 63 |
-
f.write(srt_output)
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
|
|
|
| 67 |
except Exception as e:
|
| 68 |
-
return None, f"SRT
|
| 69 |
|
| 70 |
|
| 71 |
# -----------------------------
|
| 72 |
-
#
|
| 73 |
# -----------------------------
|
| 74 |
-
def process(
|
| 75 |
try:
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
if not text:
|
| 78 |
-
return None,
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
-
return None, f"
|
| 86 |
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
#
|
| 90 |
-
#
|
| 91 |
with gr.Blocks() as app:
|
| 92 |
-
gr.Markdown("## 🎬
|
| 93 |
-
|
| 94 |
-
audio_in = gr.Audio(label="Upload Video or Audio")
|
| 95 |
-
btn = gr.Button("Generate SRT")
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
-
|
|
|
|
| 101 |
|
|
|
|
| 102 |
|
| 103 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
import requests
|
| 4 |
from datetime import timedelta
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
# -----------------------------
|
| 8 |
+
# Free ASR Model (No Token)
|
| 9 |
# -----------------------------
|
| 10 |
ASR_URL = "https://api-inference.huggingface.co/models/facebook/wav2vec2-large-960h"
|
| 11 |
|
| 12 |
|
| 13 |
+
# -----------------------------
|
| 14 |
+
# Extract audio from any video
|
| 15 |
+
# -----------------------------
|
| 16 |
+
def extract_audio(video_path):
|
| 17 |
+
try:
|
| 18 |
+
audio_path = "audio.wav"
|
| 19 |
+
cmd = [
|
| 20 |
+
"ffmpeg", "-y",
|
| 21 |
+
"-i", video_path,
|
| 22 |
+
"-ac", "1", "-ar", "16000",
|
| 23 |
+
audio_path
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
subprocess.run(
|
| 27 |
+
cmd,
|
| 28 |
+
stdout=subprocess.PIPE,
|
| 29 |
+
stderr=subprocess.PIPE,
|
| 30 |
+
check=True
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
return audio_path, "Audio extracted successfully!"
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return None, f"FFmpeg Error: {e}"
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# -----------------------------
|
| 39 |
+
# Transcribe audio using free HF model
|
| 40 |
+
# -----------------------------
|
| 41 |
def transcribe_audio(audio_path):
|
| 42 |
try:
|
| 43 |
with open(audio_path, "rb") as f:
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
# -----------------------------
|
| 63 |
+
# Create SRT without any library
|
| 64 |
# -----------------------------
|
| 65 |
def make_srt(text):
|
| 66 |
try:
|
| 67 |
words = text.split()
|
| 68 |
lines = []
|
| 69 |
+
chunk = ""
|
| 70 |
|
| 71 |
for w in words:
|
| 72 |
+
if len(chunk.split()) < 8:
|
| 73 |
+
chunk += w + " "
|
| 74 |
else:
|
| 75 |
+
lines.append(chunk.strip())
|
| 76 |
+
chunk = w + " "
|
| 77 |
|
| 78 |
+
if chunk:
|
| 79 |
+
lines.append(chunk.strip())
|
| 80 |
|
| 81 |
+
srt_out = ""
|
|
|
|
| 82 |
for i, caption in enumerate(lines, start=1):
|
| 83 |
start = timedelta(seconds=(i - 1) * 3)
|
| 84 |
end = timedelta(seconds=i * 3)
|
| 85 |
|
| 86 |
+
srt_out += f"{i}\n"
|
| 87 |
+
srt_out += f"{str(start)[:-3].replace('.', ',')} --> {str(end)[:-3].replace('.', ',')}\n"
|
| 88 |
+
srt_out += caption + "\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
file = "subtitles.srt"
|
| 91 |
+
with open(file, "w", encoding="utf-8") as f:
|
| 92 |
+
f.write(srt_out)
|
| 93 |
|
| 94 |
+
return file, "SRT created!"
|
| 95 |
except Exception as e:
|
| 96 |
+
return None, f"SRT Error: {e}"
|
| 97 |
|
| 98 |
|
| 99 |
# -----------------------------
|
| 100 |
+
# Master function for Gradio UI
|
| 101 |
# -----------------------------
|
| 102 |
+
def process(video):
|
| 103 |
try:
|
| 104 |
+
# Step 1: Extract audio
|
| 105 |
+
audio_path, log1 = extract_audio(video)
|
| 106 |
+
if not audio_path:
|
| 107 |
+
return None, log1
|
| 108 |
+
|
| 109 |
+
# Step 2: Transcribe
|
| 110 |
+
text, log2 = transcribe_audio(audio_path)
|
| 111 |
if not text:
|
| 112 |
+
return None, log2
|
| 113 |
|
| 114 |
+
# Step 3: Create SRT
|
| 115 |
+
srt_file, log3 = make_srt(text)
|
| 116 |
+
if not srt_file:
|
| 117 |
+
return None, log3
|
| 118 |
|
| 119 |
+
logs = f"{log1}\n{log2}\n{log3}"
|
| 120 |
+
return srt_file, logs
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
+
return None, f"Processing Error: {e}"
|
| 124 |
|
| 125 |
|
| 126 |
+
# -----------------------------
|
| 127 |
+
# Gradio App UI
|
| 128 |
+
# -----------------------------
|
| 129 |
with gr.Blocks() as app:
|
| 130 |
+
gr.Markdown("## 🎬 Smart Video Subtitle Generator (No Token, No Whisper)")
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
+
video = gr.Video(label="Upload Any Video File")
|
| 133 |
+
btn = gr.Button("Generate Subtitles")
|
| 134 |
|
| 135 |
+
srt_output = gr.File(label="Download SRT")
|
| 136 |
+
debug = gr.Textbox(label="Debug Log", lines=5)
|
| 137 |
|
| 138 |
+
btn.click(process, inputs=video, outputs=[srt_output, debug])
|
| 139 |
|
| 140 |
app.launch()
|