Offex commited on
Commit
980892f
·
verified ·
1 Parent(s): ab99879

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -20
app.py CHANGED
@@ -1,15 +1,17 @@
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
@@ -17,26 +19,22 @@ def load_model():
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',
39
- 'preferredquality': '192',
40
  }],
41
  'quiet': True,
42
  'no_warnings': True,
@@ -47,35 +45,46 @@ def process_audio(url):
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
 
 
1
  import gradio as gr
2
  import yt_dlp
3
  import os
4
+ import shutil
5
  from faster_whisper import WhisperModel
6
 
7
+ # --- 1. Model Setup (Base Model = Sahi Accuracy) ---
8
  model = None
9
 
10
  def load_model():
11
  global model
12
  if model is None:
13
+ print("📥 Loading Base Model (Best Balance)...")
14
+ # 'base' model accuracy aur speed ka perfect mix hai
15
  model = WhisperModel("base", device="cpu", compute_type="int8")
16
  print("✅ Model Loaded!")
17
  return model
 
19
  # --- 2. Process Audio ---
20
  def process_audio(url):
21
  if not url:
22
+ return "⚠️ URL missing!"
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': output_audio,
33
+ 'ffmpeg_location': ffmpeg_dir,
34
  'postprocessors': [{
35
  'key': 'FFmpegExtractAudio',
36
  'preferredcodec': 'mp3',
37
+ 'preferredquality': '192', # Quality wapas badha di taaki shabd saaf sunayi dein
38
  }],
39
  'quiet': True,
40
  'no_warnings': True,
 
45
  }
46
  }
47
 
 
48
  try:
49
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
50
  ydl.download([url])
51
  except Exception as e:
52
+ return f"❌ Download Error: {str(e)}"
53
 
 
54
  if not os.path.exists(f"{output_audio}.mp3"):
55
+ return "❌ Error: Audio file nahi mili."
56
 
57
+ # --- Transcribe (Smart Settings) ---
58
  try:
59
  current_model = load_model()
60
+
61
+ # 'beam_size=1' rakha hai taaki speed tez rahe
62
+ # Lekin model 'base' hai to accuracy acchi rahegi
63
+ segments, _ = current_model.transcribe(
64
+ f"{output_audio}.mp3",
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"Transcription Error: {str(e)}"
73
 
74
  # --- 3. UI ---
75
+ css = """
76
+ .container {max-width: 800px; margin: auto;}
77
+ .gr-button-primary {background-color: #2563eb !important; color: white !important;}
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
+ transcript_out = gr.Code(label="Transcript Result", language="markdown", interactive=False, lines=15)
88
 
89
  btn.click(fn=process_audio, inputs=link_input, outputs=transcript_out)
90