Vishal Patil commited on
Commit
3bb4a6a
·
verified ·
1 Parent(s): a04aea6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -21
app.py CHANGED
@@ -2,7 +2,18 @@ import tempfile
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)
@@ -19,16 +30,13 @@ def process_audio(audio_path, num_speakers):
19
  segments = pipeline.process(audio_path, num_speakers=n)
20
  except Exception as e:
21
  return f"❌ Error: {e}\n{traceback.format_exc()}", ""
22
-
23
  if not segments:
24
  return "⚠️ No speech detected.", ""
25
-
26
  _last_segments = segments
27
  abab = to_abab_text(segments)
28
  unique = len(set(s.speaker for s in segments))
29
  return f"✅ Done — {len(segments)} segments, {unique} speaker(s) detected", abab
30
 
31
-
32
  def export_json():
33
  if not _last_segments:
34
  return None
@@ -43,31 +51,26 @@ def export_csv():
43
  to_csv(_last_segments, out)
44
  return out
45
 
46
-
47
  with gr.Blocks(title="Speech Annotation Pipeline") as demo:
48
  gr.Markdown("# 🎙️ Speech Annotation Pipeline")
49
- gr.Markdown("Upload audio → get speaker-labeled transcripts automatically")
50
-
51
- audio_input = gr.Audio(label="Upload Audio (.wav/.mp3/.flac)", type="filepath")
52
  num_speakers = gr.Slider(minimum=0, maximum=10, step=1, value=0,
53
  label="Number of speakers (0 = auto-detect)")
54
- run_btn = gr.Button("▶ Run Annotation", variant="primary")
55
- status_box = gr.Textbox(label="Status", interactive=False)
56
- abab_box = gr.Textbox(label="Editable Transcript (ABAB)", lines=20,
57
- placeholder="Annotations will appear here...")
58
  with gr.Row():
59
  json_btn = gr.Button("Export JSON")
60
  csv_btn = gr.Button("Export CSV")
61
- json_file = gr.File(label="JSON Download")
62
- csv_file = gr.File(label="CSV Download")
63
 
64
- run_btn.click(
65
- fn=process_audio,
66
- inputs=[audio_input, num_speakers],
67
- outputs=[status_box, abab_box]
68
- )
69
  json_btn.click(fn=export_json, inputs=[], outputs=[json_file])
70
- csv_btn.click(fn=export_csv, inputs=[], outputs=[csv_file])
71
 
72
  if __name__ == "__main__":
73
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
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)
 
30
  segments = pipeline.process(audio_path, num_speakers=n)
31
  except Exception as e:
32
  return f"❌ Error: {e}\n{traceback.format_exc()}", ""
 
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
 
51
  to_csv(_last_segments, out)
52
  return out
53
 
 
54
  with gr.Blocks(title="Speech Annotation Pipeline") as demo:
55
  gr.Markdown("# 🎙️ Speech Annotation Pipeline")
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)