Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
| 4 |
-
import shutil
|
| 5 |
-
import requests
|
| 6 |
from faster_whisper import WhisperModel
|
| 7 |
|
| 8 |
-
# --- 1. Model Setup
|
| 9 |
model = None
|
| 10 |
|
| 11 |
def load_model():
|
| 12 |
global model
|
| 13 |
if model is None:
|
| 14 |
print("📥 Loading Whisper Model...")
|
| 15 |
-
# 'base' model balance hai speed aur accuracy ka
|
| 16 |
model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 17 |
print("✅ Model Loaded!")
|
| 18 |
return model
|
| 19 |
|
| 20 |
-
# --- 2.
|
| 21 |
-
def get_actual_url(short_url):
|
| 22 |
-
try:
|
| 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 |
-
return response.url
|
| 26 |
-
except:
|
| 27 |
-
return short_url
|
| 28 |
-
|
| 29 |
-
# --- 3. Audio Download & Transcribe ---
|
| 30 |
def process_audio(url):
|
| 31 |
if not url:
|
| 32 |
return "⚠️ कृपया URL डालें।"
|
| 33 |
|
| 34 |
print(f"Processing: {url}")
|
| 35 |
-
actual_url = get_actual_url(url)
|
| 36 |
|
| 37 |
-
# Filename
|
| 38 |
-
output_audio = "tiktok_audio
|
| 39 |
-
if os.path.exists(output_audio):
|
|
|
|
| 40 |
|
| 41 |
-
# FFmpeg
|
| 42 |
-
|
| 43 |
|
| 44 |
-
# Audio Download Settings (No Video)
|
| 45 |
ydl_opts = {
|
| 46 |
-
'format': 'bestaudio/best',
|
| 47 |
-
'outtmpl':
|
| 48 |
-
'ffmpeg_location':
|
| 49 |
'postprocessors': [{
|
| 50 |
'key': 'FFmpegExtractAudio',
|
| 51 |
'preferredcodec': 'mp3',
|
|
@@ -53,62 +40,43 @@ def process_audio(url):
|
|
| 53 |
}],
|
| 54 |
'quiet': True,
|
| 55 |
'no_warnings': True,
|
|
|
|
| 56 |
'http_headers': {
|
| 57 |
-
'User-Agent': 'Mozilla/5.0 (
|
| 58 |
'Referer': 'https://www.tiktok.com/'
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
-
# 1. Download
|
| 63 |
try:
|
| 64 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 65 |
-
ydl.download([
|
| 66 |
except Exception as e:
|
| 67 |
-
return f"❌ Download Error: {str(e)}"
|
| 68 |
|
| 69 |
# 2. Transcribe
|
|
|
|
|
|
|
|
|
|
| 70 |
try:
|
| 71 |
current_model = load_model()
|
|
|
|
| 72 |
segments, _ = current_model.transcribe(f"{output_audio}.mp3", beam_size=5)
|
| 73 |
text = " ".join([s.text for s in segments])
|
| 74 |
return text.strip()
|
| 75 |
except Exception as e:
|
| 76 |
return f"Transcription Error: {str(e)}"
|
| 77 |
|
| 78 |
-
# ---
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
.
|
| 83 |
-
"""
|
| 84 |
-
|
| 85 |
-
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 86 |
|
| 87 |
-
|
| 88 |
-
gr.Markdown(
|
| 89 |
-
"""
|
| 90 |
-
# 📝 Professional TikTok Transcriber
|
| 91 |
-
Paste the link below to extract text from the video.
|
| 92 |
-
"""
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
with gr.Row():
|
| 96 |
-
link_input = gr.Textbox(
|
| 97 |
-
label="TikTok URL",
|
| 98 |
-
placeholder="Paste https://vt.tiktok.com/... link here",
|
| 99 |
-
scale=4
|
| 100 |
-
)
|
| 101 |
-
btn = gr.Button("Transcribe Text", variant="primary", scale=1)
|
| 102 |
-
|
| 103 |
-
# 'gr.Code' box automatic copy button ke sath aata hai header me
|
| 104 |
-
transcript_out = gr.Code(
|
| 105 |
-
label="Transcript Result (Click Copy Icon 📋)",
|
| 106 |
-
language="markdown",
|
| 107 |
-
interactive=False,
|
| 108 |
-
lines=15
|
| 109 |
-
)
|
| 110 |
|
| 111 |
-
# Button Action
|
| 112 |
btn.click(fn=process_audio, inputs=link_input, outputs=transcript_out)
|
| 113 |
|
| 114 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
from faster_whisper import WhisperModel
|
| 5 |
|
| 6 |
+
# --- 1. Model Setup ---
|
| 7 |
model = None
|
| 8 |
|
| 9 |
def load_model():
|
| 10 |
global model
|
| 11 |
if model is None:
|
| 12 |
print("📥 Loading Whisper Model...")
|
|
|
|
| 13 |
model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 14 |
print("✅ Model Loaded!")
|
| 15 |
return model
|
| 16 |
|
| 17 |
+
# --- 2. Process Audio ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def process_audio(url):
|
| 19 |
if not url:
|
| 20 |
return "⚠️ कृपया URL डालें।"
|
| 21 |
|
| 22 |
print(f"Processing: {url}")
|
|
|
|
| 23 |
|
| 24 |
+
# Filename Setup
|
| 25 |
+
output_audio = "tiktok_audio"
|
| 26 |
+
if os.path.exists(f"{output_audio}.mp3"):
|
| 27 |
+
os.remove(f"{output_audio}.mp3")
|
| 28 |
|
| 29 |
+
# FIX: FFmpeg ka location folder (jahan ffmpeg aur ffprobe dono hote hain)
|
| 30 |
+
ffmpeg_dir = "/usr/bin"
|
| 31 |
|
|
|
|
| 32 |
ydl_opts = {
|
| 33 |
+
'format': 'bestaudio/best',
|
| 34 |
+
'outtmpl': output_audio,
|
| 35 |
+
'ffmpeg_location': ffmpeg_dir, # <--- DIRECT PATH FIX
|
| 36 |
'postprocessors': [{
|
| 37 |
'key': 'FFmpegExtractAudio',
|
| 38 |
'preferredcodec': 'mp3',
|
|
|
|
| 40 |
}],
|
| 41 |
'quiet': True,
|
| 42 |
'no_warnings': True,
|
| 43 |
+
'nocheckcertificate': True,
|
| 44 |
'http_headers': {
|
| 45 |
+
'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',
|
| 46 |
'Referer': 'https://www.tiktok.com/'
|
| 47 |
}
|
| 48 |
}
|
| 49 |
|
| 50 |
+
# 1. Download
|
| 51 |
try:
|
| 52 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 53 |
+
ydl.download([url])
|
| 54 |
except Exception as e:
|
| 55 |
+
return f"❌ Download Error: {str(e)}\n(Ensure packages.txt has 'ffmpeg')"
|
| 56 |
|
| 57 |
# 2. Transcribe
|
| 58 |
+
if not os.path.exists(f"{output_audio}.mp3"):
|
| 59 |
+
return "❌ Error: Audio file download nahi ho payi."
|
| 60 |
+
|
| 61 |
try:
|
| 62 |
current_model = load_model()
|
| 63 |
+
# Transcribe directly from mp3
|
| 64 |
segments, _ = current_model.transcribe(f"{output_audio}.mp3", beam_size=5)
|
| 65 |
text = " ".join([s.text for s in segments])
|
| 66 |
return text.strip()
|
| 67 |
except Exception as e:
|
| 68 |
return f"Transcription Error: {str(e)}"
|
| 69 |
|
| 70 |
+
# --- 3. UI ---
|
| 71 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 72 |
+
gr.Markdown("# 📝 Professional TikTok Transcriber")
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
link_input = gr.Textbox(label="TikTok URL", placeholder="Paste Link Here...")
|
| 76 |
+
btn = gr.Button("Transcribe", variant="primary")
|
|
|
|
| 77 |
|
| 78 |
+
transcript_out = gr.Code(label="Transcript", language="markdown", interactive=False, lines=15)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
|
|
|
| 80 |
btn.click(fn=process_audio, inputs=link_input, outputs=transcript_out)
|
| 81 |
|
| 82 |
demo.launch()
|