Offex's picture
Update app.py
5deb27a verified
raw
history blame
3.11 kB
import gradio as gr
import yt_dlp
import os
import shutil
import requests
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. URL Resolver ---
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.url
except:
return short_url
# --- 3. Process Video & Transcript ---
def process_video(url):
if not url:
return None, "⚠️ URL missing!"
actual_url = get_actual_url(url)
output_video = "final_video.mp4"
if os.path.exists(output_video): os.remove(output_video)
ffmpeg_path = shutil.which("ffmpeg") or "/usr/bin/ffmpeg"
ydl_opts = {
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
'outtmpl': 'downloaded_video.%(ext)s',
'merge_output_format': 'mp4', # ब्राउज़र संगतता के लिए पक्का MP4
'ffmpeg_location': ffmpeg_path,
'http_headers': {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Referer': 'https://www.tiktok.com/'
}
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([actual_url])
# डाउनलोड की गई फ़ाइल का नाम बदलें
downloaded_file = next(f for f in os.listdir('.') if f.startswith('downloaded_video'))
shutil.move(downloaded_file, output_video)
except Exception as e:
return None, f"❌ Error: {str(e)}"
# Transcribe
try:
current_model = load_model()
segments, _ = current_model.transcribe(output_video, beam_size=1)
text = " ".join([s.text for s in segments])
return output_video, text
except Exception as e:
return output_video, f"Transcription Error: {str(e)}"
# --- 4. UI ---
with gr.Blocks(theme="soft") as demo:
gr.Markdown("# 🎵 TikTok Downloader & Transcriber")
with gr.Row():
link_input = gr.Textbox(label="TikTok URL", placeholder="लिंक यहाँ पेस्ट करें...")
btn = gr.Button("🚀 Start", variant="primary")
with gr.Row():
video_out = gr.Video(label="Video")
# 'gr.Code' में कॉपी बटन पहले से मौजूद होता है
transcript_out = gr.Code(label="Transcript (Copy करने के लिए ऊपर दाईं ओर क्लिक करें)", language="markdown")
btn.click(fn=process_video, inputs=link_input, outputs=[video_out, transcript_out])
demo.launch()