Vishal Patil commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,13 +9,19 @@ UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
|
| 9 |
|
| 10 |
_last_segments = []
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
return f"❌ Error: {e}\n{traceback.format_exc()}", "", ""
|
| 21 |
|
|
@@ -35,10 +41,12 @@ def process_audio(audio_path):
|
|
| 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>
|
|
|
|
| 39 |
</thead><tbody>{rows}</tbody></table>"""
|
| 40 |
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
def export_json():
|
|
@@ -62,9 +70,26 @@ with gr.Blocks(title="Speech Annotation Pipeline") as demo:
|
|
| 62 |
|
| 63 |
with gr.Row():
|
| 64 |
with gr.Column():
|
| 65 |
-
audio_input = gr.Audio(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|
|
@@ -72,12 +97,18 @@ with gr.Blocks(title="Speech Annotation Pipeline") as demo:
|
|
| 72 |
csv_file = gr.File(label="CSV")
|
| 73 |
|
| 74 |
with gr.Column():
|
| 75 |
-
abab_box = gr.Textbox(
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
table_html = gr.HTML(label="Segment Table")
|
| 78 |
|
| 79 |
-
run_btn.click(
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
json_btn.click(fn=export_json, inputs=[], outputs=[json_file])
|
| 82 |
csv_btn.click(fn=export_csv, inputs=[], outputs=[csv_file])
|
| 83 |
|
|
|
|
| 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 |
+
segments = pipeline.process(
|
| 20 |
+
audio_path,
|
| 21 |
+
num_speakers=int(num_speakers) if num_speakers else 0,
|
| 22 |
+
min_speakers=int(min_speakers) if min_speakers else None,
|
| 23 |
+
max_speakers=int(max_speakers) if max_speakers else None,
|
| 24 |
+
)
|
| 25 |
except Exception as e:
|
| 26 |
return f"❌ Error: {e}\n{traceback.format_exc()}", "", ""
|
| 27 |
|
|
|
|
| 41 |
)
|
| 42 |
table = f"""<table style='width:100%;border-collapse:collapse;font-size:14px'>
|
| 43 |
<thead style='background:#1e3a5f;color:white'>
|
| 44 |
+
<tr><th style='padding:8px'>Speaker</th><th>Start</th><th>End</th>
|
| 45 |
+
<th style='text-align:left;padding:8px'>Transcript</th></tr>
|
| 46 |
</thead><tbody>{rows}</tbody></table>"""
|
| 47 |
|
| 48 |
+
unique_speakers = len(set(s.speaker for s in segments))
|
| 49 |
+
return f"✅ Done — {len(segments)} segments, {unique_speakers} speaker(s) detected", abab, table
|
| 50 |
|
| 51 |
|
| 52 |
def export_json():
|
|
|
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column():
|
| 73 |
+
audio_input = gr.Audio(
|
| 74 |
+
label="Upload Audio (.wav/.mp3/.flac)",
|
| 75 |
+
type="filepath"
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
gr.Markdown("### 🎯 Speaker Settings")
|
| 79 |
+
gr.Markdown("*If you know how many speakers, set it below for better accuracy*")
|
| 80 |
+
|
| 81 |
+
num_speakers = gr.Number(
|
| 82 |
+
label="Exact number of speakers (0 = auto-detect)",
|
| 83 |
+
value=0, precision=0
|
| 84 |
+
)
|
| 85 |
+
with gr.Row():
|
| 86 |
+
min_speakers = gr.Number(label="Min speakers", value=None, precision=0)
|
| 87 |
+
max_speakers = gr.Number(label="Max speakers", value=None, precision=0)
|
| 88 |
+
|
| 89 |
run_btn = gr.Button("▶ Run Annotation", variant="primary")
|
| 90 |
status_box = gr.Textbox(label="Status", interactive=False)
|
| 91 |
+
|
| 92 |
+
gr.Markdown("### 📥 Export Results")
|
| 93 |
with gr.Row():
|
| 94 |
json_btn = gr.Button("Export JSON")
|
| 95 |
csv_btn = gr.Button("Export CSV")
|
|
|
|
| 97 |
csv_file = gr.File(label="CSV")
|
| 98 |
|
| 99 |
with gr.Column():
|
| 100 |
+
abab_box = gr.Textbox(
|
| 101 |
+
label="Editable Transcript (ABAB)",
|
| 102 |
+
lines=20,
|
| 103 |
+
placeholder="Annotations will appear here..."
|
| 104 |
+
)
|
| 105 |
table_html = gr.HTML(label="Segment Table")
|
| 106 |
|
| 107 |
+
run_btn.click(
|
| 108 |
+
fn=process_audio,
|
| 109 |
+
inputs=[audio_input, num_speakers, min_speakers, max_speakers],
|
| 110 |
+
outputs=[status_box, abab_box, table_html]
|
| 111 |
+
)
|
| 112 |
json_btn.click(fn=export_json, inputs=[], outputs=[json_file])
|
| 113 |
csv_btn.click(fn=export_csv, inputs=[], outputs=[csv_file])
|
| 114 |
|