Vishal Patil commited on
Commit
ccbedf2
·
verified ·
1 Parent(s): a63a504

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -64
app.py CHANGED
@@ -1,9 +1,5 @@
1
- """
2
- Gradio Web UI for Speech Annotation Pipeline
3
- """
4
-
5
- import json
6
  import tempfile
 
7
  from pathlib import Path
8
  import gradio as gr
9
  from pipeline import get_pipeline, to_json, to_csv, to_abab_text
@@ -15,21 +11,16 @@ _last_segments = []
15
 
16
  def process_audio(audio_path):
17
  global _last_segments
18
-
19
  if audio_path is None:
20
- return "⚠️ Please upload an audio file first.", "", "", None
21
-
22
  try:
23
  pipeline = get_pipeline()
24
  segments = pipeline.process(audio_path)
25
- except EnvironmentError as e:
26
- return f"❌ Config error: {e}", "", "", None
27
  except Exception as e:
28
- import traceback
29
- return f"❌ Error: {e}\n{traceback.format_exc()}", "", "", None
30
 
31
  if not segments:
32
- return "⚠️ No speech detected.", "", "", None
33
 
34
  _last_segments = segments
35
  abab = to_abab_text(segments)
@@ -37,84 +28,58 @@ def process_audio(audio_path):
37
  rows = "".join(
38
  f"<tr style='background:{'#f0f4ff' if i%2==0 else 'white'}'>"
39
  f"<td style='padding:6px 8px;font-weight:bold;color:#1e3a5f'>{s.speaker}</td>"
40
- f"<td style='padding:6px 8px;color:#555'>{s.start_fmt}</td>"
41
- f"<td style='padding:6px 8px;color:#555'>{s.end_fmt}</td>"
42
  f"<td style='padding:6px 8px'>{s.text}</td></tr>"
43
  for i, s in enumerate(segments)
44
  )
45
- table = f"""
46
- <table style='width:100%;border-collapse:collapse;font-family:monospace;font-size:14px'>
47
  <thead style='background:#1e3a5f;color:white'>
48
- <tr>
49
- <th style='padding:8px;text-align:left'>Speaker</th>
50
- <th style='padding:8px'>Start</th>
51
- <th style='padding:8px'>End</th>
52
- <th style='padding:8px;text-align:left'>Transcript</th>
53
- </tr>
54
- </thead>
55
- <tbody>{rows}</tbody>
56
- </table>
57
- """
58
 
59
- status = f"✅ Done — {len(segments)} segments detected"
60
- return status, abab, table, None
61
 
62
 
63
  def export_json():
64
  if not _last_segments:
65
  return None
66
- out = str(UPLOAD_DIR / "annotation_export.json")
67
  to_json(_last_segments, out)
68
  return out
69
 
70
-
71
  def export_csv():
72
  if not _last_segments:
73
  return None
74
- out = str(UPLOAD_DIR / "annotation_export.csv")
75
  to_csv(_last_segments, out)
76
  return out
77
 
78
 
79
  with gr.Blocks(title="Speech Annotation Pipeline") as demo:
80
-
81
- gr.HTML("<h1 style='text-align:center;color:#1e3a5f'>🎙️ Speech Annotation Pipeline</h1>")
82
- gr.HTML("<p style='text-align:center;color:#666'>Upload audio → get speaker-labeled transcripts automatically</p>")
83
 
84
  with gr.Row():
85
- with gr.Column(scale=1):
86
- audio_input = gr.Audio(
87
- label="Upload Audio (.wav / .mp3 / .flac)",
88
- type="filepath"
89
- )
90
- run_btn = gr.Button("▶ Run Annotation", variant="primary", size="lg")
91
- status_box = gr.Textbox(label="Status", interactive=False, lines=2)
92
-
93
- gr.Markdown("### Export Results")
94
  with gr.Row():
95
  json_btn = gr.Button("Export JSON")
96
  csv_btn = gr.Button("Export CSV")
97
- json_file = gr.File(label="JSON Download")
98
- csv_file = gr.File(label="CSV Download")
99
-
100
- with gr.Column(scale=2):
101
- gr.Markdown("### Editable Transcript (ABAB format)")
102
- abab_box = gr.Textbox(
103
- label="Edit transcript below",
104
- lines=20,
105
- placeholder="Annotations will appear here after processing…"
106
- )
107
- gr.Markdown("### Segment Table")
108
- table_html = gr.HTML()
109
-
110
- # Wire up button clicks
111
- run_btn.click(
112
- fn=process_audio,
113
- inputs=[audio_input],
114
- outputs=[status_box, abab_box, table_html, json_file],
115
- )
116
  json_btn.click(fn=export_json, inputs=[], outputs=[json_file])
117
  csv_btn.click(fn=export_csv, inputs=[], outputs=[csv_file])
118
 
119
  if __name__ == "__main__":
120
- demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
 
 
 
 
 
 
1
  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
 
11
 
12
  def process_audio(audio_path):
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
  segments = pipeline.process(audio_path)
 
 
19
  except Exception as e:
20
+ return f"❌ Error: {e}\n{traceback.format_exc()}", "", ""
 
21
 
22
  if not segments:
23
+ return "⚠️ No speech detected.", "", ""
24
 
25
  _last_segments = segments
26
  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>"
31
+ f"<td style='padding:6px 8px'>{s.start_fmt}</td>"
32
+ f"<td style='padding:6px 8px'>{s.end_fmt}</td>"
33
  f"<td style='padding:6px 8px'>{s.text}</td></tr>"
34
  for i, s in enumerate(segments)
35
  )
36
+ table = f"""<table style='width:100%;border-collapse:collapse;font-size:14px'>
 
37
  <thead style='background:#1e3a5f;color:white'>
38
+ <tr><th style='padding:8px'>Speaker</th><th>Start</th><th>End</th><th style='text-align:left;padding:8px'>Transcript</th></tr>
39
+ </thead><tbody>{rows}</tbody></table>"""
 
 
 
 
 
 
 
 
40
 
41
+ return f"✅ Done — {len(segments)} segments", abab, table
 
42
 
43
 
44
  def export_json():
45
  if not _last_segments:
46
  return None
47
+ out = str(UPLOAD_DIR / "annotation.json")
48
  to_json(_last_segments, out)
49
  return out
50
 
 
51
  def export_csv():
52
  if not _last_segments:
53
  return None
54
+ out = str(UPLOAD_DIR / "annotation.csv")
55
  to_csv(_last_segments, out)
56
  return out
57
 
58
 
59
  with gr.Blocks(title="Speech Annotation Pipeline") as demo:
60
+ gr.Markdown("# 🎙️ Speech Annotation Pipeline")
61
+ gr.Markdown("Upload audio → get speaker-labeled transcripts automatically")
 
62
 
63
  with gr.Row():
64
+ with gr.Column():
65
+ audio_input = gr.Audio(label="Upload Audio (.wav/.mp3/.flac)", type="filepath")
66
+ run_btn = gr.Button("▶ Run Annotation", variant="primary")
67
+ status_box = gr.Textbox(label="Status", interactive=False)
 
 
 
 
 
68
  with gr.Row():
69
  json_btn = gr.Button("Export JSON")
70
  csv_btn = gr.Button("Export CSV")
71
+ json_file = gr.File(label="JSON")
72
+ csv_file = gr.File(label="CSV")
73
+
74
+ with gr.Column():
75
+ abab_box = gr.Textbox(label="Editable Transcript (ABAB)", lines=20,
76
+ placeholder="Annotations will appear here...")
77
+ table_html = gr.HTML(label="Segment Table")
78
+
79
+ run_btn.click(fn=process_audio, inputs=[audio_input],
80
+ outputs=[status_box, abab_box, table_html])
 
 
 
 
 
 
 
 
 
81
  json_btn.click(fn=export_json, inputs=[], outputs=[json_file])
82
  csv_btn.click(fn=export_csv, inputs=[], outputs=[csv_file])
83
 
84
  if __name__ == "__main__":
85
+ demo.launch(server_name="0.0.0.0", server_port=7860)