Offex commited on
Commit
1247156
·
verified ·
1 Parent(s): 5deb27a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -31
app.py CHANGED
@@ -5,18 +5,19 @@ 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
  model = WhisperModel("base", device="cpu", compute_type="int8")
16
  print("✅ Model Loaded!")
17
  return model
18
 
19
- # --- 2. URL Resolver ---
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'}
@@ -25,60 +26,89 @@ def get_actual_url(short_url):
25
  except:
26
  return short_url
27
 
28
- # --- 3. Process Video & Transcript ---
29
- def process_video(url):
30
  if not url:
31
- return None, "⚠️ URL missing!"
32
 
 
33
  actual_url = get_actual_url(url)
34
- output_video = "final_video.mp4"
35
 
36
- if os.path.exists(output_video): os.remove(output_video)
 
 
37
 
 
38
  ffmpeg_path = shutil.which("ffmpeg") or "/usr/bin/ffmpeg"
39
 
 
40
  ydl_opts = {
41
- 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
42
- 'outtmpl': 'downloaded_video.%(ext)s',
43
- 'merge_output_format': 'mp4', # ब्राउज़र संगतता के लिए पक्का MP4
44
  'ffmpeg_location': ffmpeg_path,
 
 
 
 
 
 
 
45
  'http_headers': {
46
- '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',
47
  'Referer': 'https://www.tiktok.com/'
48
  }
49
  }
50
 
 
51
  try:
52
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
53
  ydl.download([actual_url])
54
- # डाउनलोड की गई फ़ाइल का नाम बदलें
55
- downloaded_file = next(f for f in os.listdir('.') if f.startswith('downloaded_video'))
56
- shutil.move(downloaded_file, output_video)
57
  except Exception as e:
58
- return None, f"❌ Error: {str(e)}"
59
 
60
- # Transcribe
61
  try:
62
  current_model = load_model()
63
- segments, _ = current_model.transcribe(output_video, beam_size=1)
64
  text = " ".join([s.text for s in segments])
65
- return output_video, text
66
  except Exception as e:
67
- return output_video, f"Transcription Error: {str(e)}"
68
 
69
- # --- 4. UI ---
70
- with gr.Blocks(theme="soft") as demo:
71
- gr.Markdown("# 🎵 TikTok Downloader & Transcriber")
72
-
73
- with gr.Row():
74
- link_input = gr.Textbox(label="TikTok URL", placeholder="लिंक यहाँ पेस्ट करें...")
75
- btn = gr.Button("🚀 Start", variant="primary")
 
76
 
77
- with gr.Row():
78
- video_out = gr.Video(label="Video")
79
- # 'gr.Code' में कॉपी बटन पहले से मौजूद होता है
80
- transcript_out = gr.Code(label="Transcript (Copy करने के लिए ऊपर दाईं ओर क्लिक करें)", language="markdown")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- btn.click(fn=process_video, inputs=link_input, outputs=[video_out, transcript_out])
 
83
 
84
  demo.launch()
 
5
  import requests
6
  from faster_whisper import WhisperModel
7
 
8
+ # --- 1. Model Setup (Lazy Loading) ---
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. URL Resolver (Short Link Fix) ---
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'}
 
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.mp3"
39
+ if os.path.exists(output_audio): os.remove(output_audio)
40
 
41
+ # FFmpeg check
42
  ffmpeg_path = shutil.which("ffmpeg") or "/usr/bin/ffmpeg"
43
 
44
+ # Audio Download Settings (No Video)
45
  ydl_opts = {
46
+ 'format': 'bestaudio/best', # Sirf Audio download karega (Bahut Fast)
47
+ 'outtmpl': 'tiktok_audio',
 
48
  'ffmpeg_location': ffmpeg_path,
49
+ 'postprocessors': [{
50
+ 'key': 'FFmpegExtractAudio',
51
+ 'preferredcodec': 'mp3',
52
+ 'preferredquality': '192',
53
+ }],
54
+ 'quiet': True,
55
+ 'no_warnings': True,
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
+ # 1. Download Audio
63
  try:
64
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
65
  ydl.download([actual_url])
 
 
 
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
+ # --- 4. Professional UI ---
79
+ # Custom CSS for clean look
80
+ css = """
81
+ .container {max-width: 800px; margin: auto;}
82
+ .gr-button-primary {background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%); border: none;}
83
+ """
84
+
85
+ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
86
 
87
+ with gr.Column(elem_classes="container"):
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()