Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,37 +4,34 @@ import os
|
|
| 4 |
import shutil
|
| 5 |
from faster_whisper import WhisperModel
|
| 6 |
|
| 7 |
-
# --- 1. Model Setup (
|
| 8 |
model = None
|
| 9 |
|
| 10 |
def load_model():
|
| 11 |
global model
|
| 12 |
if model is None:
|
| 13 |
-
print("π₯ Loading
|
| 14 |
-
# 'base' model
|
| 15 |
model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 16 |
print("β
Model Loaded!")
|
| 17 |
return model
|
| 18 |
|
| 19 |
-
# --- 2.
|
| 20 |
-
def
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
output_audio = "tiktok_audio"
|
| 25 |
-
if os.path.exists(f"{output_audio}.mp3"):
|
| 26 |
-
os.remove(f"{output_audio}.mp3")
|
| 27 |
|
| 28 |
-
ffmpeg_dir = "/usr/bin" # System Path
|
| 29 |
|
| 30 |
ydl_opts = {
|
| 31 |
'format': 'bestaudio/best',
|
| 32 |
-
'outtmpl':
|
| 33 |
'ffmpeg_location': ffmpeg_dir,
|
| 34 |
'postprocessors': [{
|
| 35 |
'key': 'FFmpegExtractAudio',
|
| 36 |
'preferredcodec': 'mp3',
|
| 37 |
-
'preferredquality': '192',
|
| 38 |
}],
|
| 39 |
'quiet': True,
|
| 40 |
'no_warnings': True,
|
|
@@ -48,44 +45,89 @@ def process_audio(url):
|
|
| 48 |
try:
|
| 49 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 50 |
ydl.download([url])
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
if not os.path.exists(f"{output_audio}.mp3"):
|
| 55 |
-
return "β Error: Audio file nahi mili."
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
current_model = load_model()
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
# Lekin model 'base' hai to accuracy acchi rahegi
|
| 63 |
segments, _ = current_model.transcribe(
|
| 64 |
-
|
| 65 |
beam_size=1,
|
| 66 |
vad_filter=True
|
| 67 |
)
|
| 68 |
|
| 69 |
text = " ".join([s.text for s in segments])
|
| 70 |
return text.strip()
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
-
return f"
|
| 73 |
|
| 74 |
-
# ---
|
| 75 |
css = """
|
| 76 |
-
.container {max-width:
|
| 77 |
-
.gr-button-primary {background
|
|
|
|
| 78 |
"""
|
| 79 |
|
| 80 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 81 |
-
gr.Markdown("# π Accurate TikTok Transcriber")
|
| 82 |
-
|
| 83 |
-
with gr.Row():
|
| 84 |
-
link_input = gr.Textbox(label="TikTok URL", placeholder="Paste Link Here...")
|
| 85 |
-
btn = gr.Button("Transcribe", variant="primary")
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
demo.launch()
|
|
|
|
| 4 |
import shutil
|
| 5 |
from faster_whisper import WhisperModel
|
| 6 |
|
| 7 |
+
# --- 1. Model Setup (Turbo Settings) ---
|
| 8 |
model = None
|
| 9 |
|
| 10 |
def load_model():
|
| 11 |
global model
|
| 12 |
if model is None:
|
| 13 |
+
print("π₯ Loading Whisper Model (Base + Turbo Settings)...")
|
| 14 |
+
# 'base' model with int8 quantization for speed/accuracy balance
|
| 15 |
model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 16 |
print("β
Model Loaded!")
|
| 17 |
return model
|
| 18 |
|
| 19 |
+
# --- 2. Logic: Download Audio from URL ---
|
| 20 |
+
def download_audio_from_url(url):
|
| 21 |
+
output_path = "downloaded_audio"
|
| 22 |
+
# Cleanup old files
|
| 23 |
+
if os.path.exists(f"{output_path}.mp3"): os.remove(f"{output_path}.mp3")
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
ffmpeg_dir = "/usr/bin" # System Path for Hugging Face
|
| 26 |
|
| 27 |
ydl_opts = {
|
| 28 |
'format': 'bestaudio/best',
|
| 29 |
+
'outtmpl': output_path,
|
| 30 |
'ffmpeg_location': ffmpeg_dir,
|
| 31 |
'postprocessors': [{
|
| 32 |
'key': 'FFmpegExtractAudio',
|
| 33 |
'preferredcodec': 'mp3',
|
| 34 |
+
'preferredquality': '192',
|
| 35 |
}],
|
| 36 |
'quiet': True,
|
| 37 |
'no_warnings': True,
|
|
|
|
| 45 |
try:
|
| 46 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 47 |
ydl.download([url])
|
| 48 |
+
return f"{output_path}.mp3"
|
| 49 |
except Exception as e:
|
| 50 |
+
raise Exception(f"URL Download Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# --- 3. Main Transcribe Function (Handles Both) ---
|
| 53 |
+
def transcribe_media(url_input, file_input):
|
| 54 |
+
|
| 55 |
+
# Decide source: Priority given to File if both exist, else URL
|
| 56 |
+
audio_file_path = None
|
| 57 |
+
|
| 58 |
try:
|
| 59 |
+
# CASE 1: File Upload
|
| 60 |
+
if file_input is not None:
|
| 61 |
+
print(f"π Processing Uploaded File: {file_input}")
|
| 62 |
+
audio_file_path = file_input
|
| 63 |
+
|
| 64 |
+
# CASE 2: URL Input
|
| 65 |
+
elif url_input and url_input.strip() != "":
|
| 66 |
+
print(f"π Processing URL: {url_input}")
|
| 67 |
+
audio_file_path = download_audio_from_url(url_input)
|
| 68 |
+
|
| 69 |
+
else:
|
| 70 |
+
return "β οΈ Error: Please provide either a Link or Upload a File."
|
| 71 |
+
|
| 72 |
+
# --- Transcribe ---
|
| 73 |
+
if not os.path.exists(audio_file_path):
|
| 74 |
+
return "β Error: File not found."
|
| 75 |
+
|
| 76 |
current_model = load_model()
|
| 77 |
|
| 78 |
+
# Turbo Settings: beam_size=1 (Fast), vad_filter=True (Skip Silence)
|
|
|
|
| 79 |
segments, _ = current_model.transcribe(
|
| 80 |
+
audio_file_path,
|
| 81 |
beam_size=1,
|
| 82 |
vad_filter=True
|
| 83 |
)
|
| 84 |
|
| 85 |
text = " ".join([s.text for s in segments])
|
| 86 |
return text.strip()
|
| 87 |
+
|
| 88 |
except Exception as e:
|
| 89 |
+
return f"β Error: {str(e)}"
|
| 90 |
|
| 91 |
+
# --- 4. Turbo UI with Tabs ---
|
| 92 |
css = """
|
| 93 |
+
.container {max-width: 900px; margin: auto;}
|
| 94 |
+
.gr-button-primary {background: linear-gradient(90deg, #1CB5E0 0%, #000851 100%); border: none; color: white;}
|
| 95 |
+
.tab-nav {font-weight: bold; font-size: 1.1em;}
|
| 96 |
"""
|
| 97 |
|
| 98 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
with gr.Column(elem_classes="container"):
|
| 101 |
+
gr.Markdown("# π Turbo Transcriber (Link & Upload)")
|
| 102 |
+
gr.Markdown("Paste a TikTok link **OR** upload an Audio/Video file to get the text.")
|
| 103 |
+
|
| 104 |
+
with gr.Tabs():
|
| 105 |
+
|
| 106 |
+
# TAB 1: Link
|
| 107 |
+
with gr.TabItem("π Paste Link"):
|
| 108 |
+
url_in = gr.Textbox(label="TikTok / YouTube URL", placeholder="https://...")
|
| 109 |
+
btn_url = gr.Button("π Transcribe Link", variant="primary")
|
| 110 |
|
| 111 |
+
# TAB 2: File Upload
|
| 112 |
+
with gr.TabItem("π Upload File"):
|
| 113 |
+
file_in = gr.Audio(label="Upload Audio or Video", type="filepath", sources=["upload", "microphone"])
|
| 114 |
+
btn_file = gr.Button("π Transcribe File", variant="primary")
|
| 115 |
+
|
| 116 |
+
# Output Area (Common for both)
|
| 117 |
+
transcript_out = gr.Code(label="Transcript Result", language="markdown", interactive=False, lines=15)
|
| 118 |
+
|
| 119 |
+
# --- Button Actions ---
|
| 120 |
+
# Logic: Pass 'None' to the unused input
|
| 121 |
+
btn_url.click(
|
| 122 |
+
fn=transcribe_media,
|
| 123 |
+
inputs=[url_in, gr.State(None)], # Link diya, File None
|
| 124 |
+
outputs=transcript_out
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
btn_file.click(
|
| 128 |
+
fn=transcribe_media,
|
| 129 |
+
inputs=[gr.State(None), file_in], # Link None, File diya
|
| 130 |
+
outputs=transcript_out
|
| 131 |
+
)
|
| 132 |
|
| 133 |
demo.launch()
|