testingfaces commited on
Commit
dde471f
Β·
verified Β·
1 Parent(s): d8d0e03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -70
app.py CHANGED
@@ -15,98 +15,96 @@ import numpy as np
15
 
16
  print("ClearWave AI starting...")
17
 
18
- INPUT_LANGS = ["Auto Detect","English","Telugu","Hindi","Tamil","Kannada"]
19
- OUTPUT_LANGS = ["Telugu","Hindi","Tamil","English","Kannada"]
20
- LANG_CODES = {"Auto Detect":"auto","English":"en","Telugu":"te","Hindi":"hi","Tamil":"ta","Kannada":"kn"}
21
-
22
- def denoise(audio_path, out_dir):
23
- import soundfile as sf
24
- import noisereduce as nr
25
- wav = os.path.join(out_dir, "input.wav")
26
- subprocess.run(["ffmpeg","-y","-i",audio_path,"-ar","16000","-ac","1",wav], capture_output=True)
27
- data, sr = sf.read(wav)
28
- data = data.astype(np.float32)
29
- try:
30
- cleaned = nr.reduce_noise(y=data, sr=sr).astype(np.float32)
31
- except Exception:
32
- cleaned = data
33
- peak = np.abs(cleaned).max()
34
- if peak > 0:
35
- cleaned = cleaned / peak * 0.9
36
- out = os.path.join(out_dir, "denoised.wav")
37
- sf.write(out, cleaned, sr)
38
- return out
39
-
40
- def transcribe(audio_path, language="auto"):
41
- groq_key = os.environ.get("GROQ_API_KEY","")
42
- if not groq_key:
43
- return "No GROQ_API_KEY set. Add it in Space Settings Secrets.", "en", "no key"
44
- from groq import Groq
45
- client = Groq(api_key=groq_key)
46
- with open(audio_path, "rb") as f:
47
- kwargs = dict(file=("audio.wav", f, "audio/wav"),
48
- model="whisper-large-v3",
49
- response_format="verbose_json",
50
- temperature=0.0)
51
- if language and language != "auto":
52
- kwargs["language"] = language
53
- resp = client.audio.transcriptions.create(**kwargs)
54
- text = resp.text.strip()
55
- lang = getattr(resp, "language", None) or language or "en"
56
- lang_map = {"english":"en","telugu":"te","hindi":"hi","tamil":"ta","kannada":"kn"}
57
- lang = lang_map.get(lang.lower(), lang[:2].lower() if len(lang) >= 2 else "en")
58
- return text, lang, "Groq Whisper large-v3"
59
-
60
- def translate(text, src, tgt):
61
- if not text.strip() or src == tgt:
62
- return text, "skipped"
63
- try:
64
- from deep_translator import GoogleTranslator
65
- return GoogleTranslator(source=src, target=tgt).translate(text), "Google Translate"
66
- except Exception as e:
67
- return f"Translation error: {e}", "error"
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  def process(audio_path, in_lang_label, out_lang_label, progress=gr.Progress()):
70
  if audio_path is None:
71
  return None, "Please upload audio.", "", "", "No audio"
 
72
  in_lang = LANG_CODES.get(in_lang_label, "auto")
73
  out_lang = LANG_CODES.get(out_lang_label, "te")
74
- tmp = tempfile.mkdtemp()
75
- t_total = time.time()
 
76
  try:
 
77
  progress(0.1, desc="Dept 1: Denoising...")
78
- t0 = time.time(); clean = denoise(audio_path, tmp); t1 = time.time()-t0
 
 
79
 
 
80
  progress(0.4, desc="Dept 2: Transcribing...")
81
- t0 = time.time(); transcript, detected, tx_m = transcribe(clean, in_lang); t2 = time.time()-t0
 
 
82
 
 
83
  progress(0.75, desc="Dept 3: Translating...")
84
- src = detected if in_lang == "auto" else in_lang
85
- t0 = time.time(); translated, tr_m = translate(transcript, src, out_lang); t3 = time.time()-t0
 
 
86
 
87
- total = time.time()-t_total
88
  progress(1.0, desc=f"Done in {total:.1f}s!")
89
- timing = (f"| Step | Time | Method |\n|---|---|---|\n"
90
- f"| Denoise | {t1:.1f}s | noisereduce |\n"
91
- f"| Transcribe | {t2:.1f}s | {tx_m} |\n"
92
- f"| Translate | {t3:.1f}s | {tr_m} |\n"
93
- f"| **Total** | **{total:.1f}s** | |")
 
 
 
 
94
  out_audio = os.path.join(tmp, "output.wav")
95
  shutil.copy(clean, out_audio)
96
  return out_audio, transcript, translated, timing, f"Done in {total:.1f}s"
 
97
  except Exception as e:
98
  import traceback
99
  return None, f"Error: {e}", "", traceback.format_exc(), "Failed"
100
 
 
 
101
  with gr.Blocks(title="ClearWave AI", theme=gr.themes.Soft()) as demo:
102
- gr.Markdown("# ClearWave AI\n**Denoise - Transcribe - Translate**")
 
103
  with gr.Row():
104
  with gr.Column(scale=1):
105
- audio_in = gr.Audio(label="Upload Audio", type="filepath", sources=["upload","microphone"])
106
- in_lang = gr.Dropdown(INPUT_LANGS, value="Auto Detect", label="Input Language")
107
- out_lang = gr.Dropdown(OUTPUT_LANGS, value="Telugu", label="Output Language")
108
- run_btn = gr.Button("Process Audio", variant="primary", size="lg")
109
- status = gr.Markdown("Upload audio and click Process.")
 
 
 
 
 
110
  with gr.Column(scale=2):
111
  with gr.Tabs():
112
  with gr.Tab("Text"):
@@ -118,15 +116,20 @@ with gr.Blocks(title="ClearWave AI", theme=gr.themes.Soft()) as demo:
118
  gr.Markdown("#### Translation")
119
  translation_out = gr.Markdown("...")
120
  with gr.Tab("Clean Audio"):
121
- audio_out = gr.Audio(label="Denoised", type="filepath", interactive=False)
 
 
 
 
122
  with gr.Tab("Timings"):
123
  timing_out = gr.Markdown("...")
 
124
  run_btn.click(
125
  fn=process,
126
  inputs=[audio_in, in_lang, out_lang],
127
  outputs=[audio_out, transcript_out, translation_out, timing_out, status],
128
  show_progress=True,
129
- api_name=False, # Fixes: TypeError: argument of type 'bool' is not iterable
130
  )
131
 
132
  print("ClearWave AI ready!")
 
15
 
16
  print("ClearWave AI starting...")
17
 
18
+ # ── Services ────────────────────────────────────────────────────────
19
+ from services.denoiser import Denoiser
20
+ from services.transcriber import Transcriber
21
+ # βœ… FIX: Now using the full Translator class (NLLB-1.3B + Google fallback)
22
+ # Previously app.py had its own inline translate() that only used
23
+ # Google Translate and completely ignored translator.py
24
+ from services.translator import Translator
25
+
26
+ _denoiser = Denoiser()
27
+ _transcriber = Transcriber()
28
+ _translator = Translator()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
+ # ── Config ───────────────────────────────────────────────────────────
31
+ INPUT_LANGS = ["Auto Detect", "English", "Telugu", "Hindi", "Tamil", "Kannada"]
32
+ OUTPUT_LANGS = ["Telugu", "Hindi", "Tamil", "English", "Kannada"]
33
+ LANG_CODES = {
34
+ "Auto Detect": "auto",
35
+ "English": "en",
36
+ "Telugu": "te",
37
+ "Hindi": "hi",
38
+ "Tamil": "ta",
39
+ "Kannada": "kn",
40
+ }
41
+
42
+ # ── Pipeline ─────────────────────────────────────────────────────────
43
  def process(audio_path, in_lang_label, out_lang_label, progress=gr.Progress()):
44
  if audio_path is None:
45
  return None, "Please upload audio.", "", "", "No audio"
46
+
47
  in_lang = LANG_CODES.get(in_lang_label, "auto")
48
  out_lang = LANG_CODES.get(out_lang_label, "te")
49
+ tmp = tempfile.mkdtemp()
50
+ t_total = time.time()
51
+
52
  try:
53
+ # Dept 1 β€” Denoise
54
  progress(0.1, desc="Dept 1: Denoising...")
55
+ t0 = time.time()
56
+ clean = _denoiser.process(audio_path, tmp)
57
+ t1 = time.time() - t0
58
 
59
+ # Dept 2 β€” Transcribe
60
  progress(0.4, desc="Dept 2: Transcribing...")
61
+ t0 = time.time()
62
+ transcript, detected, tx_m = _transcriber.transcribe(clean, in_lang)
63
+ t2 = time.time() - t0
64
 
65
+ # Dept 3 β€” Translate
66
  progress(0.75, desc="Dept 3: Translating...")
67
+ src = detected if in_lang == "auto" else in_lang
68
+ t0 = time.time()
69
+ translated, tr_m = _translator.translate(transcript, src, out_lang)
70
+ t3 = time.time() - t0
71
 
72
+ total = time.time() - t_total
73
  progress(1.0, desc=f"Done in {total:.1f}s!")
74
+
75
+ timing = (
76
+ f"| Step | Time | Method |\n|---|---|---|\n"
77
+ f"| Denoise | {t1:.1f}s | noisereduce |\n"
78
+ f"| Transcribe | {t2:.1f}s | {tx_m} |\n"
79
+ f"| Translate | {t3:.1f}s | {tr_m} |\n"
80
+ f"| **Total** | **{total:.1f}s** | |"
81
+ )
82
+
83
  out_audio = os.path.join(tmp, "output.wav")
84
  shutil.copy(clean, out_audio)
85
  return out_audio, transcript, translated, timing, f"Done in {total:.1f}s"
86
+
87
  except Exception as e:
88
  import traceback
89
  return None, f"Error: {e}", "", traceback.format_exc(), "Failed"
90
 
91
+
92
+ # ── UI ───────────────────────────────────────────────────────────────
93
  with gr.Blocks(title="ClearWave AI", theme=gr.themes.Soft()) as demo:
94
+ gr.Markdown("# 🎡 ClearWave AI\n**Denoise · Transcribe · Translate**")
95
+
96
  with gr.Row():
97
  with gr.Column(scale=1):
98
+ audio_in = gr.Audio(
99
+ label="Upload Audio",
100
+ type="filepath",
101
+ sources=["upload", "microphone"],
102
+ )
103
+ in_lang = gr.Dropdown(INPUT_LANGS, value="Auto Detect", label="Input Language")
104
+ out_lang = gr.Dropdown(OUTPUT_LANGS, value="Telugu", label="Output Language")
105
+ run_btn = gr.Button("Process Audio", variant="primary", size="lg")
106
+ status = gr.Markdown("Upload audio and click Process.")
107
+
108
  with gr.Column(scale=2):
109
  with gr.Tabs():
110
  with gr.Tab("Text"):
 
116
  gr.Markdown("#### Translation")
117
  translation_out = gr.Markdown("...")
118
  with gr.Tab("Clean Audio"):
119
+ audio_out = gr.Audio(
120
+ label="Denoised",
121
+ type="filepath",
122
+ interactive=False,
123
+ )
124
  with gr.Tab("Timings"):
125
  timing_out = gr.Markdown("...")
126
+
127
  run_btn.click(
128
  fn=process,
129
  inputs=[audio_in, in_lang, out_lang],
130
  outputs=[audio_out, transcript_out, translation_out, timing_out, status],
131
  show_progress=True,
132
+ api_name=False,
133
  )
134
 
135
  print("ClearWave AI ready!")