Vishal Patil commited on
Commit
02642d5
·
verified ·
1 Parent(s): 5db6b38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -26
app.py CHANGED
@@ -2,22 +2,10 @@ import tempfile
2
  import traceback
3
  from pathlib import Path
4
  import gradio as gr
5
- import importlib.util, sys
6
-
7
- # Load pipeline module explicitly to avoid circular import
8
- spec = importlib.util.spec_from_file_location("speech_pipeline", "/app/pipeline.py")
9
- mod = importlib.util.module_from_spec(spec)
10
- sys.modules["speech_pipeline"] = mod
11
- spec.loader.exec_module(mod)
12
-
13
- get_pipeline = mod.get_pipeline
14
- to_json = mod.to_json
15
- to_csv = mod.to_csv
16
- to_abab_text = mod.to_abab_text
17
 
18
  UPLOAD_DIR = Path(tempfile.gettempdir()) / "speech_annotation"
19
  UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
20
-
21
  _last_segments = []
22
 
23
  def process_audio(audio_path, num_speakers):
@@ -33,20 +21,17 @@ def process_audio(audio_path, num_speakers):
33
  if not segments:
34
  return "⚠️ No speech detected.", ""
35
  _last_segments = segments
36
- abab = to_abab_text(segments)
37
  unique = len(set(s.speaker for s in segments))
38
- return f"✅ Done — {len(segments)} segments, {unique} speaker(s) detected", abab
39
 
40
  def export_json():
41
- if not _last_segments:
42
- return None
43
  out = str(UPLOAD_DIR / "annotation.json")
44
  to_json(_last_segments, out)
45
  return out
46
 
47
  def export_csv():
48
- if not _last_segments:
49
- return None
50
  out = str(UPLOAD_DIR / "annotation.csv")
51
  to_csv(_last_segments, out)
52
  return out
@@ -56,21 +41,20 @@ with gr.Blocks(title="Speech Annotation Pipeline") as demo:
56
  audio_input = gr.Audio(label="Upload Audio (.wav/.mp3/.flac)", type="filepath")
57
  num_speakers = gr.Slider(minimum=0, maximum=10, step=1, value=0,
58
  label="Number of speakers (0 = auto-detect)")
59
- run_btn = gr.Button("▶ Run Annotation", variant="primary")
60
- status_box = gr.Textbox(label="Status", value="Ready.", interactive=False)
61
- abab_box = gr.Textbox(label="Transcript (ABAB)", lines=15, value="",
62
- placeholder="Annotations will appear here...")
63
  with gr.Row():
64
  json_btn = gr.Button("Export JSON")
65
  csv_btn = gr.Button("Export CSV")
66
  json_file = gr.File(label="JSON")
67
  csv_file = gr.File(label="CSV")
68
 
69
- run_btn.click(fn=process_audio,
70
- inputs=[audio_input, num_speakers],
71
  outputs=[status_box, abab_box])
72
  json_btn.click(fn=export_json, inputs=[], outputs=[json_file])
73
- csv_btn.click(fn=export_csv, inputs=[], outputs=[csv_file])
74
 
75
  if __name__ == "__main__":
76
  demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)
 
2
  import traceback
3
  from pathlib import Path
4
  import gradio as gr
5
+ from pipeline import get_pipeline, to_json, to_csv, to_abab_text
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  UPLOAD_DIR = Path(tempfile.gettempdir()) / "speech_annotation"
8
  UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
 
9
  _last_segments = []
10
 
11
  def process_audio(audio_path, num_speakers):
 
21
  if not segments:
22
  return "⚠️ No speech detected.", ""
23
  _last_segments = segments
 
24
  unique = len(set(s.speaker for s in segments))
25
+ return f"✅ Done — {len(segments)} segments, {unique} speaker(s) detected", to_abab_text(segments)
26
 
27
  def export_json():
28
+ if not _last_segments: return None
 
29
  out = str(UPLOAD_DIR / "annotation.json")
30
  to_json(_last_segments, out)
31
  return out
32
 
33
  def export_csv():
34
+ if not _last_segments: return None
 
35
  out = str(UPLOAD_DIR / "annotation.csv")
36
  to_csv(_last_segments, out)
37
  return out
 
41
  audio_input = gr.Audio(label="Upload Audio (.wav/.mp3/.flac)", type="filepath")
42
  num_speakers = gr.Slider(minimum=0, maximum=10, step=1, value=0,
43
  label="Number of speakers (0 = auto-detect)")
44
+ run_btn = gr.Button("▶ Run Annotation", variant="primary")
45
+ status_box = gr.Textbox(label="Status", value="Ready.", interactive=False)
46
+ abab_box = gr.Textbox(label="Transcript (ABAB)", lines=15, value="",
47
+ placeholder="Annotations will appear here...")
48
  with gr.Row():
49
  json_btn = gr.Button("Export JSON")
50
  csv_btn = gr.Button("Export CSV")
51
  json_file = gr.File(label="JSON")
52
  csv_file = gr.File(label="CSV")
53
 
54
+ run_btn.click(fn=process_audio, inputs=[audio_input, num_speakers],
 
55
  outputs=[status_box, abab_box])
56
  json_btn.click(fn=export_json, inputs=[], outputs=[json_file])
57
+ csv_btn.click(fn=export_csv, inputs=[], outputs=[csv_file])
58
 
59
  if __name__ == "__main__":
60
  demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)