Spaces:
Running
Running
| import gradio as gr | |
| import yt_dlp | |
| import os | |
| import shutil | |
| from faster_whisper import WhisperModel | |
| # 1. Model Setup | |
| model = None | |
| def load_model(): | |
| global model | |
| if model is None: | |
| print("Loading Whisper Model...") | |
| model = WhisperModel("base", device="cpu", compute_type="int8") | |
| print("Model Loaded!") | |
| return model | |
| # 2. Audio Download Function (Fixed FFmpeg Issue) | |
| def get_audio(url): | |
| try: | |
| output = "tiktok_audio" | |
| # Purani files safai | |
| if os.path.exists(f"{output}.mp3"): os.remove(f"{output}.mp3") | |
| # FFmpeg ka path dhoondo (System me kahan hai) | |
| ffmpeg_path = shutil.which("ffmpeg") or "/usr/bin/ffmpeg" | |
| ydl_opts = { | |
| 'format': 'bestaudio/best', | |
| 'outtmpl': output, | |
| 'ffmpeg_location': ffmpeg_path, # <--- Fix: FFmpeg ka pakka rasta | |
| 'postprocessors': [{ | |
| 'key': 'FFmpegExtractAudio', | |
| 'preferredcodec': 'mp3', | |
| 'preferredquality': '192', | |
| }], | |
| 'quiet': False, # Logs dikhayega agar error aaye | |
| 'no_warnings': False, | |
| # TikTok Block Bypass Headers | |
| 'http_headers': { | |
| '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', | |
| 'Referer': 'https://www.tiktok.com/' | |
| } | |
| } | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| ydl.download([url]) | |
| return f"{output}.mp3" | |
| except Exception as e: | |
| # Error ko saaf dikhane ke liye | |
| return f"Download Error: {str(e)}" | |
| # 3. Transcription Function | |
| def transcribe(url): | |
| if not url: return "⚠️ URL missing!" | |
| print(f"Processing: {url}") | |
| audio = get_audio(url) | |
| # Agar audio file nahi bani, to error wapas karo | |
| if not audio.endswith(".mp3"): | |
| return f"❌ {audio}" | |
| try: | |
| current_model = load_model() | |
| segments, _ = current_model.transcribe(audio, beam_size=5) | |
| text = " ".join([s.text for s in segments]) | |
| return text | |
| except Exception as e: | |
| return f"Transcription Error: {str(e)}" | |
| # 4. UI (Gradio 6.0 Fixed) | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# 🚀 Turbo TikTok Transcriber") | |
| gr.Markdown("Paste a TikTok link below. (Agar error aaye to dobara try karein, TikTok kabhi-kabhi block karta hai).") | |
| with gr.Row(): | |
| link = gr.Textbox(label="TikTok URL", placeholder="https://www.tiktok.com/@...") | |
| btn = gr.Button("Transcribe", variant="primary") | |
| out = gr.Textbox(label="Transcript Result", lines=10) | |
| btn.click(fn=transcribe, inputs=link, outputs=out) | |
| demo.launch() | |