Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import yt_dlp
|
| 3 |
-
import whisper
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
print("Loading Whisper Model...")
|
| 9 |
-
model = whisper.load_model("base")
|
| 10 |
-
print("Model Loaded!")
|
| 11 |
|
| 12 |
-
def
|
| 13 |
"""
|
| 14 |
-
|
|
|
|
| 15 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Agar purani file hai to delete karein
|
| 21 |
if os.path.exists(f"{output_filename}.mp3"):
|
| 22 |
os.remove(f"{output_filename}.mp3")
|
| 23 |
|
| 24 |
ydl_opts = {
|
| 25 |
'format': 'bestaudio/best',
|
| 26 |
-
'outtmpl': output_filename,
|
| 27 |
'postprocessors': [{
|
| 28 |
'key': 'FFmpegExtractAudio',
|
| 29 |
'preferredcodec': 'mp3',
|
|
@@ -38,51 +43,48 @@ def get_audio_from_tiktok(url):
|
|
| 38 |
ydl.download([url])
|
| 39 |
|
| 40 |
return f"{output_filename}.mp3"
|
| 41 |
-
|
| 42 |
except Exception as e:
|
| 43 |
-
return str(e)
|
| 44 |
|
| 45 |
def process_tiktok(tiktok_url):
|
| 46 |
-
"""
|
| 47 |
-
Main function jo UI se connect hoga
|
| 48 |
-
"""
|
| 49 |
if not tiktok_url:
|
| 50 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
print(f"Downloading from: {tiktok_url}")
|
| 54 |
audio_path = get_audio_from_tiktok(tiktok_url)
|
| 55 |
|
| 56 |
-
# Check if download was successful (audio path should be a file path, not error text)
|
| 57 |
if not audio_path.endswith(".mp3"):
|
| 58 |
-
return f"Download
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
print("Transcribing...")
|
| 62 |
try:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
except Exception as e:
|
| 68 |
return f"Transcription Error: {str(e)}"
|
| 69 |
|
| 70 |
-
# ---
|
| 71 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 72 |
-
gr.Markdown(
|
| 73 |
-
|
| 74 |
-
# 🎵 TikTok to Text Transcriber
|
| 75 |
-
Paste a TikTok link below to get the text transcript of the video.
|
| 76 |
-
"""
|
| 77 |
-
)
|
| 78 |
|
| 79 |
with gr.Row():
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
|
| 87 |
-
# Launch
|
| 88 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import yt_dlp
|
|
|
|
| 3 |
import os
|
| 4 |
+
from faster_whisper import WhisperModel
|
| 5 |
|
| 6 |
+
# Global variable to store the model
|
| 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 for the first time... (Please wait)")
|
| 17 |
+
# 'tiny' fast hai, 'base' thoda better hai.
|
| 18 |
+
model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 19 |
+
print("✅ Model Loaded!")
|
| 20 |
+
return model
|
| 21 |
+
|
| 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 |
|
| 29 |
ydl_opts = {
|
| 30 |
'format': 'bestaudio/best',
|
| 31 |
+
'outtmpl': output_filename,
|
| 32 |
'postprocessors': [{
|
| 33 |
'key': 'FFmpegExtractAudio',
|
| 34 |
'preferredcodec': 'mp3',
|
|
|
|
| 43 |
ydl.download([url])
|
| 44 |
|
| 45 |
return f"{output_filename}.mp3"
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
+
return f"Error: {str(e)}"
|
| 48 |
|
| 49 |
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 Loading Error: {str(e)}"
|
| 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(theme=gr.themes.Soft()) as demo:
|
| 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 |
+
output_text = gr.Textbox(label="Transcript", lines=10, show_copy_button=True)
|
| 87 |
|
| 88 |
+
run_btn.click(fn=process_tiktok, inputs=url_input, outputs=output_text)
|
| 89 |
|
|
|
|
| 90 |
demo.launch()
|