Spaces:
Running
Running
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import yt_dlp
|
| 3 |
+
import whisper
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# 1. Load Whisper Model (Small model CPU par fast chalta hai)
|
| 7 |
+
# Agar GPU available hai, to ye automatically use karega, warna CPU.
|
| 8 |
+
print("Loading Whisper Model...")
|
| 9 |
+
model = whisper.load_model("base")
|
| 10 |
+
print("Model Loaded!")
|
| 11 |
+
|
| 12 |
+
def get_audio_from_tiktok(url):
|
| 13 |
+
"""
|
| 14 |
+
TikTok URL se audio download karne ka function using yt-dlp
|
| 15 |
+
"""
|
| 16 |
+
try:
|
| 17 |
+
# Output filename template
|
| 18 |
+
output_filename = "downloaded_audio"
|
| 19 |
+
|
| 20 |
+
# Agar purani file hai to delete karein
|
| 21 |
+
if os.path.exists(f"{output_filename}.mp3"):
|
| 22 |
+
os.remove(f"{output_filename}.mp3")
|
| 23 |
+
|
| 24 |
+
ydl_opts = {
|
| 25 |
+
'format': 'bestaudio/best',
|
| 26 |
+
'outtmpl': output_filename, # File name without extension
|
| 27 |
+
'postprocessors': [{
|
| 28 |
+
'key': 'FFmpegExtractAudio',
|
| 29 |
+
'preferredcodec': 'mp3',
|
| 30 |
+
'preferredquality': '192',
|
| 31 |
+
}],
|
| 32 |
+
'quiet': True,
|
| 33 |
+
'no_warnings': True,
|
| 34 |
+
'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'
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 38 |
+
ydl.download([url])
|
| 39 |
+
|
| 40 |
+
return f"{output_filename}.mp3"
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return str(e)
|
| 44 |
+
|
| 45 |
+
def process_tiktok(tiktok_url):
|
| 46 |
+
"""
|
| 47 |
+
Main function jo UI se connect hoga
|
| 48 |
+
"""
|
| 49 |
+
if not tiktok_url:
|
| 50 |
+
return "Error: Please enter a valid URL."
|
| 51 |
+
|
| 52 |
+
# Step 1: Download Audio
|
| 53 |
+
print(f"Downloading from: {tiktok_url}")
|
| 54 |
+
audio_path = get_audio_from_tiktok(tiktok_url)
|
| 55 |
+
|
| 56 |
+
# Check if download was successful (audio path should be a file path, not error text)
|
| 57 |
+
if not audio_path.endswith(".mp3"):
|
| 58 |
+
return f"Download Failed: {audio_path}"
|
| 59 |
+
|
| 60 |
+
# Step 2: Transcribe using Whisper
|
| 61 |
+
print("Transcribing...")
|
| 62 |
+
try:
|
| 63 |
+
# Whisper audio ko text me badal dega
|
| 64 |
+
result = model.transcribe(audio_path)
|
| 65 |
+
transcript = result["text"]
|
| 66 |
+
return transcript
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return f"Transcription Error: {str(e)}"
|
| 69 |
+
|
| 70 |
+
# --- Gradio UI ---
|
| 71 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 72 |
+
gr.Markdown(
|
| 73 |
+
"""
|
| 74 |
+
# 🎵 TikTok to Text Transcriber
|
| 75 |
+
Paste a TikTok link below to get the text transcript of the video.
|
| 76 |
+
"""
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
with gr.Row():
|
| 80 |
+
inp_url = gr.Textbox(label="TikTok Video URL", placeholder="Paste link here (e.g., https://www.tiktok.com/@user/video/...)")
|
| 81 |
+
btn = gr.Button("Transcribe 📝", variant="primary")
|
| 82 |
+
|
| 83 |
+
out_text = gr.Textbox(label="Transcript", lines=10, show_copy_button=True)
|
| 84 |
+
|
| 85 |
+
btn.click(fn=process_tiktok, inputs=inp_url, outputs=out_text)
|
| 86 |
+
|
| 87 |
+
# Launch
|
| 88 |
+
demo.launch()
|