LapisLa2uli commited on
Commit
081ffc0
·
verified ·
1 Parent(s): 6c7cabd

Initial commit

Browse files
Files changed (2) hide show
  1. app.py +322 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,322 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import os
3
+ import json
4
+ import http.client
5
+ from io import BytesIO
6
+
7
+ import gradio as gr
8
+ from dotenv import load_dotenv
9
+ from elevenlabs.client import ElevenLabs
10
+
11
+ # ----------------------------
12
+ # Config & clients
13
+ # ----------------------------
14
+ load_dotenv() # supports local .env; on HF Spaces, set secrets in the UI
15
+
16
+ ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY", "")
17
+ API_KEY_302 = os.getenv("API_KEY_302", "")
18
+
19
+ # ElevenLabs client (only if key is present)
20
+ elevenlabs_client = None
21
+ if ELEVENLABS_API_KEY:
22
+ elevenlabs_client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
23
+
24
+ # ----------------------------
25
+ # Prompt templates
26
+ # ----------------------------
27
+ PROMPT_TEMPLATE_1 = """\
28
+ You are a speech-language assistant. Given the ORIGINAL script and the TRANSCRIPT (imperfect ASR),
29
+ list words/phrases likely to trigger stuttering (e.g., consonant clusters, long multisyllabic words).
30
+ Output a short, structured summary and diagnosis for easy-to-stutter scenarios.
31
+
32
+ ORIGINAL:
33
+ {original_text}
34
+
35
+ TRANSCRIPT:
36
+ {transcribed_text}
37
+
38
+ Never give any suggestion. Only return a concise, principled diagnosis notes with easy-to-stutter scenarios.
39
+ """
40
+
41
+ PROMPT_TEMPLATE_2 = """\
42
+ You are a speech-language assistant. Rewrite the ORIGINAL script to reduce stuttering risk, while
43
+ preserving meaning and tone. Prefer simpler synonyms, shorter clauses, easier onsets. Keep it concise.
44
+
45
+ Diagnosis notes on easy-to-stutter scenarios:
46
+ {notes}
47
+
48
+ ORIGINAL:
49
+ {original_text}
50
+
51
+ Only return the revised full script, nothing else.
52
+ """
53
+
54
+ # New: IPA-only prompt (Baseline+IPA, step 1)
55
+ PROMPT_TEMPLATE_IPA = """\
56
+ Convert BOTH the ORIGINAL script and the ASR TRANSCRIPT into IPA with syllable boundaries.
57
+ Return ONLY the IPA text in a clearly labeled, compact format, such as:
58
+
59
+ ORIGINAL_IPA:
60
+ <ipa for original with syllable markers>
61
+
62
+ TRANSCRIPT_IPA:
63
+ <ipa for transcript with syllable markers>
64
+
65
+ Do not include any additional commentary.
66
+
67
+ ORIGINAL:
68
+ {original_text}
69
+
70
+ TRANSCRIPT:
71
+ {transcribed_text}
72
+ """
73
+
74
+ # New: Diagnosis that uses IPA as extra signal (Baseline+IPA, step 2)
75
+ PROMPT_TEMPLATE_1_WITH_IPA = """\
76
+ You are a speech-language assistant. Given the ORIGINAL script, the TRANSCRIPT (imperfect ASR),
77
+ and their IPA annotations, list words/phrases likely to trigger stuttering (e.g., consonant clusters,
78
+ long multisyllabic words, difficult onsets). Output a short, structured summary and diagnosis for
79
+ easy-to-stutter scenarios.
80
+
81
+ ORIGINAL:
82
+ {original_text}
83
+
84
+ TRANSCRIPT:
85
+ {transcribed_text}
86
+
87
+ IPA_ANNOTATIONS:
88
+ {ipa_text}
89
+
90
+ Never give any suggestion. Only return a concise, principled diagnosis notes with easy-to-stutter scenarios.
91
+ """
92
+
93
+ # ----------------------------
94
+ # Helpers: STT & LLM calls
95
+ # ----------------------------
96
+ def transcribe_audio(record_path: str | None) -> str:
97
+ """
98
+ Returns the transcribed text (or an error message).
99
+ """
100
+ audio_path = record_path
101
+ if not audio_path:
102
+ return "No audio provided. Please upload or record audio."
103
+
104
+ if not ELEVENLABS_API_KEY:
105
+ return "ELEVENLABS_API_KEY not set. Please configure your environment."
106
+
107
+ try:
108
+ with open(audio_path, "rb") as f:
109
+ audio_data = BytesIO(f.read())
110
+ except Exception as e:
111
+ return f"Failed to read audio: {e}"
112
+
113
+ try:
114
+ transcription = elevenlabs_client.speech_to_text.convert(
115
+ file=audio_data,
116
+ model_id="scribe_v1",
117
+ tag_audio_events=True,
118
+ language_code="eng",
119
+ diarize=True,
120
+ )
121
+ return transcription.text or ""
122
+ except Exception as e:
123
+ return f"Transcription error: {e}"
124
+
125
+ def call_llm_302(model: str, prompt: str) -> str:
126
+ """
127
+ Minimal wrapper around 302.ai /v1/chat/completions.
128
+ Returns assistant text or an error string.
129
+ """
130
+ if not API_KEY_302:
131
+ return "API_KEY_302 not set. Please configure your environment."
132
+
133
+ try:
134
+ conn = http.client.HTTPSConnection("api.302.ai")
135
+ payload = json.dumps({
136
+ "model": model,
137
+ "messages": [
138
+ {"role": "user", "content": prompt}
139
+ ]
140
+ })
141
+ headers = {
142
+ "Accept": "application/json",
143
+ "Authorization": f"Bearer {API_KEY_302}",
144
+ "Content-Type": "application/json"
145
+ }
146
+ conn.request("POST", "/v1/chat/completions", payload, headers)
147
+ res = conn.getresponse()
148
+ raw = res.read().decode("utf-8")
149
+ conn.close()
150
+
151
+ output = json.loads(raw)
152
+ msg = output.get("choices", [{}])[0].get("message", {})
153
+ text = msg.get("content") or msg.get("text") or str(msg)
154
+ return text.strip()
155
+ except Exception as e:
156
+ return f"LLM API error: {e}"
157
+
158
+ # ----------------------------
159
+ # Button handlers (shared)
160
+ # ----------------------------
161
+ def on_click_transcribe(record_path):
162
+ """Row 1: Transcribe audio."""
163
+ text = transcribe_audio(record_path)
164
+ return gr.update(value=text)
165
+
166
+ def on_click_analyze_baseline(selected_model, original_text, transcribed_text):
167
+ """
168
+ Baseline Tab: Single-call analysis using PROMPT_TEMPLATE_1.
169
+ """
170
+ prompt = PROMPT_TEMPLATE_1.format(
171
+ original_text=original_text or "",
172
+ transcribed_text=transcribed_text or "",
173
+ )
174
+ analysis = call_llm_302(selected_model, prompt)
175
+ return gr.update(value=analysis)
176
+
177
+ def on_click_analyze_ipa(selected_model, original_text, transcribed_text):
178
+ """
179
+ Baseline+IPA Tab: Two-step analysis.
180
+ 1) Generate IPA annotations.
181
+ 2) Use IPA + original + transcript for diagnosis.
182
+ Returns (ipa_box_update, summary_update)
183
+ """
184
+ # Step 1: IPA
185
+ ipa_prompt = PROMPT_TEMPLATE_IPA.format(
186
+ original_text=original_text or "",
187
+ transcribed_text=transcribed_text or "",
188
+ )
189
+ ipa_text = call_llm_302(selected_model, ipa_prompt)
190
+
191
+ # Step 2: Diagnosis with IPA
192
+ diag_prompt = PROMPT_TEMPLATE_1_WITH_IPA.format(
193
+ original_text=original_text or "",
194
+ transcribed_text=transcribed_text or "",
195
+ ipa_text=ipa_text or "",
196
+ )
197
+ summary = call_llm_302(selected_model, diag_prompt)
198
+
199
+ return gr.update(value=ipa_text), gr.update(value=summary)
200
+
201
+ def on_click_rewrite(selected_model, original_text, _transcribed_text_unused, summary):
202
+ """
203
+ Row 3: Rewrite script (always annotated version) -> PROMPT_TEMPLATE_2.
204
+ """
205
+ prompt = PROMPT_TEMPLATE_2.format(
206
+ notes=summary or "",
207
+ original_text=original_text or "",
208
+ )
209
+ revised = call_llm_302(selected_model, prompt)
210
+ return gr.update(value=revised)
211
+
212
+ # Simple pass-through to mirror recorded file into a Gradio File component
213
+ def passthrough_file(path):
214
+ return path
215
+
216
+ # ----------------------------
217
+ # Gradio UI (Tabs)
218
+ # ----------------------------
219
+ with gr.Blocks(title="DeStammerer: AI-assisted Speech Script Revision") as demo:
220
+ # gr.Markdown("### DeStammerer\nChoose a mode below. Both tabs share the same LLM selector semantics.")
221
+
222
+ with gr.Tabs():
223
+ # ------------------------ Tab 1: Baseline ------------------------
224
+ with gr.Tab("Baseline"):
225
+ # Row 1: Record + Download + Transcribe
226
+ with gr.Row():
227
+ audio_record_b = gr.Audio(label="Record Audio", sources=["microphone"], type="filepath")
228
+ audio_download_b = gr.File(label="Audio Download", interactive=False)
229
+ btn_transcribe_b = gr.Button("1) Transcribe")
230
+
231
+ # Row 2: ASR, Original, Model selector, Analyze
232
+ with gr.Row():
233
+ txt_transcribed_b = gr.Textbox(label="Transcribed Text (ASR)", interactive=False, lines=6, placeholder="ASR output appears here.")
234
+ txt_original_b = gr.Textbox(label="Original Script (input)", lines=6, placeholder="Paste your original script here.")
235
+ model_selector_b = gr.Dropdown(
236
+ choices=["gpt-4o-mini", "gpt-5"],
237
+ value="gpt-4o-mini",
238
+ label="LLM Model"
239
+ )
240
+ btn_analyze_b = gr.Button("2) Analyze")
241
+
242
+ # Row 3: Summary, Revised, Revise button
243
+ with gr.Row():
244
+ txt_summary_b = gr.Textbox(label="LLM Summary: Easy-to-Stutter Words", lines=8, placeholder="Analysis will appear here.")
245
+ txt_revised_b = gr.Textbox(label="Revised Script", lines=8, placeholder="Rewritten script will appear here.")
246
+ btn_rewrite_b = gr.Button("3) Revise Script")
247
+
248
+ # Row 4: Post-hoc audio record and download
249
+ with gr.Row():
250
+ posthoc_record_b = gr.Audio(label="Post-hoc Record Audio", sources=["microphone"], type="filepath")
251
+ posthoc_download_b = gr.File(label="Post-hoc Audio Download", interactive=False)
252
+
253
+ # Wiring (Baseline)
254
+ audio_record_b.change(fn=passthrough_file, inputs=audio_record_b, outputs=audio_download_b)
255
+ btn_transcribe_b.click(fn=on_click_transcribe, inputs=[audio_record_b], outputs=[txt_transcribed_b])
256
+ btn_analyze_b.click(
257
+ fn=on_click_analyze_baseline,
258
+ inputs=[model_selector_b, txt_original_b, txt_transcribed_b],
259
+ outputs=[txt_summary_b],
260
+ )
261
+ btn_rewrite_b.click(
262
+ fn=on_click_rewrite,
263
+ inputs=[model_selector_b, txt_original_b, txt_transcribed_b, txt_summary_b],
264
+ outputs=[txt_revised_b],
265
+ )
266
+ posthoc_record_b.change(fn=passthrough_file, inputs=posthoc_record_b, outputs=posthoc_download_b)
267
+
268
+ # -------------------- Tab 2: Baseline+IPA --------------------
269
+ with gr.Tab("Baseline+IPA"):
270
+ # Row 1: Record + Download + Transcribe
271
+ with gr.Row():
272
+ audio_record_i = gr.Audio(label="Record Audio", sources=["microphone"], type="filepath")
273
+ audio_download_i = gr.File(label="Audio Download", interactive=False)
274
+ btn_transcribe_i = gr.Button("1) Transcribe")
275
+
276
+ # Row 2: ASR, Original, IPA box, Model selector, Analyze
277
+ with gr.Row():
278
+ txt_transcribed_i = gr.Textbox(label="Transcribed Text (ASR)", interactive=False, lines=6, placeholder="ASR output appears here.")
279
+ txt_original_i = gr.Textbox(label="Original Script (input)", lines=6, placeholder="Paste your original script here.")
280
+ txt_ipa_i = gr.Textbox(label="IPA Annotations (LLM Output)", interactive=False, lines=6, placeholder="IPA for Original & Transcript will appear here.")
281
+ model_selector_i = gr.Dropdown(
282
+ choices=["gpt-4o-mini", "gpt-5"],
283
+ value="gpt-4o-mini",
284
+ label="LLM Model"
285
+ )
286
+ btn_analyze_i = gr.Button("2) Analyze (IPA → Diagnosis)")
287
+
288
+ # Row 3: Summary, Revised, Revise button
289
+ with gr.Row():
290
+ txt_summary_i = gr.Textbox(label="LLM Summary: Easy-to-Stutter Words (IPA-aware)", lines=8, placeholder="Analysis will appear here.")
291
+ txt_revised_i = gr.Textbox(label="Revised Script", lines=8, placeholder="Rewritten script will appear here.")
292
+ btn_rewrite_i = gr.Button("3) Revise Script")
293
+
294
+ # Row 4: Post-hoc audio record and download
295
+ with gr.Row():
296
+ posthoc_record_i = gr.Audio(label="Post-hoc Record Audio", sources=["microphone"], type="filepath")
297
+ posthoc_download_i = gr.File(label="Post-hoc Audio Download", interactive=False)
298
+
299
+ # Wiring (Baseline+IPA)
300
+ audio_record_i.change(fn=passthrough_file, inputs=audio_record_i, outputs=audio_download_i)
301
+ btn_transcribe_i.click(fn=on_click_transcribe, inputs=[audio_record_i], outputs=[txt_transcribed_i])
302
+
303
+ # Analyze in two steps: IPA then Diagnosis
304
+ def analyze_ipa_pipeline(model, original_text, transcribed_text):
305
+ ipa_update, summary_update = on_click_analyze_ipa(model, original_text, transcribed_text)
306
+ return ipa_update, summary_update
307
+
308
+ btn_analyze_i.click(
309
+ fn=analyze_ipa_pipeline,
310
+ inputs=[model_selector_i, txt_original_i, txt_transcribed_i],
311
+ outputs=[txt_ipa_i, txt_summary_i],
312
+ )
313
+
314
+ btn_rewrite_i.click(
315
+ fn=on_click_rewrite,
316
+ inputs=[model_selector_i, txt_original_i, txt_transcribed_i, txt_summary_i],
317
+ outputs=[txt_revised_i],
318
+ )
319
+ posthoc_record_i.change(fn=passthrough_file, inputs=posthoc_record_i, outputs=posthoc_download_i)
320
+
321
+ if __name__ == "__main__":
322
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ python-dotenv
2
+ elevenlabs
3
+ gradio