Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,18 +3,13 @@ import yt_dlp
|
|
| 3 |
import os
|
| 4 |
from faster_whisper import WhisperModel
|
| 5 |
|
| 6 |
-
# Global variable
|
| 7 |
model = None
|
| 8 |
|
| 9 |
def load_model_if_needed():
|
| 10 |
-
"""
|
| 11 |
-
Model ko tabhi load karega jab zaroorat hogi (Lazy Loading).
|
| 12 |
-
Isse App turant start ho jayegi.
|
| 13 |
-
"""
|
| 14 |
global model
|
| 15 |
if model is None:
|
| 16 |
-
print("📥 Loading Whisper Model
|
| 17 |
-
# 'tiny' fast hai, 'base' thoda better hai.
|
| 18 |
model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 19 |
print("✅ Model Loaded!")
|
| 20 |
return model
|
|
@@ -22,7 +17,6 @@ def load_model_if_needed():
|
|
| 22 |
def get_audio_from_tiktok(url):
|
| 23 |
try:
|
| 24 |
output_filename = "tiktok_audio"
|
| 25 |
-
# Purani file delete karein
|
| 26 |
if os.path.exists(f"{output_filename}.mp3"):
|
| 27 |
os.remove(f"{output_filename}.mp3")
|
| 28 |
|
|
@@ -36,7 +30,7 @@ def get_audio_from_tiktok(url):
|
|
| 36 |
}],
|
| 37 |
'quiet': True,
|
| 38 |
'no_warnings': True,
|
| 39 |
-
'user_agent': 'Mozilla/5.0
|
| 40 |
}
|
| 41 |
|
| 42 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
@@ -50,41 +44,34 @@ def process_tiktok(tiktok_url):
|
|
| 50 |
if not tiktok_url:
|
| 51 |
return "⚠️ Please enter a URL."
|
| 52 |
|
| 53 |
-
# 1. Load Model (Sirf pehli baar time lega)
|
| 54 |
try:
|
| 55 |
current_model = load_model_if_needed()
|
| 56 |
except Exception as e:
|
| 57 |
-
return f"Model
|
| 58 |
|
| 59 |
-
# 2. Download Audio
|
| 60 |
-
print(f"⬇️ Downloading audio from: {tiktok_url}")
|
| 61 |
audio_path = get_audio_from_tiktok(tiktok_url)
|
| 62 |
-
|
| 63 |
if not audio_path.endswith(".mp3"):
|
| 64 |
return f"Download Error: {audio_path}"
|
| 65 |
|
| 66 |
-
# 3. Transcribe
|
| 67 |
-
print("📝 Transcribing...")
|
| 68 |
try:
|
| 69 |
segments, info = current_model.transcribe(audio_path, beam_size=5)
|
| 70 |
-
full_text = ""
|
| 71 |
-
for segment in segments:
|
| 72 |
-
full_text += segment.text + " "
|
| 73 |
return full_text.strip()
|
| 74 |
except Exception as e:
|
| 75 |
return f"Transcription Error: {str(e)}"
|
| 76 |
|
| 77 |
-
# --- UI ---
|
| 78 |
-
with gr.Blocks(
|
| 79 |
gr.Markdown("# 🚀 Turbo TikTok Transcriber")
|
| 80 |
-
gr.Markdown("Note: Pehli baar run karne par 1-2 minute lagenge (Model download hoga). Uske baad ye fast chalega.")
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
url_input = gr.Textbox(label="TikTok URL", placeholder="Paste link here...")
|
| 84 |
run_btn = gr.Button("Transcribe", variant="primary")
|
| 85 |
|
| 86 |
-
|
|
|
|
| 87 |
|
| 88 |
run_btn.click(fn=process_tiktok, inputs=url_input, outputs=output_text)
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
from faster_whisper import WhisperModel
|
| 5 |
|
| 6 |
+
# Global variable for model
|
| 7 |
model = None
|
| 8 |
|
| 9 |
def load_model_if_needed():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
global model
|
| 11 |
if model is None:
|
| 12 |
+
print("📥 Loading Whisper Model...")
|
|
|
|
| 13 |
model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 14 |
print("✅ Model Loaded!")
|
| 15 |
return model
|
|
|
|
| 17 |
def get_audio_from_tiktok(url):
|
| 18 |
try:
|
| 19 |
output_filename = "tiktok_audio"
|
|
|
|
| 20 |
if os.path.exists(f"{output_filename}.mp3"):
|
| 21 |
os.remove(f"{output_filename}.mp3")
|
| 22 |
|
|
|
|
| 30 |
}],
|
| 31 |
'quiet': True,
|
| 32 |
'no_warnings': True,
|
| 33 |
+
'user_agent': 'Mozilla/5.0'
|
| 34 |
}
|
| 35 |
|
| 36 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
|
|
| 44 |
if not tiktok_url:
|
| 45 |
return "⚠️ Please enter a URL."
|
| 46 |
|
|
|
|
| 47 |
try:
|
| 48 |
current_model = load_model_if_needed()
|
| 49 |
except Exception as e:
|
| 50 |
+
return f"Model Error: {str(e)}"
|
| 51 |
|
|
|
|
|
|
|
| 52 |
audio_path = get_audio_from_tiktok(tiktok_url)
|
|
|
|
| 53 |
if not audio_path.endswith(".mp3"):
|
| 54 |
return f"Download Error: {audio_path}"
|
| 55 |
|
|
|
|
|
|
|
| 56 |
try:
|
| 57 |
segments, info = current_model.transcribe(audio_path, beam_size=5)
|
| 58 |
+
full_text = " ".join([segment.text for segment in segments])
|
|
|
|
|
|
|
| 59 |
return full_text.strip()
|
| 60 |
except Exception as e:
|
| 61 |
return f"Transcription Error: {str(e)}"
|
| 62 |
|
| 63 |
+
# --- UI Fixed for Gradio 6.0 ---
|
| 64 |
+
with gr.Blocks() as demo:
|
| 65 |
gr.Markdown("# 🚀 Turbo TikTok Transcriber")
|
|
|
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
url_input = gr.Textbox(label="TikTok URL", placeholder="Paste link here...")
|
| 69 |
run_btn = gr.Button("Transcribe", variant="primary")
|
| 70 |
|
| 71 |
+
# Error fix: 'show_copy_button' removed as it's causing the crash
|
| 72 |
+
output_text = gr.Textbox(label="Transcript", lines=10)
|
| 73 |
|
| 74 |
run_btn.click(fn=process_tiktok, inputs=url_input, outputs=output_text)
|
| 75 |
|
| 76 |
+
# Error fix: Theme moved to launch()
|
| 77 |
+
demo.launch(theme=gr.themes.Soft())
|