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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -33
app.py CHANGED
@@ -4,37 +4,34 @@ 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
18
 
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,
@@ -48,44 +45,89 @@ def process_audio(url):
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
 
91
  demo.launch()
 
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',
34
+ 'preferredquality': '192',
35
  }],
36
  'quiet': True,
37
  'no_warnings': True,
 
45
  try:
46
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
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:
59
+ # CASE 1: File Upload
60
+ if file_input is not None:
61
+ print(f"πŸ“‚ Processing Uploaded File: {file_input}")
62
+ audio_file_path = file_input
63
+
64
+ # CASE 2: URL Input
65
+ elif url_input and url_input.strip() != "":
66
+ print(f"πŸ”— Processing URL: {url_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,
82
  vad_filter=True
83
  )
84
 
85
  text = " ".join([s.text for s in segments])
86
  return text.strip()
87
+
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://...")
109
+ btn_url = gr.Button("πŸš€ Transcribe Link", variant="primary")
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()