Vishal Patil commited on
Commit
88d0593
·
verified ·
1 Parent(s): 04ba68f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -22
app.py CHANGED
@@ -9,18 +9,14 @@ UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
9
 
10
  _last_segments = []
11
 
12
- def process_audio(audio_path, num_speakers, min_speakers, max_speakers):
13
  global _last_segments
14
  if audio_path is None:
15
  return "⚠️ Please upload an audio file first.", "", ""
16
-
17
  try:
18
  pipeline = get_pipeline()
19
  n = int(num_speakers) if num_speakers and int(num_speakers) > 0 else 0
20
- mn = int(min_speakers) if min_speakers and int(min_speakers) > 0 else None
21
- mx = int(max_speakers) if max_speakers and int(max_speakers) > 0 else None
22
- segments = pipeline.process(audio_path, num_speakers=n,
23
- min_speakers=mn, max_speakers=mx)
24
  except Exception as e:
25
  return f"❌ Error: {e}\n{traceback.format_exc()}", "", ""
26
 
@@ -29,7 +25,6 @@ def process_audio(audio_path, num_speakers, min_speakers, max_speakers):
29
 
30
  _last_segments = segments
31
  abab = to_abab_text(segments)
32
-
33
  rows = "".join(
34
  f"<tr style='background:{'#f0f4ff' if i%2==0 else 'white'}'>"
35
  f"<td style='padding:6px 8px;font-weight:bold;color:#1e3a5f'>{s.speaker}</td>"
@@ -43,7 +38,6 @@ def process_audio(audio_path, num_speakers, min_speakers, max_speakers):
43
  <tr><th style='padding:8px'>Speaker</th><th>Start</th><th>End</th>
44
  <th style='text-align:left;padding:8px'>Transcript</th></tr>
45
  </thead><tbody>{rows}</tbody></table>"""
46
-
47
  unique = len(set(s.speaker for s in segments))
48
  return f"✅ Done — {len(segments)} segments, {unique} speaker(s) detected", abab, table
49
 
@@ -68,32 +62,38 @@ with gr.Blocks(title="Speech Annotation Pipeline") as demo:
68
  gr.Markdown("Upload audio → get speaker-labeled transcripts automatically")
69
 
70
  with gr.Row():
71
- with gr.Column():
72
- audio_input = gr.Audio(label="Upload Audio (.wav/.mp3/.flac)", type="filepath")
73
- gr.Markdown("### 🎯 Speaker Settings")
74
- num_speakers = gr.Slider(minimum=0, maximum=10, step=1, value=0,
75
- label="Number of speakers (0 = auto-detect)")
 
 
 
 
76
  run_btn = gr.Button("▶ Run Annotation", variant="primary")
77
  status_box = gr.Textbox(label="Status", interactive=False)
78
  with gr.Row():
79
  json_btn = gr.Button("Export JSON")
80
  csv_btn = gr.Button("Export CSV")
81
- json_file = gr.File(label="JSON")
82
- csv_file = gr.File(label="CSV")
83
 
84
- with gr.Column():
85
- abab_box = gr.Textbox(label="Editable Transcript (ABAB)", lines=20,
86
- placeholder="Annotations will appear here...")
87
- table_html = gr.HTML(label="Segment Table")
 
 
 
88
 
89
  run_btn.click(
90
  fn=process_audio,
91
- inputs=[audio_input, num_speakers,
92
- gr.State(0), gr.State(0)],
93
  outputs=[status_box, abab_box, table_html]
94
  )
95
  json_btn.click(fn=export_json, inputs=[], outputs=[json_file])
96
- csv_btn.click(fn=export_csv, inputs=[], outputs=[csv_file])
97
 
98
  if __name__ == "__main__":
99
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
9
 
10
  _last_segments = []
11
 
12
+ def process_audio(audio_path, num_speakers):
13
  global _last_segments
14
  if audio_path is None:
15
  return "⚠️ Please upload an audio file first.", "", ""
 
16
  try:
17
  pipeline = get_pipeline()
18
  n = int(num_speakers) if num_speakers and int(num_speakers) > 0 else 0
19
+ segments = pipeline.process(audio_path, num_speakers=n)
 
 
 
20
  except Exception as e:
21
  return f"❌ Error: {e}\n{traceback.format_exc()}", "", ""
22
 
 
25
 
26
  _last_segments = segments
27
  abab = to_abab_text(segments)
 
28
  rows = "".join(
29
  f"<tr style='background:{'#f0f4ff' if i%2==0 else 'white'}'>"
30
  f"<td style='padding:6px 8px;font-weight:bold;color:#1e3a5f'>{s.speaker}</td>"
 
38
  <tr><th style='padding:8px'>Speaker</th><th>Start</th><th>End</th>
39
  <th style='text-align:left;padding:8px'>Transcript</th></tr>
40
  </thead><tbody>{rows}</tbody></table>"""
 
41
  unique = len(set(s.speaker for s in segments))
42
  return f"✅ Done — {len(segments)} segments, {unique} speaker(s) detected", abab, table
43
 
 
62
  gr.Markdown("Upload audio → get speaker-labeled transcripts automatically")
63
 
64
  with gr.Row():
65
+ with gr.Column(scale=1):
66
+ audio_input = gr.Audio(
67
+ label="Upload Audio (.wav/.mp3/.flac)",
68
+ type="filepath"
69
+ )
70
+ num_speakers = gr.Slider(
71
+ minimum=0, maximum=10, step=1, value=0,
72
+ label="Number of speakers (0 = auto-detect)"
73
+ )
74
  run_btn = gr.Button("▶ Run Annotation", variant="primary")
75
  status_box = gr.Textbox(label="Status", interactive=False)
76
  with gr.Row():
77
  json_btn = gr.Button("Export JSON")
78
  csv_btn = gr.Button("Export CSV")
79
+ json_file = gr.File(label="JSON Download")
80
+ csv_file = gr.File(label="CSV Download")
81
 
82
+ with gr.Column(scale=2):
83
+ abab_box = gr.Textbox(
84
+ label="Editable Transcript (ABAB)",
85
+ lines=20,
86
+ placeholder="Annotations will appear here..."
87
+ )
88
+ table_html = gr.HTML()
89
 
90
  run_btn.click(
91
  fn=process_audio,
92
+ inputs=[audio_input, num_speakers],
 
93
  outputs=[status_box, abab_box, table_html]
94
  )
95
  json_btn.click(fn=export_json, inputs=[], outputs=[json_file])
96
+ csv_btn.click(fn=export_csv, inputs=[], outputs=[csv_file])
97
 
98
  if __name__ == "__main__":
99
  demo.launch(server_name="0.0.0.0", server_port=7860)