Vishal Patil commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,46 @@
|
|
| 1 |
"""
|
| 2 |
Gradio Web UI for Speech Annotation Pipeline
|
| 3 |
-
Run: python app.py
|
| 4 |
"""
|
| 5 |
|
| 6 |
import json
|
| 7 |
-
import csv
|
| 8 |
-
import io
|
| 9 |
-
import os
|
| 10 |
import tempfile
|
| 11 |
from pathlib import Path
|
| 12 |
-
|
| 13 |
import gradio as gr
|
| 14 |
from pipeline import get_pipeline, to_json, to_csv, to_abab_text
|
| 15 |
|
| 16 |
UPLOAD_DIR = Path(tempfile.gettempdir()) / "speech_annotation"
|
| 17 |
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
_last_segments = [] # module-level cache for export
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
def process_audio(audio_path: str):
|
| 25 |
global _last_segments
|
|
|
|
| 26 |
if audio_path is None:
|
| 27 |
-
return "⚠️ Please upload an audio file.", "",
|
| 28 |
|
| 29 |
try:
|
| 30 |
pipeline = get_pipeline()
|
| 31 |
segments = pipeline.process(audio_path)
|
| 32 |
except EnvironmentError as e:
|
| 33 |
-
return f"❌ Config error:
|
| 34 |
except Exception as e:
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
if not segments:
|
| 38 |
-
return "⚠️ No speech detected.", "",
|
| 39 |
|
| 40 |
_last_segments = segments
|
| 41 |
abab = to_abab_text(segments)
|
| 42 |
|
| 43 |
-
# Build HTML table
|
| 44 |
rows = "".join(
|
| 45 |
-
f"<tr
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
table = f"""
|
| 49 |
<table style='width:100%;border-collapse:collapse;font-family:monospace;font-size:14px'>
|
|
@@ -55,94 +52,69 @@ def process_audio(audio_path: str):
|
|
| 55 |
<th style='padding:8px;text-align:left'>Transcript</th>
|
| 56 |
</tr>
|
| 57 |
</thead>
|
| 58 |
-
<tbody>
|
| 59 |
-
{''.join(f"<tr style='background:{'#f0f4ff' if i%2==0 else 'white'}'>" +
|
| 60 |
-
f"<td style='padding:6px 8px;font-weight:bold;color:#1e3a5f'>{s.speaker}</td>" +
|
| 61 |
-
f"<td style='padding:6px 8px;color:#555'>{s.start_fmt}</td>" +
|
| 62 |
-
f"<td style='padding:6px 8px;color:#555'>{s.end_fmt}</td>" +
|
| 63 |
-
f"<td style='padding:6px 8px'>{s.text}</td></tr>"
|
| 64 |
-
for i, s in enumerate(segments))}
|
| 65 |
-
</tbody>
|
| 66 |
</table>
|
| 67 |
"""
|
| 68 |
|
| 69 |
status = f"✅ Done — {len(segments)} segments detected"
|
| 70 |
-
return status, abab, table
|
| 71 |
|
| 72 |
|
| 73 |
def export_json():
|
| 74 |
if not _last_segments:
|
| 75 |
return None
|
| 76 |
-
out = UPLOAD_DIR / "annotation_export.json"
|
| 77 |
-
to_json(_last_segments,
|
| 78 |
-
return
|
| 79 |
|
| 80 |
|
| 81 |
def export_csv():
|
| 82 |
if not _last_segments:
|
| 83 |
return None
|
| 84 |
-
out = UPLOAD_DIR / "annotation_export.csv"
|
| 85 |
-
to_csv(_last_segments,
|
| 86 |
-
return
|
| 87 |
|
| 88 |
|
| 89 |
-
|
| 90 |
-
CSS = """
|
| 91 |
-
#title { text-align: center; font-size: 28px; font-weight: bold; color: #1e3a5f; margin-bottom: 4px; }
|
| 92 |
-
#subtitle { text-align: center; color: #666; margin-bottom: 20px; }
|
| 93 |
-
.gr-button-primary { background: #1e3a5f !important; }
|
| 94 |
-
"""
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
gr.HTML("<div id='title'>🎙️ Speech Annotation Pipeline</div>")
|
| 99 |
-
gr.HTML("<div id='subtitle'>Upload audio → get speaker-labeled transcripts automatically</div>")
|
| 100 |
|
| 101 |
with gr.Row():
|
| 102 |
with gr.Column(scale=1):
|
| 103 |
audio_input = gr.Audio(
|
| 104 |
label="Upload Audio (.wav / .mp3 / .flac)",
|
| 105 |
-
type="filepath"
|
| 106 |
)
|
| 107 |
-
run_btn = gr.Button("▶ Run Annotation", variant="primary")
|
| 108 |
-
status_box = gr.Textbox(label="Status", interactive=False, lines=
|
| 109 |
|
| 110 |
-
gr.Markdown("###
|
| 111 |
with gr.Row():
|
| 112 |
json_btn = gr.Button("Export JSON")
|
| 113 |
-
csv_btn
|
| 114 |
-
json_file = gr.File(label="JSON Download"
|
| 115 |
-
csv_file
|
| 116 |
|
| 117 |
with gr.Column(scale=2):
|
| 118 |
-
gr.Markdown("###
|
| 119 |
abab_box = gr.Textbox(
|
| 120 |
label="Edit transcript below",
|
| 121 |
lines=20,
|
| 122 |
-
placeholder="Annotations will appear here after processing…"
|
| 123 |
)
|
| 124 |
-
gr.Markdown("###
|
| 125 |
table_html = gr.HTML()
|
| 126 |
|
| 127 |
-
#
|
| 128 |
run_btn.click(
|
| 129 |
fn=process_audio,
|
| 130 |
inputs=[audio_input],
|
| 131 |
-
outputs=[status_box, abab_box, table_html],
|
| 132 |
)
|
| 133 |
-
|
| 134 |
json_btn.click(fn=export_json, inputs=[], outputs=[json_file])
|
| 135 |
-
csv_btn.click(fn=export_csv,
|
| 136 |
-
|
| 137 |
-
gr.Markdown(
|
| 138 |
-
"""
|
| 139 |
-
---
|
| 140 |
-
**Tips:**
|
| 141 |
-
- Set `HF_TOKEN` env var before running (required for speaker diarization)
|
| 142 |
-
- GPU strongly recommended for files > 5 min
|
| 143 |
-
- Edits in the transcript box are yours to save — export when ready
|
| 144 |
-
"""
|
| 145 |
-
)
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
| 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
|
| 10 |
|
| 11 |
UPLOAD_DIR = Path(tempfile.gettempdir()) / "speech_annotation"
|
| 12 |
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
| 13 |
|
| 14 |
+
_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)
|
| 36 |
|
|
|
|
| 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'>
|
|
|
|
| 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)
|