File size: 7,598 Bytes
1614239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fb620e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# app_clean.py
import os
import json
import http.client
from io import BytesIO
import gradio as gr
from dotenv import load_dotenv
from elevenlabs.client import ElevenLabs

load_dotenv()

KEY_EL = os.getenv("ELEVENLABS_API_KEY", "")
KEY_LLM = os.getenv("API_KEY_302", "")

client_el = None
if KEY_EL:
    client_el = ElevenLabs(api_key=KEY_EL)

PROMPT_A = """\
You are a speech-language pathology helper. Review the ORIGINAL text and the ASR TRANSCRIPT
(which may include recognition mistakes) and identify words or sequences that commonly provoke
stuttering (e.g., complex consonant clusters, multi-syllabic constructions, challenging onsets).

Provide a brief, well-organized diagnostic summary focusing on potential stutter-triggering elements.

ORIGINAL:
{ot}

TRANSCRIPT:
{tt}

Do not provide advice or revision. Respond only with concise diagnostic notes and easy-to-stutter patterns.
"""

PROMPT_B = """\
You are a speech-language pathology helper. Reformulate the ORIGINAL text to lower the likelihood
of stuttering while keeping the original meaning, style, and intention. Favor simpler vocabulary,
shorter phrases, and smoother beginnings of words.

Reference diagnosis details:
{ns}

ORIGINAL:
{ot}

Your reply must contain only the fully rewritten script, no commentary.
"""

PROMPT_C = """\
Transcribe both the ORIGINAL text and the ASR TRANSCRIPT into IPA, marking syllable boundaries.
Return your output in a compact format with clear section labels, for example:

ORIGINAL_IPA:
<original in IPA with syllable breaks>

TRANSCRIPT_IPA:
<transcript in IPA with syllable breaks>

No added explanation or extra notes.

ORIGINAL:
{ot}

TRANSCRIPT:
{tt}
"""

PROMPT_D = """\
You are a speech-language pathology helper. Using the ORIGINAL script, the ASR TRANSCRIPT,
and the IPA annotations, identify segments that are stutter-prone (e.g., long or complex words,
difficult consonant combinations, problematic sound onsets).

Provide a concise, structured diagnostic overview of likely stuttering hotspots.

ORIGINAL:
{ot}

TRANSCRIPT:
{tt}

IPA_ANNOTATIONS:
{ip}

Do not include suggestions or edits. Only provide principled diagnostic notes.
"""

def fn_transcribe(pth: str | None) -> str:
    if not pth:
        return "No audio provided. Please upload or record audio."
    if not KEY_EL:
        return "ELEVENLABS_API_KEY not set. Please configure your environment."
    try:
        with open(pth, "rb") as f:
            buf = BytesIO(f.read())
    except Exception as e:
        return f"Failed to read audio: {e}"
    try:
        tr = client_el.speech_to_text.convert(
            file=buf,
            model_id="scribe_v1",
            tag_audio_events=True,
            language_code="eng",
            diarize=True,
        )
        return tr.text or ""
    except Exception as e:
        return f"Transcription error: {e}"

def fn_llm(model: str, prm: str) -> str:
    if not KEY_LLM:
        return "API_KEY_302 not set. Please configure your environment."
    try:
        con = http.client.HTTPSConnection("api.302.ai")
        pl = json.dumps({
            "model": model,
            "messages": [
                {"role": "user", "content": prm}
            ]
        })
        hd = {
            "Accept": "application/json",
            "Authorization": f"Bearer {KEY_LLM}",
            "Content-Type": "application/json"
        }
        con.request("POST", "/v1/chat/completions", pl, hd)
        rs = con.getresponse()
        raw = rs.read().decode("utf-8")
        con.close()
        out = json.loads(raw)
        msg = out.get("choices", [{}])[0].get("message", {})
        tx = msg.get("content") or msg.get("text") or str(msg)
        return tx.strip()
    except Exception as e:
        return f"LLM API error: {e}"

def cb_transcribe(p):
    return gr.update(value=fn_transcribe(p))

def cb_basic(m, o, t):
    prm = PROMPT_A.format(ot=o or "", tt=t or "")
    rz = fn_llm(m, prm)
    return gr.update(value=rz)

def cb_ipa(m, o, t):
    p1 = PROMPT_C.format(ot=o or "", tt=t or "")
    ipa = fn_llm(m, p1)
    p2 = PROMPT_D.format(ot=o or "", tt=t or "", ip=ipa or "")
    sm = fn_llm(m, p2)
    return gr.update(value=ipa), gr.update(value=sm)

def cb_rewrite(m, o, u, s):
    prm = PROMPT_B.format(ns=s or "", ot=o or "")
    rw = fn_llm(m, prm)
    return gr.update(value=rw)

def passthru(p):
    return p

with gr.Blocks(title="Stutter Up") as ui:
    with gr.Tabs():
        with gr.Tab("Baseline"):
            with gr.Row():
                a_r1 = gr.Audio(label="Record Audio", sources=["microphone"], type="filepath")
                a_f1 = gr.File(label="Audio Download", interactive=False)
                b_t1 = gr.Button("1) Transcribe")
            with gr.Row():
                t_t1 = gr.Textbox(label="Transcribed Text (ASR)", interactive=False, lines=6)
                t_o1 = gr.Textbox(label="Original Script (input)", lines=6)
                dr1 = gr.Dropdown(choices=["gpt-4o-mini", "gpt-5"], value="gpt-4o-mini", label="LLM Model")
                b_a1 = gr.Button("2) Analyze")
            with gr.Row():
                t_s1 = gr.Textbox(label="LLM Summary: Easy-to-Stutter Words", lines=8)
                t_r1 = gr.Textbox(label="Revised Script", lines=8)
                b_r1 = gr.Button("3) Revise Script")
            with gr.Row():
                a_r2 = gr.Audio(label="Post-hoc Record Audio", sources=["microphone"], type="filepath")
                a_f2 = gr.File(label="Post-hoc Audio Download", interactive=False)

            a_r1.change(fn=passthru, inputs=a_r1, outputs=a_f1)
            b_t1.click(fn=cb_transcribe, inputs=[a_r1], outputs=[t_t1])
            b_a1.click(fn=cb_basic, inputs=[dr1, t_o1, t_t1], outputs=[t_s1])
            b_r1.click(fn=cb_rewrite, inputs=[dr1, t_o1, t_t1, t_s1], outputs=[t_r1])
            a_r2.change(fn=passthru, inputs=a_r2, outputs=a_f2)

        with gr.Tab("Baseline+IPA"):
            with gr.Row():
                a_r3 = gr.Audio(label="Record Audio", sources=["microphone"], type="filepath")
                a_f3 = gr.File(label="Audio Download", interactive=False)
                b_t2 = gr.Button("1) Transcribe")
            with gr.Row():
                t_t2 = gr.Textbox(label="Transcribed Text (ASR)", interactive=False, lines=6)
                t_o2 = gr.Textbox(label="Original Script (input)", lines=6)
                t_i2 = gr.Textbox(label="IPA Annotations (LLM Output)", interactive=False, lines=6)
                dr2 = gr.Dropdown(choices=["gpt-4o-mini", "gpt-5"], value="gpt-4o-mini", label="LLM Model")
                b_a2 = gr.Button("2) Analyze (IPA → Diagnosis)")
            with gr.Row():
                t_s2 = gr.Textbox(label="LLM Summary: Easy-to-Stutter Words (IPA-aware)", lines=8)
                t_r2 = gr.Textbox(label="Revised Script", lines=8)
                b_r2 = gr.Button("3) Revise Script")
            with gr.Row():
                a_r4 = gr.Audio(label="Post-hoc Record Audio", sources=["microphone"], type="filepath")
                a_f4 = gr.File(label="Post-hoc Audio Download", interactive=False)

            a_r3.change(fn=passthru, inputs=a_r3, outputs=a_f3)
            b_t2.click(fn=cb_transcribe, inputs=[a_r3], outputs=[t_t2])
            def pipe(m, o, t):
                x, y = cb_ipa(m, o, t)
                return x, y
            b_a2.click(fn=pipe, inputs=[dr2, t_o2, t_t2], outputs=[t_i2, t_s2])
            b_r2.click(fn=cb_rewrite, inputs=[dr2, t_o2, t_t2, t_s2], outputs=[t_r2])
            a_r4.change(fn=passthru, inputs=a_r4, outputs=a_f4)

if __name__ == "__main__":
    ui.launch(share=False)