Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,86 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
|
|
|
| 4 |
from faster_whisper import WhisperModel
|
| 5 |
|
| 6 |
-
#
|
| 7 |
model = None
|
| 8 |
|
| 9 |
-
def
|
| 10 |
global model
|
| 11 |
if model is None:
|
| 12 |
-
print("
|
| 13 |
model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 14 |
-
print("
|
| 15 |
return model
|
| 16 |
|
| 17 |
-
|
|
|
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
ydl_opts = {
|
| 24 |
'format': 'bestaudio/best',
|
| 25 |
-
'outtmpl':
|
|
|
|
| 26 |
'postprocessors': [{
|
| 27 |
'key': 'FFmpegExtractAudio',
|
| 28 |
'preferredcodec': 'mp3',
|
| 29 |
'preferredquality': '192',
|
| 30 |
}],
|
| 31 |
-
'quiet':
|
| 32 |
-
'no_warnings':
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
|
| 36 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 37 |
ydl.download([url])
|
| 38 |
|
| 39 |
-
return f"{
|
| 40 |
-
except Exception as e:
|
| 41 |
-
return f"Error: {str(e)}"
|
| 42 |
-
|
| 43 |
-
def process_tiktok(tiktok_url):
|
| 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 |
-
|
| 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 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
return f"Transcription Error: {str(e)}"
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
with gr.Blocks() as demo:
|
| 65 |
gr.Markdown("# 🚀 Turbo TikTok Transcriber")
|
|
|
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
run_btn.click(fn=process_tiktok, inputs=url_input, outputs=output_text)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
| 4 |
+
import shutil
|
| 5 |
from faster_whisper import WhisperModel
|
| 6 |
|
| 7 |
+
# 1. Model Setup
|
| 8 |
model = None
|
| 9 |
|
| 10 |
+
def load_model():
|
| 11 |
global 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 |
+
# 2. Audio Download Function (Fixed FFmpeg Issue)
|
| 19 |
+
def get_audio(url):
|
| 20 |
try:
|
| 21 |
+
output = "tiktok_audio"
|
| 22 |
+
# Purani files safai
|
| 23 |
+
if os.path.exists(f"{output}.mp3"): os.remove(f"{output}.mp3")
|
| 24 |
+
|
| 25 |
+
# FFmpeg ka path dhoondo (System me kahan hai)
|
| 26 |
+
ffmpeg_path = shutil.which("ffmpeg") or "/usr/bin/ffmpeg"
|
| 27 |
|
| 28 |
ydl_opts = {
|
| 29 |
'format': 'bestaudio/best',
|
| 30 |
+
'outtmpl': output,
|
| 31 |
+
'ffmpeg_location': ffmpeg_path, # <--- Fix: FFmpeg ka pakka rasta
|
| 32 |
'postprocessors': [{
|
| 33 |
'key': 'FFmpegExtractAudio',
|
| 34 |
'preferredcodec': 'mp3',
|
| 35 |
'preferredquality': '192',
|
| 36 |
}],
|
| 37 |
+
'quiet': False, # Logs dikhayega agar error aaye
|
| 38 |
+
'no_warnings': False,
|
| 39 |
+
# TikTok Block Bypass Headers
|
| 40 |
+
'http_headers': {
|
| 41 |
+
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1',
|
| 42 |
+
'Referer': 'https://www.tiktok.com/'
|
| 43 |
+
}
|
| 44 |
}
|
| 45 |
|
| 46 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 47 |
ydl.download([url])
|
| 48 |
|
| 49 |
+
return f"{output}.mp3"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
+
# Error ko saaf dikhane ke liye
|
| 52 |
+
return f"Download Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# 3. Transcription Function
|
| 55 |
+
def transcribe(url):
|
| 56 |
+
if not url: return "⚠️ URL missing!"
|
| 57 |
+
|
| 58 |
+
print(f"Processing: {url}")
|
| 59 |
+
audio = get_audio(url)
|
| 60 |
+
|
| 61 |
+
# Agar audio file nahi bani, to error wapas karo
|
| 62 |
+
if not audio.endswith(".mp3"):
|
| 63 |
+
return f"❌ {audio}"
|
| 64 |
+
|
| 65 |
try:
|
| 66 |
+
current_model = load_model()
|
| 67 |
+
segments, _ = current_model.transcribe(audio, beam_size=5)
|
| 68 |
+
text = " ".join([s.text for s in segments])
|
| 69 |
+
return text
|
| 70 |
except Exception as e:
|
| 71 |
return f"Transcription Error: {str(e)}"
|
| 72 |
|
| 73 |
+
# 4. UI (Gradio 6.0 Fixed)
|
| 74 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 75 |
gr.Markdown("# 🚀 Turbo TikTok Transcriber")
|
| 76 |
+
gr.Markdown("Paste a TikTok link below. (Agar error aaye to dobara try karein, TikTok kabhi-kabhi block karta hai).")
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
+
link = gr.Textbox(label="TikTok URL", placeholder="https://www.tiktok.com/@...")
|
| 80 |
+
btn = gr.Button("Transcribe", variant="primary")
|
| 81 |
|
| 82 |
+
out = gr.Textbox(label="Transcript Result", lines=10)
|
| 83 |
+
|
| 84 |
+
btn.click(fn=transcribe, inputs=link, outputs=out)
|
|
|
|
| 85 |
|
| 86 |
+
demo.launch()
|
|
|