Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,12 @@ import gradio as gr
|
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
| 4 |
import shutil
|
|
|
|
| 5 |
from faster_whisper import WhisperModel
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
| 8 |
model = None
|
| 9 |
|
| 10 |
def load_model():
|
|
@@ -12,116 +15,139 @@ def load_model():
|
|
| 12 |
if model is None:
|
| 13 |
print("π₯ Loading Whisper Model...")
|
| 14 |
model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 15 |
-
print("β
Model Loaded
|
| 16 |
return model
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
path = shutil.which("ffmpeg")
|
| 22 |
-
if path
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def download_audio_from_url(url):
|
| 28 |
-
|
| 29 |
-
if os.path.exists(f"{output_path}.mp3"): os.remove(f"{output_path}.mp3")
|
| 30 |
-
|
| 31 |
-
# Dynamic FFmpeg Path (Ye error fix karega)
|
| 32 |
-
ffmpeg_dir = get_ffmpeg_dir()
|
| 33 |
-
print(f"π§ FFmpeg found at: {ffmpeg_dir}")
|
| 34 |
|
| 35 |
ydl_opts = {
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
}],
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
'nocheckcertificate': True,
|
| 47 |
-
'http_headers': {
|
| 48 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
| 49 |
-
'Referer': 'https://www.tiktok.com/'
|
| 50 |
-
}
|
| 51 |
}
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
ydl.download([url])
|
| 56 |
-
return f"{output_path}.mp3"
|
| 57 |
-
except Exception as e:
|
| 58 |
-
raise Exception(f"Download Fail: {str(e)}")
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def transcribe_media(url_input, file_input):
|
| 62 |
-
|
| 63 |
-
audio_file_path = None
|
| 64 |
-
|
| 65 |
try:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
else:
|
| 77 |
-
return "β οΈ
|
| 78 |
-
|
| 79 |
-
if not os.path.exists(
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
audio_file_path,
|
| 88 |
-
beam_size=1,
|
| 89 |
vad_filter=True
|
| 90 |
)
|
| 91 |
-
|
| 92 |
-
text = " ".join(
|
| 93 |
-
return text.strip()
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
return f"β Error: {str(e)}"
|
| 97 |
|
| 98 |
-
#
|
|
|
|
|
|
|
| 99 |
css = """
|
| 100 |
.container {max-width: 900px; margin: auto;}
|
| 101 |
-
.gr-button-primary {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
"""
|
| 103 |
|
| 104 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 105 |
-
|
| 106 |
with gr.Column(elem_classes="container"):
|
| 107 |
-
gr.Markdown("
|
| 108 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
with gr.Tabs():
|
| 111 |
-
# TAB 1: Link
|
| 112 |
with gr.TabItem("π Paste Link"):
|
| 113 |
-
url_in = gr.Textbox(
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
# TAB 2: File Upload
|
| 117 |
with gr.TabItem("π Upload File"):
|
| 118 |
-
file_in = gr.
|
|
|
|
|
|
|
|
|
|
| 119 |
btn_file = gr.Button("π Transcribe File", variant="primary")
|
| 120 |
|
| 121 |
-
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
btn_file.click(fn=transcribe_media, inputs=[gr.State(None), file_in], outputs=transcript_out)
|
| 126 |
|
| 127 |
-
demo.launch()
|
|
|
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
| 4 |
import shutil
|
| 5 |
+
import subprocess
|
| 6 |
from faster_whisper import WhisperModel
|
| 7 |
|
| 8 |
+
# ===============================
|
| 9 |
+
# 1. Whisper Model (Lazy Load)
|
| 10 |
+
# ===============================
|
| 11 |
model = None
|
| 12 |
|
| 13 |
def load_model():
|
|
|
|
| 15 |
if model is None:
|
| 16 |
print("π₯ Loading Whisper Model...")
|
| 17 |
model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 18 |
+
print("β
Model Loaded")
|
| 19 |
return model
|
| 20 |
|
| 21 |
+
# ===============================
|
| 22 |
+
# 2. FFmpeg Path
|
| 23 |
+
# ===============================
|
| 24 |
+
def get_ffmpeg_path():
|
| 25 |
path = shutil.which("ffmpeg")
|
| 26 |
+
return path if path else "/usr/bin/ffmpeg"
|
| 27 |
+
|
| 28 |
+
# ===============================
|
| 29 |
+
# 3. Convert Video β Audio
|
| 30 |
+
# ===============================
|
| 31 |
+
def extract_audio(video_path):
|
| 32 |
+
audio_path = "uploaded_audio.wav"
|
| 33 |
+
if os.path.exists(audio_path):
|
| 34 |
+
os.remove(audio_path)
|
| 35 |
+
|
| 36 |
+
cmd = [
|
| 37 |
+
get_ffmpeg_path(),
|
| 38 |
+
"-i", video_path,
|
| 39 |
+
"-vn",
|
| 40 |
+
"-ac", "1",
|
| 41 |
+
"-ar", "16000",
|
| 42 |
+
audio_path,
|
| 43 |
+
"-y"
|
| 44 |
+
]
|
| 45 |
+
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 46 |
+
return audio_path
|
| 47 |
+
|
| 48 |
+
# ===============================
|
| 49 |
+
# 4. Download Audio from ANY URL
|
| 50 |
+
# ===============================
|
| 51 |
def download_audio_from_url(url):
|
| 52 |
+
output = "url_audio.%(ext)s"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
ydl_opts = {
|
| 55 |
+
"format": "bestaudio/best",
|
| 56 |
+
"outtmpl": output,
|
| 57 |
+
"ffmpeg_location": os.path.dirname(get_ffmpeg_path()),
|
| 58 |
+
"postprocessors": [{
|
| 59 |
+
"key": "FFmpegExtractAudio",
|
| 60 |
+
"preferredcodec": "wav",
|
| 61 |
+
"preferredquality": "192",
|
| 62 |
}],
|
| 63 |
+
"quiet": True,
|
| 64 |
+
"nocheckcertificate": True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
}
|
| 66 |
|
| 67 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 68 |
+
ydl.download([url])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
return "url_audio.wav"
|
| 71 |
+
|
| 72 |
+
# ===============================
|
| 73 |
+
# 5. Main Transcribe Logic
|
| 74 |
+
# ===============================
|
| 75 |
def transcribe_media(url_input, file_input):
|
| 76 |
+
|
|
|
|
|
|
|
| 77 |
try:
|
| 78 |
+
audio_path = None
|
| 79 |
+
|
| 80 |
+
# ---------- FILE UPLOAD ----------
|
| 81 |
+
if file_input:
|
| 82 |
+
ext = os.path.splitext(file_input)[1].lower()
|
| 83 |
+
|
| 84 |
+
if ext in [".mp3", ".wav", ".m4a"]:
|
| 85 |
+
audio_path = file_input
|
| 86 |
+
else:
|
| 87 |
+
audio_path = extract_audio(file_input)
|
| 88 |
+
|
| 89 |
+
# ---------- URL ----------
|
| 90 |
+
elif url_input and url_input.strip():
|
| 91 |
+
audio_path = download_audio_from_url(url_input)
|
| 92 |
+
|
| 93 |
else:
|
| 94 |
+
return "β οΈ Please paste a link or upload a file."
|
| 95 |
+
|
| 96 |
+
if not os.path.exists(audio_path):
|
| 97 |
+
return "β Audio processing failed."
|
| 98 |
+
|
| 99 |
+
model = load_model()
|
| 100 |
+
|
| 101 |
+
segments, _ = model.transcribe(
|
| 102 |
+
audio_path,
|
| 103 |
+
beam_size=1,
|
|
|
|
|
|
|
| 104 |
vad_filter=True
|
| 105 |
)
|
| 106 |
+
|
| 107 |
+
text = " ".join(seg.text for seg in segments)
|
| 108 |
+
return text.strip() if text else "β οΈ No speech detected."
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
return f"β Error: {str(e)}"
|
| 112 |
|
| 113 |
+
# ===============================
|
| 114 |
+
# 6. UI
|
| 115 |
+
# ===============================
|
| 116 |
css = """
|
| 117 |
.container {max-width: 900px; margin: auto;}
|
| 118 |
+
.gr-button-primary {
|
| 119 |
+
background: linear-gradient(90deg,#ff416c,#ff4b2b);
|
| 120 |
+
border: none;
|
| 121 |
+
color: white;
|
| 122 |
+
}
|
| 123 |
"""
|
| 124 |
|
| 125 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
|
|
| 126 |
with gr.Column(elem_classes="container"):
|
| 127 |
+
gr.Markdown("## π Universal Video Transcript Tool")
|
| 128 |
+
gr.Markdown(
|
| 129 |
+
"Supports **YouTube, TikTok, Instagram, Facebook, Twitter/X**\n\n"
|
| 130 |
+
"**OR** upload video/audio file."
|
| 131 |
+
)
|
| 132 |
|
| 133 |
with gr.Tabs():
|
|
|
|
| 134 |
with gr.TabItem("π Paste Link"):
|
| 135 |
+
url_in = gr.Textbox(
|
| 136 |
+
label="Video URL",
|
| 137 |
+
placeholder="https://youtube.com / tiktok.com / instagram.com"
|
| 138 |
+
)
|
| 139 |
+
btn_url = gr.Button("π§ Transcribe Link", variant="primary")
|
| 140 |
|
|
|
|
| 141 |
with gr.TabItem("π Upload File"):
|
| 142 |
+
file_in = gr.File(
|
| 143 |
+
label="Upload Video / Audio",
|
| 144 |
+
file_types=[".mp4", ".mkv", ".mov", ".webm", ".avi", ".mp3", ".wav"]
|
| 145 |
+
)
|
| 146 |
btn_file = gr.Button("π Transcribe File", variant="primary")
|
| 147 |
|
| 148 |
+
output = gr.Code(label="Transcript Output", language="markdown", lines=15)
|
| 149 |
|
| 150 |
+
btn_url.click(transcribe_media, [url_in, gr.State(None)], output)
|
| 151 |
+
btn_file.click(transcribe_media, [gr.State(None), file_in], output)
|
|
|
|
| 152 |
|
| 153 |
+
demo.launch()
|