Offex commited on
Commit
0a869f5
Β·
verified Β·
1 Parent(s): 666ec0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -57
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. Helper: TikTok Short URL to Long URL Resolver
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. Audio Download Function
31
- def get_audio(url):
32
- try:
33
- # Pehle URL ko resolve karo (Short -> Long)
34
- actual_url = get_actual_url(url)
35
-
36
- output = "tiktok_audio"
37
- if os.path.exists(f"{output}.mp3"): os.remove(f"{output}.mp3")
38
-
39
- ffmpeg_path = shutil.which("ffmpeg") or "/usr/bin/ffmpeg"
 
 
 
 
 
40
 
41
- ydl_opts = {
42
- 'format': 'bestaudio/best',
43
- 'outtmpl': output,
44
- 'ffmpeg_location': ffmpeg_path,
45
- 'postprocessors': [{
46
- 'key': 'FFmpegExtractAudio',
47
- 'preferredcodec': 'mp3',
48
- 'preferredquality': '192',
49
- }],
50
- 'quiet': False,
51
- 'no_warnings': True,
52
- 'nocheckcertificate': True, # SSL Errors ignore karne ke liye
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
- # 4. Transcription Function
70
- def transcribe(url):
71
- if not url: return "⚠️ URL missing!"
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
- # Beam size 1 is faster for CPU
82
- segments, _ = current_model.transcribe(audio, beam_size=1)
83
  text = " ".join([s.text for s in segments])
84
- return text
 
 
85
  except Exception as e:
86
- return f"Transcription Error: {str(e)}"
87
 
88
- # 5. UI
89
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
90
- gr.Markdown("# πŸš€ TikTok Transcriber (Fixed)")
91
- gr.Markdown("Agar 'Status Code 0' error aaye, to TikTok ne server IP block kiya hai. Dobara try karein.")
92
 
93
  with gr.Row():
94
- link = gr.Textbox(label="TikTok URL", placeholder="Paste https://vt.tiktok.com/... link")
95
- btn = gr.Button("Transcribe", variant="primary")
96
 
97
- out = gr.Textbox(label="Result", lines=10)
98
- btn.click(fn=transcribe, inputs=link, outputs=out)
 
 
 
 
 
 
 
 
 
 
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()