Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import shutil
|
|
| 5 |
import requests
|
| 6 |
from faster_whisper import WhisperModel
|
| 7 |
|
| 8 |
-
# 1. Model Setup
|
| 9 |
model = None
|
| 10 |
|
| 11 |
def load_model():
|
|
@@ -16,85 +16,87 @@ def load_model():
|
|
| 16 |
print("β
Model Loaded!")
|
| 17 |
return model
|
| 18 |
|
| 19 |
-
# 2.
|
| 20 |
def get_actual_url(short_url):
|
| 21 |
try:
|
| 22 |
-
# User-Agent lagana zaroori hai taaki TikTok redirect allow kare
|
| 23 |
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'}
|
| 24 |
response = requests.head(short_url, allow_redirects=True, headers=headers)
|
| 25 |
-
print(f"π Converted {short_url} to {response.url}")
|
| 26 |
return response.url
|
| 27 |
except:
|
| 28 |
return short_url
|
| 29 |
|
| 30 |
-
# 3.
|
| 31 |
-
def
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
'
|
| 52 |
-
'
|
| 53 |
-
'ignoreerrors': False,
|
| 54 |
-
# TikTok Special Options
|
| 55 |
-
'extractor_args': {'tiktok': {'app_version': ['30.0.0']}},
|
| 56 |
-
'http_headers': {
|
| 57 |
-
'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',
|
| 58 |
-
'Referer': 'https://www.tiktok.com/'
|
| 59 |
-
}
|
| 60 |
}
|
|
|
|
| 61 |
|
|
|
|
| 62 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 63 |
ydl.download([actual_url])
|
| 64 |
-
|
| 65 |
-
return f"{output}.mp3"
|
| 66 |
except Exception as e:
|
| 67 |
-
return f"β Download Error: {str(e)}"
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
print(f"Processing: {url}")
|
| 74 |
-
audio = get_audio(url)
|
| 75 |
-
|
| 76 |
-
if not audio.endswith(".mp3"):
|
| 77 |
-
return audio # Return error message
|
| 78 |
-
|
| 79 |
try:
|
| 80 |
current_model = load_model()
|
| 81 |
-
#
|
| 82 |
-
segments, _ = current_model.transcribe(
|
| 83 |
text = " ".join([s.text for s in segments])
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
-
return f"Transcription Error: {str(e)}"
|
| 87 |
|
| 88 |
-
#
|
| 89 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 90 |
-
gr.Markdown("#
|
| 91 |
-
gr.Markdown("
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
-
|
| 95 |
-
btn = gr.Button("
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
demo.launch()
|
|
|
|
| 5 |
import requests
|
| 6 |
from faster_whisper import WhisperModel
|
| 7 |
|
| 8 |
+
# --- 1. Model Setup ---
|
| 9 |
model = None
|
| 10 |
|
| 11 |
def load_model():
|
|
|
|
| 16 |
print("β
Model Loaded!")
|
| 17 |
return model
|
| 18 |
|
| 19 |
+
# --- 2. URL Resolver (Short -> Long) ---
|
| 20 |
def get_actual_url(short_url):
|
| 21 |
try:
|
|
|
|
| 22 |
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'}
|
| 23 |
response = requests.head(short_url, allow_redirects=True, headers=headers)
|
|
|
|
| 24 |
return response.url
|
| 25 |
except:
|
| 26 |
return short_url
|
| 27 |
|
| 28 |
+
# --- 3. Process Video (Download & Transcribe) ---
|
| 29 |
+
def process_video(url):
|
| 30 |
+
if not url:
|
| 31 |
+
return None, "β οΈ URL missing!"
|
| 32 |
+
|
| 33 |
+
print(f"Processing: {url}")
|
| 34 |
+
|
| 35 |
+
# 1. Resolve URL
|
| 36 |
+
actual_url = get_actual_url(url)
|
| 37 |
+
|
| 38 |
+
# 2. Download Video (MP4)
|
| 39 |
+
output_file = "tiktok_video.mp4"
|
| 40 |
+
|
| 41 |
+
# Purani file safai
|
| 42 |
+
if os.path.exists(output_file): os.remove(output_file)
|
| 43 |
|
| 44 |
+
ffmpeg_path = shutil.which("ffmpeg") or "/usr/bin/ffmpeg"
|
| 45 |
+
|
| 46 |
+
ydl_opts = {
|
| 47 |
+
'format': 'best[ext=mp4]/best', # <-- Ab hum Video download kar rahe hain
|
| 48 |
+
'outtmpl': "tiktok_video.%(ext)s",
|
| 49 |
+
'ffmpeg_location': ffmpeg_path,
|
| 50 |
+
'quiet': False,
|
| 51 |
+
'no_warnings': True,
|
| 52 |
+
'nocheckcertificate': True,
|
| 53 |
+
'http_headers': {
|
| 54 |
+
'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',
|
| 55 |
+
'Referer': 'https://www.tiktok.com/'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
+
}
|
| 58 |
|
| 59 |
+
try:
|
| 60 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 61 |
ydl.download([actual_url])
|
|
|
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
+
return None, f"β Download Error: {str(e)}"
|
| 64 |
|
| 65 |
+
if not os.path.exists(output_file):
|
| 66 |
+
return None, "β Video download failed."
|
| 67 |
+
|
| 68 |
+
# 3. Transcribe (Audio from Video)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
try:
|
| 70 |
current_model = load_model()
|
| 71 |
+
# Whisper direct video file se audio padh lega
|
| 72 |
+
segments, _ = current_model.transcribe(output_file, beam_size=1)
|
| 73 |
text = " ".join([s.text for s in segments])
|
| 74 |
+
|
| 75 |
+
# Return Video Path AND Transcript Text
|
| 76 |
+
return output_file, text
|
| 77 |
except Exception as e:
|
| 78 |
+
return output_file, f"Transcription Error: {str(e)}"
|
| 79 |
|
| 80 |
+
# --- 4. UI Design ---
|
| 81 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 82 |
+
gr.Markdown("# π΅ TikTok Downloader & Transcriber")
|
| 83 |
+
gr.Markdown("Link paste karo -> Video download karo -> Text copy karo (Ek sath!)")
|
| 84 |
|
| 85 |
with gr.Row():
|
| 86 |
+
link_input = gr.Textbox(label="TikTok URL", placeholder="Paste link here...", scale=4)
|
| 87 |
+
btn = gr.Button("π Process", variant="primary", scale=1)
|
| 88 |
|
| 89 |
+
with gr.Row():
|
| 90 |
+
# Left Side: Video Player
|
| 91 |
+
with gr.Column():
|
| 92 |
+
video_out = gr.Video(label="Download Video", format="mp4")
|
| 93 |
+
|
| 94 |
+
# Right Side: Code Box for Easy Copy
|
| 95 |
+
with gr.Column():
|
| 96 |
+
# 'gr.Code' use kiya hai taki 1-click copy button mile
|
| 97 |
+
transcript_out = gr.Code(label="Transcript (Click Copy Icon π)", language=None, lines=10)
|
| 98 |
+
|
| 99 |
+
# Button Action
|
| 100 |
+
btn.click(fn=process_video, inputs=link_input, outputs=[video_out, transcript_out])
|
| 101 |
|
| 102 |
demo.launch()
|