video-saathi / app.py
Hemantt's picture
Kaggle notebook + GPU fallback for P100
98e8223 verified
Raw History Blame Contribute Delete
6.28 kB
import os, tempfile, threading, queue
import gradio as gr
import pipeline as P
OUT = os.path.join(tempfile.gettempdir(), "video_saathi_out")
# Pehli baar DeepFilterNet tool download kar lo (taaki pehla video tez chale)
threading.Thread(target=P.deep_filter_bin, daemon=True).start()
STRENGTH = {"Poora (best saaf aawaz)": 100, "Madhyam (natural)": 24, "Halka": 12, "Band": 0}
LANG = {"Hindi": "hi", "English": "en", "Apne aap pehchaane": "auto"}
def _stream(fn, *a, **kw):
"""Kaam alag thread mein, log line-by-line UI par."""
q, box = queue.Queue(), {}
def work():
try:
box["r"] = fn(*a, log=lambda m: q.put(m), **kw)
except Exception as e:
box["e"] = e
q.put(None)
threading.Thread(target=work, daemon=True).start()
lines = []
while True:
m = q.get()
if m is None:
break
lines.append(m)
yield "\n".join(lines), None
if "e" in box:
lines.append(f"❌ Galti: {box['e']}")
yield "\n".join(lines), None
else:
lines.append("✅ Taiyaar!")
yield "\n".join(lines), box["r"]
def run_main(f, strength, cut, thresh, min_sil, margin, caps, burn, lang, title, subtitle, cards):
if f is None:
yield "Pehle video ya audio file chuniye.", None, None, None
return
for log, r in _stream(P.process, f if isinstance(f, str) else f.name, OUT,
strength_db=STRENGTH[strength], cut_silence=cut, thresh_db=thresh,
min_silence=min_sil, margin=margin, captions=caps, burn=burn,
language=LANG[lang], title=title, subtitle=subtitle, cards_text=cards):
if r is None:
yield log, None, None, None
else:
out, srt, engine, d, k = r
log += f"\nLambai {d/60:.1f} min → {k/60:.1f} min" + (f" • Subtitle: {engine}" if engine else "")
vid = out if out.endswith(".mp4") else None
yield log, vid, [x for x in (out, srt) if x], None
def run_text(f, title, subtitle, cards, srt):
if f is None:
yield "Pehle video chuniye.", None
return
for log, r in _stream(P.add_text_only, f if isinstance(f, str) else f.name, OUT, title=title,
subtitle=subtitle, cards_text=cards,
srt_path=(srt if isinstance(srt, str) else srt.name) if srt else None):
yield log, r
CARD_HELP = ("Har line: samay | text (samay final video ka)\n"
"0:30-0:36 | सूत्र: D = b² − 4ac\n"
"1:12 | उत्तर: x = 2, 3 (sirf shuru ka samay = 5 second)")
with gr.Blocks(title="Gyan Ghar Video Saathi") as demo:
gr.Markdown("# 🎬 Gyan Ghar Video Saathi\nRecording daaliye → shor hatega, khamoshi kategi, Hindi subtitle "
"aur title judega → YouTube ke liye taiyaar MP4 + SRT.")
with gr.Tab("1. Video taiyaar karein"):
with gr.Row():
with gr.Column():
f = gr.File(label="Video / audio file (MP4, MKV, MOV, MP3, WAV, M4A…)", type="filepath")
strength = gr.Radio(list(STRENGTH), value="Poora (best saaf aawaz)", label="Shor hatana (DeepFilterNet)")
cut = gr.Checkbox(value=True, label="Khamoshi wali jagah kaatein")
with gr.Accordion("Khamoshi settings", open=False):
thresh = gr.Slider(-60, -20, value=-40, step=1, label="Kitni dheemi aawaz = khamoshi (dB)",
info="Saans/halka shor bhi kat raha ho to -45; khamoshi na kate to -35")
min_sil = gr.Slider(0.3, 2.0, value=0.6, step=0.1, label="Kam se kam kitni lambi khamoshi kaatein (sec)")
margin = gr.Slider(0.0, 0.5, value=0.15, step=0.05, label="Shabdon ke aage-peeche chhoda samay (sec)")
caps = gr.Checkbox(value=True, label="Subtitle banayein (SRT)")
burn = gr.Checkbox(value=True, label="Subtitle video par bhi chhapein")
lang = gr.Radio(list(LANG), value="Hindi", label="Bhasha")
title = gr.Textbox(label="Title card (shuru ke 4 sec, animation ke saath)", placeholder="द्विघात समीकरण")
subtitle = gr.Textbox(label="Title ke neeche", placeholder="कक्षा 10 • अध्याय 4 • प्रश्नावली 4.1")
cards = gr.Textbox(label="Text cards (vaikalpik)", lines=3, placeholder=CARD_HELP)
go = gr.Button("▶ Shuru karein", variant="primary")
with gr.Column():
log = gr.Textbox(label="Kaam ki sthiti", lines=8)
vid = gr.Video(label="Taiyaar video")
files = gr.File(label="Download (MP4 / MP3 + SRT)", file_count="multiple")
hidden = gr.State()
go.click(run_main, [f, strength, cut, thresh, min_sil, margin, caps, burn, lang, title, subtitle, cards],
[log, vid, files, hidden])
with gr.Tab("2. Sirf text / subtitle jodein"):
gr.Markdown("Taiyaar video dekhkar samay note kijiye, phir yahan text cards ya sudhara hua SRT jodiye.")
with gr.Row():
with gr.Column():
f2 = gr.File(label="Video (MP4)", type="filepath")
t2 = gr.Textbox(label="Title card")
s2 = gr.Textbox(label="Title ke neeche")
c2 = gr.Textbox(label="Text cards", lines=4, placeholder=CARD_HELP)
srt2 = gr.File(label="SRT (vaikalpik, sudhar kar)", type="filepath")
go2 = gr.Button("▶ Text jodein", variant="primary")
with gr.Column():
log2 = gr.Textbox(label="Kaam ki sthiti", lines=4)
vid2 = gr.Video(label="Nayi video")
go2.click(run_text, [f2, t2, s2, c2, srt2], [log2, vid2])
gr.Markdown("Tip: YouTube par SRT alag se upload karein (Subtitles → Upload file), search mein madad milti hai.")
if __name__ == "__main__":
import sys
in_colab = ("google.colab" in sys.modules or os.path.exists("/content")
or os.path.exists("/kaggle") or bool(os.environ.get("KAGGLE_KERNEL_RUN_TYPE")))
demo.queue(default_concurrency_limit=1).launch(share=in_colab, debug=in_colab)