Spaces:
Running
Running
| import gradio as gr | |
| import yt_dlp | |
| import os | |
| from faster_whisper import WhisperModel | |
| # Global variable to store the model | |
| model = None | |
| def load_model_if_needed(): | |
| """ | |
| Model ko tabhi load karega jab zaroorat hogi (Lazy Loading). | |
| Isse App turant start ho jayegi. | |
| """ | |
| global model | |
| if model is None: | |
| print("π₯ Loading Whisper Model for the first time... (Please wait)") | |
| # 'tiny' fast hai, 'base' thoda better hai. | |
| model = WhisperModel("base", device="cpu", compute_type="int8") | |
| print("β Model Loaded!") | |
| return model | |
| def get_audio_from_tiktok(url): | |
| try: | |
| output_filename = "tiktok_audio" | |
| # Purani file delete karein | |
| if os.path.exists(f"{output_filename}.mp3"): | |
| os.remove(f"{output_filename}.mp3") | |
| ydl_opts = { | |
| 'format': 'bestaudio/best', | |
| 'outtmpl': output_filename, | |
| 'postprocessors': [{ | |
| 'key': 'FFmpegExtractAudio', | |
| 'preferredcodec': 'mp3', | |
| 'preferredquality': '192', | |
| }], | |
| 'quiet': True, | |
| 'no_warnings': True, | |
| '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' | |
| } | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| ydl.download([url]) | |
| return f"{output_filename}.mp3" | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def process_tiktok(tiktok_url): | |
| if not tiktok_url: | |
| return "β οΈ Please enter a URL." | |
| # 1. Load Model (Sirf pehli baar time lega) | |
| try: | |
| current_model = load_model_if_needed() | |
| except Exception as e: | |
| return f"Model Loading Error: {str(e)}" | |
| # 2. Download Audio | |
| print(f"β¬οΈ Downloading audio from: {tiktok_url}") | |
| audio_path = get_audio_from_tiktok(tiktok_url) | |
| if not audio_path.endswith(".mp3"): | |
| return f"Download Error: {audio_path}" | |
| # 3. Transcribe | |
| print("π Transcribing...") | |
| try: | |
| segments, info = current_model.transcribe(audio_path, beam_size=5) | |
| full_text = "" | |
| for segment in segments: | |
| full_text += segment.text + " " | |
| return full_text.strip() | |
| except Exception as e: | |
| return f"Transcription Error: {str(e)}" | |
| # --- UI --- | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# π Turbo TikTok Transcriber") | |
| gr.Markdown("Note: Pehli baar run karne par 1-2 minute lagenge (Model download hoga). Uske baad ye fast chalega.") | |
| with gr.Row(): | |
| url_input = gr.Textbox(label="TikTok URL", placeholder="Paste link here...") | |
| run_btn = gr.Button("Transcribe", variant="primary") | |
| output_text = gr.Textbox(label="Transcript", lines=10, show_copy_button=True) | |
| run_btn.click(fn=process_tiktok, inputs=url_input, outputs=output_text) | |
| demo.launch() | |