File size: 17,058 Bytes
effad1c
4792427
 
 
 
 
 
 
 
 
 
effad1c
 
4792427
6bdc489
6b6f26e
0fdb888
b3067c5
4792427
6bdc489
4792427
0fdb888
4792427
 
 
 
 
0fdb888
4792427
 
 
fd022eb
4792427
 
 
392a5eb
4792427
 
a609646
0fdb888
4792427
 
0fdb888
4792427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fdb888
4792427
 
 
0fdb888
4792427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fdb888
4792427
 
 
 
0fdb888
4792427
 
 
0fdb888
4792427
 
 
 
 
 
 
 
 
0fdb888
4792427
 
 
0fdb888
4792427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd022eb
4792427
 
 
 
 
0fdb888
4792427
 
 
364ce74
4792427
 
 
364ce74
4792427
0fdb888
4792427
 
fd022eb
4792427
 
 
 
 
 
 
 
 
 
 
 
 
6b6f26e
4792427
 
 
0fdb888
4792427
 
 
 
 
 
 
 
 
6b6f26e
4792427
 
 
 
 
 
 
 
7cce69a
4792427
392a5eb
4792427
 
 
 
 
 
 
 
 
6b6f26e
4792427
 
 
 
 
 
 
 
392a5eb
4792427
 
 
 
 
 
 
 
364ce74
4792427
 
 
 
 
 
 
 
 
 
 
 
 
 
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"""
SMARTok Demo โ€“ Stable Build (2025-06-09)
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
Tabs
 1) ๐ŸŽ™๏ธ ์˜ค๋””์˜ค ๋ฒˆ์—ญ
 2) ๐Ÿ“„ ๋ฌธ์„œยท์ด๋ฏธ์ง€ ๋ฒˆ์—ญ
 3) โฑ๏ธ ์‹ค์‹œ๊ฐ„ 1์–ธ์–ด
 4) ๐ŸŒ ์‹ค์‹œ๊ฐ„ 4์–ธ์–ด (์˜ยท์ค‘๊ฐ„ยทํƒœยท๋Ÿฌ)
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
ํ•„์ˆ˜ apt  : packages.txt ์ฐธ๊ณ 
ํ•„์ˆ˜ pip  : requirements.txt ์ฐธ๊ณ 
"""

import gradio as gr, openai, os, io, mimetypes, tempfile
from dotenv import load_dotenv
from PIL import Image
import pdfplumber, ocrmypdf, pytesseract

# โ”€โ”€โ”€โ”€โ”€ 0. Init โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
load_dotenv()
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY", ""))

LANG = ["Korean","English","Japanese","Chinese","Thai","Russian",
        "Vietnamese","Spanish","French"]
LC   = {               # tesseract lang codes
    "Korean":"kor","English":"eng","Japanese":"jpn","Chinese":"chi_sim",
    "Thai":"tha","Russian":"rus","Vietnamese":"vie","Spanish":"spa","French":"fra"
}
VOICE = {l:("nova" if l in ["Korean","Japanese","Chinese"] else "alloy") for l in LANG}
FOUR  = ["English","Chinese","Thai","Russian"]
CHUNK = 4   # sec โ€“ ์‹ค์‹œ๊ฐ„ ์ฒญํฌ ๊ธธ์ด

# โ”€โ”€โ”€โ”€โ”€ 1. helpers โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def _path(v):
    return None if v is None else (v["name"] if isinstance(v,dict) else v)

def _gpt(txt, src, tgt):
    rsp = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role":"system",
             "content":f"Translate the following {src} text to {tgt}. "
                        "Return only the translated text."},
            {"role":"user","content":txt}],
        temperature=0.3,max_tokens=4096)
    return rsp.choices[0].message.content.strip()

def _tts(txt, lang):
    res = client.audio.speech.create(model="tts-1",voice=VOICE[lang],input=txt[:4096])
    f = tempfile.NamedTemporaryFile(delete=False,suffix=".mp3")
    f.write(res.content); f.close(); return f.name

# โ”€โ”€โ”€โ”€โ”€ 2. audio โ†’ ๋ฒˆ์—ญ + TTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def trans_audio(audio, src, tgt):
    p=_path(audio)
    if not p or not os.path.exists(p): return "โš ๏ธ ์Œ์„ฑ ํŒŒ์ผ ํ•„์š”","",None
    with open(p,"rb") as f:
        stt = client.audio.transcriptions.create(model="whisper-1",file=f,
                                                 language=LC.get(src,"eng"))
    orig = stt.text.strip()
    if not orig: return "โš ๏ธ ์Œ์„ฑ ์ธ์‹ ์‹คํŒจ","",None
    trans = _gpt(orig,src,tgt)
    return orig, trans, _tts(trans,tgt)

# โ”€โ”€โ”€โ”€โ”€ 3. PDF / Image OCR โ†’ ๋ฒˆ์—ญ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def trans_doc(file, src, tgt):
    p=_path(file)
    if not p or not os.path.exists(p): return "โš ๏ธ ํŒŒ์ผ ์—…๋กœ๋“œ",""
    ext = os.path.splitext(p)[1].lower()
    mime= mimetypes.guess_type(p)[0] or ""
    txt = ""
    try:
        # PDF
        if ext==".pdf" or "pdf" in mime:
            with pdfplumber.open(p) as pdf:
                txt="\n".join(pg.extract_text() or "" for pg in pdf.pages[:5])
        # ์ด๋ฏธ์ง€
        else:
            tmp_pdf=tempfile.NamedTemporaryFile(delete=False,suffix=".pdf").name
            Image.open(p).save(tmp_pdf,"PDF")
            ocr_pdf=tempfile.NamedTemporaryFile(delete=False,suffix=".pdf").name
            try:
                ocrmypdf.ocr(tmp_pdf, ocr_pdf,
                             lang=f"{LC.get(src,'eng')}+eng",
                             deskew=True,optimize=0,progress_bar=False)
                with pdfplumber.open(ocr_pdf) as pdf:
                    txt="\n".join(pg.extract_text() or "" for pg in pdf.pages)
            except Exception:
                txt = pytesseract.image_to_string(
                    Image.open(p), lang=LC.get(src,"eng"))
    except Exception as e:
        return f"โŒ ์ถ”์ถœ ์˜ค๋ฅ˜: {e}",""
    txt = txt.strip()
    if not txt: return "โš ๏ธ ํ…์ŠคํŠธ ์ถ”์ถœ ์‹คํŒจ",""
    return txt, _gpt(txt,src,tgt)

# โ”€โ”€โ”€โ”€โ”€ 4. ์‹ค์‹œ๊ฐ„ 1์–ธ์–ด ์ŠคํŠธ๋ฆผ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def stream_one(path, src, tgt, st):
    st = st or {"o":"","t":""}
    if not path or not os.path.exists(path): return st["o"],st["t"],st
    with open(path,"rb") as f:
        stt=client.audio.transcriptions.create(model="whisper-1",file=f,
                                               language=LC.get(src,"eng"))
    full=stt.text.strip(); new=full[len(st["o"]):]
    if new:
        st["o"]=full
        st["t"]+=" "+_gpt(new,src,tgt)
    return st["o"],st["t"].strip(),st

# โ”€โ”€โ”€โ”€โ”€ 5. ์‹ค์‹œ๊ฐ„ 4์–ธ์–ด ์ŠคํŠธ๋ฆผ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def stream_four(path, src, st):
    st = st or {k:"" for k in ["o"]+FOUR}
    if not path or not os.path.exists(path):
        return st["o"],st["English"],st["Chinese"],st["Thai"],st["Russian"],st
    with open(path,"rb") as f:
        stt=client.audio.transcriptions.create(model="whisper-1",file=f,
                                               language=LC.get(src,"eng"))
    full=stt.text.strip(); new=full[len(st["o"]):]
    if new:
        st["o"]=full
        for l in FOUR:
            st[l]+=" "+_gpt(new,src,l)
    return (st["o"].strip(),st["English"].strip(),st["Chinese"].strip(),
            st["Thai"].strip(),st["Russian"].strip(),st)

# โ”€โ”€โ”€โ”€โ”€ 6. UI โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
with gr.Blocks(title="SMARTok Demo",theme=gr.themes.Soft()) as app:
    with gr.Tabs():
        # โ‘  ์˜ค๋””์˜ค ๋ฒˆ์—ญ
        with gr.TabItem("๐ŸŽ™๏ธ ์˜ค๋””์˜ค ๋ฒˆ์—ญ"):
            s1=gr.Dropdown(LANG,value="Korean",label="์ž…๋ ฅ")
            t1=gr.Dropdown(LANG,value="English",label="์ถœ๋ ฅ")
            a1=gr.Audio(sources=["microphone","upload"],type="filepath")
            btn1=gr.Button("๋ฒˆ์—ญ")
            o1=gr.Textbox(label="์›๋ฌธ",lines=5); tr1=gr.Textbox(label="๋ฒˆ์—ญ",lines=5)
            aud1=gr.Audio(label="TTS",type="filepath",autoplay=True)
            btn1.click(trans_audio,[a1,s1,t1],[o1,tr1,aud1])

        # โ‘ก ๋ฌธ์„œยท์ด๋ฏธ์ง€ ๋ฒˆ์—ญ
        with gr.TabItem("๐Ÿ“„ ๋ฌธ์„œยท์ด๋ฏธ์ง€ ๋ฒˆ์—ญ"):
            s2=gr.Dropdown(LANG,value="Korean",label="์ž…๋ ฅ")
            t2=gr.Dropdown(LANG,value="English",label="์ถœ๋ ฅ")
            f2=gr.File(file_types=[".pdf",".png",".jpg",".jpeg",".bmp",".tiff",".gif"])
            btn2=gr.Button("๋ฒˆ์—ญ")
            o2=gr.Textbox(label="์ถ”์ถœ ์›๋ฌธ",lines=15); tr2=gr.Textbox(label="๋ฒˆ์—ญ",lines=15)
            btn2.click(trans_doc,[f2,s2,t2],[o2,tr2])

        # โ‘ข ์‹ค์‹œ๊ฐ„ 1์–ธ์–ด
        with gr.TabItem("โฑ๏ธ ์‹ค์‹œ๊ฐ„ 1์–ธ์–ด"):
            s3=gr.Dropdown(LANG,value="Korean",label="์ž…๋ ฅ")
            t3=gr.Dropdown(LANG,value="English",label="์ถœ๋ ฅ")
            mic3=gr.Audio(sources=["microphone"],streaming=True)
            o3=gr.Textbox(label="์›๋ฌธ(์‹ค์‹œ๊ฐ„)",lines=8); tr3=gr.Textbox(label="๋ฒˆ์—ญ(์‹ค์‹œ๊ฐ„)",lines=8)
            st3=gr.State()
            mic3.stream(stream_one,inputs=[s3,t3,st3],outputs=[o3,tr3,st3])

        # โ‘ฃ ์‹ค์‹œ๊ฐ„ 4์–ธ์–ด
        with gr.TabItem("๐ŸŒ ์‹ค์‹œ๊ฐ„ 4์–ธ์–ด"):
            s4=gr.Dropdown(LANG,value="Korean",label="์ž…๋ ฅ ์–ธ์–ด")
            mic4=gr.Audio(sources=["microphone"],streaming=True)
            o4=gr.Textbox(label="์›๋ฌธ",lines=8)
            e4=gr.Textbox(label="English",lines=8)
            c4=gr.Textbox(label="Chinese(็ฎ€ไฝ“)",lines=8)
            th4=gr.Textbox(label="Thai",lines=8)
            r4=gr.Textbox(label="Russian",lines=8)
            st4=gr.State()
            mic4.stream(stream_four,inputs=[s4,st4],
                        outputs=[o4,e4,c4,th4,r4,st4])

# โ”€โ”€โ”€โ”€โ”€ 7. Launch โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
if __name__=="__main__":
    app.launch(server_name="0.0.0.0",server_port=7860,share=False,debug=True)
"""
SMARTok Demo โ€“ Stable Build (2025-06-09)
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
Tabs
 1) ๐ŸŽ™๏ธ ์˜ค๋””์˜ค ๋ฒˆ์—ญ
 2) ๐Ÿ“„ ๋ฌธ์„œยท์ด๋ฏธ์ง€ ๋ฒˆ์—ญ
 3) โฑ๏ธ ์‹ค์‹œ๊ฐ„ 1์–ธ์–ด
 4) ๐ŸŒ ์‹ค์‹œ๊ฐ„ 4์–ธ์–ด (์˜ยท์ค‘๊ฐ„ยทํƒœยท๋Ÿฌ)
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
ํ•„์ˆ˜ apt  : packages.txt ์ฐธ๊ณ 
ํ•„์ˆ˜ pip  : requirements.txt ์ฐธ๊ณ 
"""

import gradio as gr, openai, os, io, mimetypes, tempfile
from dotenv import load_dotenv
from PIL import Image
import pdfplumber, ocrmypdf, pytesseract

# โ”€โ”€โ”€โ”€โ”€ 0. Init โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
load_dotenv()
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY", ""))

LANG = ["Korean","English","Japanese","Chinese","Thai","Russian",
        "Vietnamese","Spanish","French"]
LC   = {               # tesseract lang codes
    "Korean":"kor","English":"eng","Japanese":"jpn","Chinese":"chi_sim",
    "Thai":"tha","Russian":"rus","Vietnamese":"vie","Spanish":"spa","French":"fra"
}
VOICE = {l:("nova" if l in ["Korean","Japanese","Chinese"] else "alloy") for l in LANG}
FOUR  = ["English","Chinese","Thai","Russian"]
CHUNK = 4   # sec โ€“ ์‹ค์‹œ๊ฐ„ ์ฒญํฌ ๊ธธ์ด

# โ”€โ”€โ”€โ”€โ”€ 1. helpers โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def _path(v):
    return None if v is None else (v["name"] if isinstance(v,dict) else v)

def _gpt(txt, src, tgt):
    rsp = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role":"system",
             "content":f"Translate the following {src} text to {tgt}. "
                        "Return only the translated text."},
            {"role":"user","content":txt}],
        temperature=0.3,max_tokens=4096)
    return rsp.choices[0].message.content.strip()

def _tts(txt, lang):
    res = client.audio.speech.create(model="tts-1",voice=VOICE[lang],input=txt[:4096])
    f = tempfile.NamedTemporaryFile(delete=False,suffix=".mp3")
    f.write(res.content); f.close(); return f.name

# โ”€โ”€โ”€โ”€โ”€ 2. audio โ†’ ๋ฒˆ์—ญ + TTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def trans_audio(audio, src, tgt):
    p=_path(audio)
    if not p or not os.path.exists(p): return "โš ๏ธ ์Œ์„ฑ ํŒŒ์ผ ํ•„์š”","",None
    with open(p,"rb") as f:
        stt = client.audio.transcriptions.create(model="whisper-1",file=f,
                                                 language=LC.get(src,"eng"))
    orig = stt.text.strip()
    if not orig: return "โš ๏ธ ์Œ์„ฑ ์ธ์‹ ์‹คํŒจ","",None
    trans = _gpt(orig,src,tgt)
    return orig, trans, _tts(trans,tgt)

# โ”€โ”€โ”€โ”€โ”€ 3. PDF / Image OCR โ†’ ๋ฒˆ์—ญ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def trans_doc(file, src, tgt):
    p=_path(file)
    if not p or not os.path.exists(p): return "โš ๏ธ ํŒŒ์ผ ์—…๋กœ๋“œ",""
    ext = os.path.splitext(p)[1].lower()
    mime= mimetypes.guess_type(p)[0] or ""
    txt = ""
    try:
        # PDF
        if ext==".pdf" or "pdf" in mime:
            with pdfplumber.open(p) as pdf:
                txt="\n".join(pg.extract_text() or "" for pg in pdf.pages[:5])
        # ์ด๋ฏธ์ง€
        else:
            tmp_pdf=tempfile.NamedTemporaryFile(delete=False,suffix=".pdf").name
            Image.open(p).save(tmp_pdf,"PDF")
            ocr_pdf=tempfile.NamedTemporaryFile(delete=False,suffix=".pdf").name
            try:
                ocrmypdf.ocr(tmp_pdf, ocr_pdf,
                             lang=f"{LC.get(src,'eng')}+eng",
                             deskew=True,optimize=0,progress_bar=False)
                with pdfplumber.open(ocr_pdf) as pdf:
                    txt="\n".join(pg.extract_text() or "" for pg in pdf.pages)
            except Exception:
                txt = pytesseract.image_to_string(
                    Image.open(p), lang=LC.get(src,"eng"))
    except Exception as e:
        return f"โŒ ์ถ”์ถœ ์˜ค๋ฅ˜: {e}",""
    txt = txt.strip()
    if not txt: return "โš ๏ธ ํ…์ŠคํŠธ ์ถ”์ถœ ์‹คํŒจ",""
    return txt, _gpt(txt,src,tgt)

# โ”€โ”€โ”€โ”€โ”€ 4. ์‹ค์‹œ๊ฐ„ 1์–ธ์–ด ์ŠคํŠธ๋ฆผ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def stream_one(path, src, tgt, st):
    st = st or {"o":"","t":""}
    if not path or not os.path.exists(path): return st["o"],st["t"],st
    with open(path,"rb") as f:
        stt=client.audio.transcriptions.create(model="whisper-1",file=f,
                                               language=LC.get(src,"eng"))
    full=stt.text.strip(); new=full[len(st["o"]):]
    if new:
        st["o"]=full
        st["t"]+=" "+_gpt(new,src,tgt)
    return st["o"],st["t"].strip(),st

# โ”€โ”€โ”€โ”€โ”€ 5. ์‹ค์‹œ๊ฐ„ 4์–ธ์–ด ์ŠคํŠธ๋ฆผ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def stream_four(path, src, st):
    st = st or {k:"" for k in ["o"]+FOUR}
    if not path or not os.path.exists(path):
        return st["o"],st["English"],st["Chinese"],st["Thai"],st["Russian"],st
    with open(path,"rb") as f:
        stt=client.audio.transcriptions.create(model="whisper-1",file=f,
                                               language=LC.get(src,"eng"))
    full=stt.text.strip(); new=full[len(st["o"]):]
    if new:
        st["o"]=full
        for l in FOUR:
            st[l]+=" "+_gpt(new,src,l)
    return (st["o"].strip(),st["English"].strip(),st["Chinese"].strip(),
            st["Thai"].strip(),st["Russian"].strip(),st)

# โ”€โ”€โ”€โ”€โ”€ 6. UI โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
with gr.Blocks(title="SMARTok Demo",theme=gr.themes.Soft()) as app:
    with gr.Tabs():
        # โ‘  ์˜ค๋””์˜ค ๋ฒˆ์—ญ
        with gr.TabItem("๐ŸŽ™๏ธ ์˜ค๋””์˜ค ๋ฒˆ์—ญ"):
            s1=gr.Dropdown(LANG,value="Korean",label="์ž…๋ ฅ")
            t1=gr.Dropdown(LANG,value="English",label="์ถœ๋ ฅ")
            a1=gr.Audio(sources=["microphone","upload"],type="filepath")
            btn1=gr.Button("๋ฒˆ์—ญ")
            o1=gr.Textbox(label="์›๋ฌธ",lines=5); tr1=gr.Textbox(label="๋ฒˆ์—ญ",lines=5)
            aud1=gr.Audio(label="TTS",type="filepath",autoplay=True)
            btn1.click(trans_audio,[a1,s1,t1],[o1,tr1,aud1])

        # โ‘ก ๋ฌธ์„œยท์ด๋ฏธ์ง€ ๋ฒˆ์—ญ
        with gr.TabItem("๐Ÿ“„ ๋ฌธ์„œยท์ด๋ฏธ์ง€ ๋ฒˆ์—ญ"):
            s2=gr.Dropdown(LANG,value="Korean",label="์ž…๋ ฅ")
            t2=gr.Dropdown(LANG,value="English",label="์ถœ๋ ฅ")
            f2=gr.File(file_types=[".pdf",".png",".jpg",".jpeg",".bmp",".tiff",".gif"])
            btn2=gr.Button("๋ฒˆ์—ญ")
            o2=gr.Textbox(label="์ถ”์ถœ ์›๋ฌธ",lines=15); tr2=gr.Textbox(label="๋ฒˆ์—ญ",lines=15)
            btn2.click(trans_doc,[f2,s2,t2],[o2,tr2])

        # โ‘ข ์‹ค์‹œ๊ฐ„ 1์–ธ์–ด
        with gr.TabItem("โฑ๏ธ ์‹ค์‹œ๊ฐ„ 1์–ธ์–ด"):
            s3=gr.Dropdown(LANG,value="Korean",label="์ž…๋ ฅ")
            t3=gr.Dropdown(LANG,value="English",label="์ถœ๋ ฅ")
            mic3=gr.Audio(sources=["microphone"],streaming=True)
            o3=gr.Textbox(label="์›๋ฌธ(์‹ค์‹œ๊ฐ„)",lines=8); tr3=gr.Textbox(label="๋ฒˆ์—ญ(์‹ค์‹œ๊ฐ„)",lines=8)
            st3=gr.State()
            mic3.stream(stream_one,inputs=[s3,t3,st3],outputs=[o3,tr3,st3])

        # โ‘ฃ ์‹ค์‹œ๊ฐ„ 4์–ธ์–ด
        with gr.TabItem("๐ŸŒ ์‹ค์‹œ๊ฐ„ 4์–ธ์–ด"):
            s4=gr.Dropdown(LANG,value="Korean",label="์ž…๋ ฅ ์–ธ์–ด")
            mic4=gr.Audio(sources=["microphone"],streaming=True)
            o4=gr.Textbox(label="์›๋ฌธ",lines=8)
            e4=gr.Textbox(label="English",lines=8)
            c4=gr.Textbox(label="Chinese(็ฎ€ไฝ“)",lines=8)
            th4=gr.Textbox(label="Thai",lines=8)
            r4=gr.Textbox(label="Russian",lines=8)
            st4=gr.State()
            mic4.stream(stream_four,inputs=[s4,st4],
                        outputs=[o4,e4,c4,th4,r4,st4])

# โ”€โ”€โ”€โ”€โ”€ 7. Launch โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
if __name__=="__main__":
    app.launch(server_name="0.0.0.0",server_port=7860,share=False,debug=True)