Offex commited on
Commit
2f774b2
Β·
verified Β·
1 Parent(s): 2d3b613

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -33
app.py CHANGED
@@ -4,30 +4,38 @@ import os
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',
@@ -47,12 +55,11 @@ def download_audio_from_url(url):
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:
@@ -67,15 +74,15 @@ def transcribe_media(url_input, file_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,
@@ -88,21 +95,19 @@ def transcribe_media(url_input, file_input):
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://...")
@@ -110,24 +115,13 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
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()
 
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 Whisper Model...")
 
14
  model = WhisperModel("base", device="cpu", compute_type="int8")
15
  print("βœ… Model Loaded!")
16
  return model
17
 
18
+ # --- 2. Helper: Find FFmpeg ---
19
+ def get_ffmpeg_dir():
20
+ # System me ffmpeg kahan hai, ye pata lagao
21
+ path = shutil.which("ffmpeg")
22
+ if path:
23
+ return os.path.dirname(path) # Folder ka rasta return karo
24
+ return "/usr/bin" # Default fallback
25
+
26
+ # --- 3. Logic: Download Audio from URL ---
27
  def download_audio_from_url(url):
28
  output_path = "downloaded_audio"
 
29
  if os.path.exists(f"{output_path}.mp3"): os.remove(f"{output_path}.mp3")
30
 
31
+ # Dynamic FFmpeg Path (Ye error fix karega)
32
+ ffmpeg_dir = get_ffmpeg_dir()
33
+ print(f"πŸ”§ FFmpeg found at: {ffmpeg_dir}")
34
 
35
  ydl_opts = {
36
  'format': 'bestaudio/best',
37
  'outtmpl': output_path,
38
+ 'ffmpeg_location': ffmpeg_dir, # <--- FIXED
39
  'postprocessors': [{
40
  'key': 'FFmpegExtractAudio',
41
  'preferredcodec': 'mp3',
 
55
  ydl.download([url])
56
  return f"{output_path}.mp3"
57
  except Exception as e:
58
+ raise Exception(f"Download Fail: {str(e)}")
59
 
60
+ # --- 4. Main Transcribe Function (Handles Both) ---
61
  def transcribe_media(url_input, file_input):
62
 
 
63
  audio_file_path = None
64
 
65
  try:
 
74
  audio_file_path = download_audio_from_url(url_input)
75
 
76
  else:
77
+ return "⚠️ Error: Link daalein ya File upload karein."
78
 
 
79
  if not os.path.exists(audio_file_path):
80
+ return "❌ Error: File nahi mili."
81
 
82
+ # --- Transcribe ---
83
  current_model = load_model()
84
 
85
+ # Turbo Settings: beam_size=1 (Fast)
86
  segments, _ = current_model.transcribe(
87
  audio_file_path,
88
  beam_size=1,
 
95
  except Exception as e:
96
  return f"❌ Error: {str(e)}"
97
 
98
+ # --- 5. UI ---
99
  css = """
100
  .container {max-width: 900px; margin: auto;}
101
  .gr-button-primary {background: linear-gradient(90deg, #1CB5E0 0%, #000851 100%); border: none; color: white;}
 
102
  """
103
 
104
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
105
 
106
  with gr.Column(elem_classes="container"):
107
  gr.Markdown("# πŸš€ Turbo Transcriber (Link & Upload)")
108
+ gr.Markdown("Paste a TikTok link **OR** upload an Audio/Video file.")
109
 
110
  with gr.Tabs():
 
111
  # TAB 1: Link
112
  with gr.TabItem("πŸ”— Paste Link"):
113
  url_in = gr.Textbox(label="TikTok / YouTube URL", placeholder="https://...")
 
115
 
116
  # TAB 2: File Upload
117
  with gr.TabItem("πŸ“‚ Upload File"):
118
+ file_in = gr.Audio(label="Upload File", type="filepath", sources=["upload", "microphone"])
119
  btn_file = gr.Button("πŸ“‚ Transcribe File", variant="primary")
120
 
 
121
  transcript_out = gr.Code(label="Transcript Result", language="markdown", interactive=False, lines=15)
122
 
123
+ # Actions
124
+ btn_url.click(fn=transcribe_media, inputs=[url_in, gr.State(None)], outputs=transcript_out)
125
+ btn_file.click(fn=transcribe_media, inputs=[gr.State(None), file_in], outputs=transcript_out)
 
 
 
 
 
 
 
 
 
 
126
 
127
  demo.launch()