Spaces:
Running
Running
| import gradio as gr | |
| import yt_dlp | |
| import os | |
| import shutil | |
| from faster_whisper import WhisperModel | |
| # --- 1. Model Setup (Base Model = Sahi Accuracy) --- | |
| model = None | |
| def load_model(): | |
| global model | |
| if model is None: | |
| print("π₯ Loading Base Model (Best Balance)...") | |
| # 'base' model accuracy aur speed ka perfect mix hai | |
| model = WhisperModel("base", device="cpu", compute_type="int8") | |
| print("β Model Loaded!") | |
| return model | |
| # --- 2. Process Audio --- | |
| def process_audio(url): | |
| if not url: | |
| return "β οΈ URL missing!" | |
| output_audio = "tiktok_audio" | |
| if os.path.exists(f"{output_audio}.mp3"): | |
| os.remove(f"{output_audio}.mp3") | |
| ffmpeg_dir = "/usr/bin" # System Path | |
| ydl_opts = { | |
| 'format': 'bestaudio/best', | |
| 'outtmpl': output_audio, | |
| 'ffmpeg_location': ffmpeg_dir, | |
| 'postprocessors': [{ | |
| 'key': 'FFmpegExtractAudio', | |
| 'preferredcodec': 'mp3', | |
| 'preferredquality': '192', # Quality wapas badha di taaki shabd saaf sunayi dein | |
| }], | |
| 'quiet': True, | |
| 'no_warnings': True, | |
| 'nocheckcertificate': True, | |
| 'http_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', | |
| 'Referer': 'https://www.tiktok.com/' | |
| } | |
| } | |
| try: | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| ydl.download([url]) | |
| except Exception as e: | |
| return f"β Download Error: {str(e)}" | |
| if not os.path.exists(f"{output_audio}.mp3"): | |
| return "β Error: Audio file nahi mili." | |
| # --- Transcribe (Smart Settings) --- | |
| try: | |
| current_model = load_model() | |
| # 'beam_size=1' rakha hai taaki speed tez rahe | |
| # Lekin model 'base' hai to accuracy acchi rahegi | |
| segments, _ = current_model.transcribe( | |
| f"{output_audio}.mp3", | |
| beam_size=1, | |
| vad_filter=True | |
| ) | |
| text = " ".join([s.text for s in segments]) | |
| return text.strip() | |
| except Exception as e: | |
| return f"Transcription Error: {str(e)}" | |
| # --- 3. UI --- | |
| css = """ | |
| .container {max-width: 800px; margin: auto;} | |
| .gr-button-primary {background-color: #2563eb !important; color: white !important;} | |
| """ | |
| with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo: | |
| gr.Markdown("# π Accurate TikTok Transcriber") | |
| with gr.Row(): | |
| link_input = gr.Textbox(label="TikTok URL", placeholder="Paste Link Here...") | |
| btn = gr.Button("Transcribe", variant="primary") | |
| transcript_out = gr.Code(label="Transcript Result", language="markdown", interactive=False, lines=15) | |
| btn.click(fn=process_audio, inputs=link_input, outputs=transcript_out) | |
| demo.launch() | |