Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ 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
|
|
@@ -10,77 +11,90 @@ model = None
|
|
| 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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
| 32 |
'postprocessors': [{
|
| 33 |
'key': 'FFmpegExtractAudio',
|
| 34 |
'preferredcodec': 'mp3',
|
| 35 |
'preferredquality': '192',
|
| 36 |
}],
|
| 37 |
-
'quiet': False,
|
| 38 |
-
'no_warnings':
|
| 39 |
-
#
|
|
|
|
|
|
|
|
|
|
| 40 |
'http_headers': {
|
| 41 |
-
'User-Agent': 'Mozilla/5.0 (
|
| 42 |
'Referer': 'https://www.tiktok.com/'
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 47 |
-
ydl.download([
|
| 48 |
|
| 49 |
return f"{output}.mp3"
|
| 50 |
except Exception as e:
|
| 51 |
-
|
| 52 |
-
return f"Download Error: {str(e)}"
|
| 53 |
|
| 54 |
-
#
|
| 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
|
| 64 |
|
| 65 |
try:
|
| 66 |
current_model = load_model()
|
| 67 |
-
|
|
|
|
| 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 |
-
#
|
| 74 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 75 |
-
gr.Markdown("# 🚀
|
| 76 |
-
gr.Markdown("
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
-
link = gr.Textbox(label="TikTok URL", placeholder="https://
|
| 80 |
btn = gr.Button("Transcribe", variant="primary")
|
| 81 |
|
| 82 |
-
out = gr.Textbox(label="
|
| 83 |
-
|
| 84 |
btn.click(fn=transcribe, inputs=link, outputs=out)
|
| 85 |
|
| 86 |
demo.launch()
|
|
|
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
| 4 |
import shutil
|
| 5 |
+
import requests
|
| 6 |
from faster_whisper import WhisperModel
|
| 7 |
|
| 8 |
# 1. Model Setup
|
|
|
|
| 11 |
def load_model():
|
| 12 |
global model
|
| 13 |
if model is None:
|
| 14 |
+
print("📥 Loading Whisper Model...")
|
| 15 |
model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 16 |
+
print("✅ Model Loaded!")
|
| 17 |
return model
|
| 18 |
|
| 19 |
+
# 2. Helper: TikTok Short URL to Long URL Resolver
|
| 20 |
+
def get_actual_url(short_url):
|
| 21 |
+
try:
|
| 22 |
+
# User-Agent lagana zaroori hai taaki TikTok redirect allow kare
|
| 23 |
+
headers = {'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'}
|
| 24 |
+
response = requests.head(short_url, allow_redirects=True, headers=headers)
|
| 25 |
+
print(f"🔗 Converted {short_url} to {response.url}")
|
| 26 |
+
return response.url
|
| 27 |
+
except:
|
| 28 |
+
return short_url
|
| 29 |
+
|
| 30 |
+
# 3. Audio Download Function
|
| 31 |
def get_audio(url):
|
| 32 |
try:
|
| 33 |
+
# Pehle URL ko resolve karo (Short -> Long)
|
| 34 |
+
actual_url = get_actual_url(url)
|
| 35 |
+
|
| 36 |
output = "tiktok_audio"
|
|
|
|
| 37 |
if os.path.exists(f"{output}.mp3"): os.remove(f"{output}.mp3")
|
| 38 |
|
|
|
|
| 39 |
ffmpeg_path = shutil.which("ffmpeg") or "/usr/bin/ffmpeg"
|
| 40 |
|
| 41 |
ydl_opts = {
|
| 42 |
'format': 'bestaudio/best',
|
| 43 |
'outtmpl': output,
|
| 44 |
+
'ffmpeg_location': ffmpeg_path,
|
| 45 |
'postprocessors': [{
|
| 46 |
'key': 'FFmpegExtractAudio',
|
| 47 |
'preferredcodec': 'mp3',
|
| 48 |
'preferredquality': '192',
|
| 49 |
}],
|
| 50 |
+
'quiet': False,
|
| 51 |
+
'no_warnings': True,
|
| 52 |
+
'nocheckcertificate': True, # SSL Errors ignore karne ke liye
|
| 53 |
+
'ignoreerrors': False,
|
| 54 |
+
# TikTok Special Options
|
| 55 |
+
'extractor_args': {'tiktok': {'app_version': ['30.0.0']}},
|
| 56 |
'http_headers': {
|
| 57 |
+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 58 |
'Referer': 'https://www.tiktok.com/'
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 63 |
+
ydl.download([actual_url])
|
| 64 |
|
| 65 |
return f"{output}.mp3"
|
| 66 |
except Exception as e:
|
| 67 |
+
return f"❌ Download Error: {str(e)}"
|
|
|
|
| 68 |
|
| 69 |
+
# 4. Transcription Function
|
| 70 |
def transcribe(url):
|
| 71 |
if not url: return "⚠️ URL missing!"
|
| 72 |
|
| 73 |
print(f"Processing: {url}")
|
| 74 |
audio = get_audio(url)
|
| 75 |
|
|
|
|
| 76 |
if not audio.endswith(".mp3"):
|
| 77 |
+
return audio # Return error message
|
| 78 |
|
| 79 |
try:
|
| 80 |
current_model = load_model()
|
| 81 |
+
# Beam size 1 is faster for CPU
|
| 82 |
+
segments, _ = current_model.transcribe(audio, beam_size=1)
|
| 83 |
text = " ".join([s.text for s in segments])
|
| 84 |
return text
|
| 85 |
except Exception as e:
|
| 86 |
return f"Transcription Error: {str(e)}"
|
| 87 |
|
| 88 |
+
# 5. UI
|
| 89 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 90 |
+
gr.Markdown("# 🚀 TikTok Transcriber (Fixed)")
|
| 91 |
+
gr.Markdown("Agar 'Status Code 0' error aaye, to TikTok ne server IP block kiya hai. Dobara try karein.")
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
+
link = gr.Textbox(label="TikTok URL", placeholder="Paste https://vt.tiktok.com/... link")
|
| 95 |
btn = gr.Button("Transcribe", variant="primary")
|
| 96 |
|
| 97 |
+
out = gr.Textbox(label="Result", lines=10)
|
|
|
|
| 98 |
btn.click(fn=transcribe, inputs=link, outputs=out)
|
| 99 |
|
| 100 |
demo.launch()
|