Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950511 unmodified line5354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114import gradio as grimport yt_dlpimport osimport shutilimport requestsfrom faster_whisper import WhisperModel# --- 1. Model Setup (Lazy Loading) ---model = Nonedef load_model():global modelif model is None:print("📥 Loading Whisper Model...")# 'base' model balance hai speed aur accuracy kamodel = WhisperModel("base", device="cpu", compute_type="int8")print("✅ Model Loaded!")return model# --- 2. URL Resolver (Short Link Fix) ---def get_actual_url(short_url):try: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'}response = requests.head(short_url, allow_redirects=True, headers=headers)return response.urlexcept:return short_url# --- 3. Audio Download & Transcribe ---def process_audio(url):if not url:return "⚠️ कृपया URL डालें।"print(f"Processing: {url}")actual_url = get_actual_url(url)# Filenameoutput_audio = "tiktok_audio.mp3"if os.path.exists(output_audio): os.remove(output_audio)# FFmpeg checkffmpeg_path = shutil.which("ffmpeg") or "/usr/bin/ffmpeg"# Audio Download Settings (No Video)ydl_opts = {'format': 'bestaudio/best', # Sirf Audio download karega (Bahut Fast)'outtmpl': 'tiktok_audio','ffmpeg_location': ffmpeg_path,'postprocessors': [{'key': 'FFmpegExtractAudio','preferredcodec': 'mp3',1 unmodified line}],'quiet': True,'no_warnings': True,'http_headers': {'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','Referer': 'https://www.tiktok.com/'}}# 1. Download Audiotry:with yt_dlp.YoutubeDL(ydl_opts) as ydl:ydl.download([actual_url])except Exception as e:return f"❌ Download Error: {str(e)}"# 2. Transcribetry:current_model = load_model()segments, _ = current_model.transcribe(f"{output_audio}.mp3", beam_size=5)text = " ".join([s.text for s in segments])return text.strip()except Exception as e:return f"Transcription Error: {str(e)}"# --- 4. Professional UI ---# Custom CSS for clean lookcss = """.container {max-width: 800px; margin: auto;}.gr-button-primary {background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%); border: none;}"""with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:with gr.Column(elem_classes="container"):gr.Markdown("""# 📝 Professional TikTok TranscriberPaste the link below to extract text from the video.""")with gr.Row():link_input = gr.Textbox(label="TikTok URL",placeholder="Paste https://vt.tiktok.com/... link here",scale=4)btn = gr.Button("Transcribe Text", variant="primary", scale=1)# 'gr.Code' box automatic copy button ke sath aata hai header metranscript_out = gr.Code(label="Transcript Result (Click Copy Icon 📋)",language="markdown",interactive=False,lines=15)# Button Actionbtn.click(fn=process_audio, inputs=link_input, outputs=transcript_out)demo.launch()12345678910111213141516171819202122232425262728293031323334353637381 unmodified line40414243444546474849505152535455565758596061626364656667686970717273747576777879808182import gradio as grimport yt_dlpimport osfrom faster_whisper import WhisperModel# --- 1. Model Setup ---model = Nonedef load_model():global modelif model is None:print("📥 Loading Whisper Model...")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 डालें।"print(f"Processing: {url}")# Filename Setupoutput_audio = "tiktok_audio"if os.path.exists(f"{output_audio}.mp3"):os.remove(f"{output_audio}.mp3")# FIX: FFmpeg ka location folder (jahan ffmpeg aur ffprobe dono hote hain)ffmpeg_dir = "/usr/bin"ydl_opts = {'format': 'bestaudio/best','outtmpl': output_audio,'ffmpeg_location': ffmpeg_dir, # <--- DIRECT PATH FIX'postprocessors': [{'key': 'FFmpegExtractAudio','preferredcodec': 'mp3',1 unmodified line}],'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/'}}# 1. Downloadtry:with yt_dlp.YoutubeDL(ydl_opts) as ydl:ydl.download([url])except Exception as e:return f"❌ Download Error: {str(e)}\n(Ensure packages.txt has 'ffmpeg')"# 2. Transcribeif not os.path.exists(f"{output_audio}.mp3"):return "❌ Error: Audio file download nahi ho payi."try:current_model = load_model()# Transcribe directly from mp3segments, _ = current_model.transcribe(f"{output_audio}.mp3", beam_size=5)text = " ".join([s.text for s in segments])return text.strip()except Exception as e:return f"Transcription Error: {str(e)}"# --- 3. UI ---with gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# 📝 Professional 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", language="markdown", interactive=False, lines=15)btn.click(fn=process_audio, inputs=link_input, outputs=transcript_out)demo.launch()
app.py+28-60