File size: 17,054 Bytes
92dd616
 
 
6bdc489
8000eeb
 
b3067c5
92dd616
6bdc489
92dd616
 
 
 
 
 
 
 
 
4e89e7e
92dd616
 
 
 
a609646
92dd616
 
 
 
4792427
 
92dd616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e89e7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8000eeb
 
 
 
f1697bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8000eeb
 
 
 
 
 
f1697bd
 
 
8000eeb
 
 
 
 
 
4e89e7e
f1697bd
4e89e7e
f1697bd
 
 
 
 
 
 
 
 
 
4e89e7e
 
 
f1697bd
 
 
4e89e7e
 
 
f1697bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e89e7e
 
 
 
 
 
 
92dd616
8000eeb
4e89e7e
 
8000eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92dd616
4e89e7e
8000eeb
 
 
 
 
 
f1697bd
8000eeb
f1697bd
8000eeb
 
4e89e7e
8000eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e89e7e
92dd616
 
4e89e7e
 
92dd616
8000eeb
 
4e89e7e
 
8000eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e89e7e
 
 
8000eeb
 
 
 
 
 
 
 
f1697bd
8000eeb
 
4e89e7e
8000eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e89e7e
 
 
92dd616
 
 
4792427
92dd616
 
4e89e7e
 
 
 
 
 
 
 
 
92dd616
 
 
4e89e7e
 
 
 
 
 
 
 
92dd616
 
 
4e89e7e
 
f1697bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e89e7e
 
 
 
 
 
f1697bd
 
 
4e89e7e
4792427
92dd616
 
4e89e7e
f1697bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e89e7e
 
 
 
 
 
f1697bd
 
 
4e89e7e
 
 
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import os, asyncio, json, tempfile, websockets, pdfplumber
import gradio as gr
import openai
from dotenv import load_dotenv
import numpy as np
import wave

# โ”€โ”€โ”€ 0. ์ดˆ๊ธฐํ™” โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
    raise RuntimeError("OPENAI_API_KEY ๊ฐ€ .env ์— ์—†์Šต๋‹ˆ๋‹ค!")

LANG = ["Korean","English","Japanese","Chinese",
        "Thai","Russian","Vietnamese","Spanish","French"]
VOICE = {l: ("nova" if l in ["Korean","Japanese","Chinese"] else "alloy")
         for l in LANG}
FOUR = ["English","Chinese","Thai","Russian"]
WS_URL = "wss://api.openai.com/v1/realtime"  # ์˜ฌ๋ฐ”๋ฅธ ์—”๋“œํฌ์ธํŠธ๋กœ ์ˆ˜์ •

# โ”€โ”€โ”€ 1. ๊ณตํ†ต GPT ๋ฒˆ์—ญ / TTS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
async def gpt_translate(text, src, tgt):
    rsp = await openai.AsyncClient().chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role":"system",
                   "content":f"Translate {src} โ†’ {tgt}. Return only the text."},
                  {"role":"user","content":text}],
        temperature=0.3,max_tokens=2048)
    return rsp.choices[0].message.content.strip()

async def gpt_tts(text, lang):
    rsp = await openai.AsyncClient().audio.speech.create(
        model="tts-1", voice=VOICE[lang], input=text[:4096])
    tmp = tempfile.NamedTemporaryFile(delete=False,suffix=".mp3")
    tmp.write(rsp.content); tmp.close(); return tmp.name

# โ”€โ”€โ”€ 2. PDF ๋ฒˆ์—ญ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def translate_pdf(file, src, tgt):
    if not file: return "โš ๏ธ PDF ์—…๋กœ๋“œ ํ•„์š”", ""
    with pdfplumber.open(file.name) as pdf:
        text = "\n".join(p.extract_text() or "" for p in pdf.pages[:5]).strip()
    if not text:
        return "โš ๏ธ ํ…์ŠคํŠธ ์ถ”์ถœ ์‹คํŒจ", ""
    return text, asyncio.run(gpt_translate(text, src, tgt))

# โ”€โ”€โ”€ 2-1. ์˜ค๋””์˜ค ๋ฒˆ์—ญ (ํƒญ1์šฉ) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
async def translate_audio_async(file, src, tgt):
    if not file: return "โš ๏ธ ์˜ค๋””์˜ค ์—…๋กœ๋“œ ํ•„์š”", "", None
    
    # STT: Whisper API ์‚ฌ์šฉ
    with open(file, 'rb') as audio_file:
        transcript = await openai.AsyncClient().audio.transcriptions.create(
            model="whisper-1",
            file=audio_file,
            language=src[:2].lower()  # ์–ธ์–ด ์ฝ”๋“œ ๊ฐ„์†Œํ™”
        )
    
    orig_text = transcript.text
    trans_text = await gpt_translate(orig_text, src, tgt)
    audio_path = await gpt_tts(trans_text, tgt)
    
    return orig_text, trans_text, audio_path

def translate_audio(file, src, tgt):
    return asyncio.run(translate_audio_async(file, src, tgt))

# โ”€โ”€โ”€ 3. ์‹ค์‹œ๊ฐ„ STT (Whisper API ์‚ฌ์šฉ) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
async def process_audio_chunk(audio_data, src_lang):
    """์˜ค๋””์˜ค ์ฒญํฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜์—ฌ ํ…์ŠคํŠธ๋กœ ๋ณ€ํ™˜"""
    if audio_data is None:
        return ""
    
    try:
        # Gradio๋Š” (sample_rate, audio_array) ํŠœํ”Œ์„ ๋ฐ˜ํ™˜
        if isinstance(audio_data, tuple):
            sample_rate, audio_array = audio_data
            
            # ์˜ค๋””์˜ค๊ฐ€ ๋„ˆ๋ฌด ์งง์œผ๋ฉด ๋ฌด์‹œ (0.5์ดˆ ๋ฏธ๋งŒ)
            if len(audio_array) < sample_rate * 0.5:
                return ""
            
            # ์˜ค๋””์˜ค ์ •๊ทœํ™” ๋ฐ ๋…ธ์ด์ฆˆ ํ•„ํ„ฐ๋ง
            audio_array = audio_array.astype(np.float32)
            
            # ๋ฌด์Œ ๊ฐ์ง€ - RMS๊ฐ€ ๋„ˆ๋ฌด ๋‚ฎ์œผ๋ฉด ๋ฌด์‹œ
            rms = np.sqrt(np.mean(audio_array**2))
            if rms < 0.01:  # ๋ฌด์Œ ์ž„๊ณ„๊ฐ’
                return ""
            
            # ์ •๊ทœํ™”
            max_val = np.max(np.abs(audio_array))
            if max_val > 0:
                audio_array = audio_array / max_val * 0.95
            
            # numpy array๋ฅผ WAV ํŒŒ์ผ๋กœ ๋ณ€ํ™˜
            with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
                with wave.open(tmp.name, 'wb') as wav_file:
                    wav_file.setnchannels(1)  # mono
                    wav_file.setsampwidth(2)  # 16-bit
                    wav_file.setframerate(sample_rate)
                    
                    # float32๋ฅผ 16-bit PCM์œผ๋กœ ๋ณ€ํ™˜
                    audio_int16 = (audio_array * 32767).astype(np.int16)
                    wav_file.writeframes(audio_int16.tobytes())
                tmp_path = tmp.name
        else:
            # bytes ๋ฐ์ดํ„ฐ์ธ ๊ฒฝ์šฐ
            with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
                tmp.write(audio_data)
                tmp_path = tmp.name
        
        # Whisper API๋กœ ๋ณ€ํ™˜ - ์–ธ์–ด ํžŒํŠธ์™€ ํ”„๋กฌํ”„ํŠธ ์ถ”๊ฐ€
        with open(tmp_path, 'rb') as audio_file:
            # ์–ธ์–ด๋ณ„ ํ”„๋กฌํ”„ํŠธ ์„ค์ •์œผ๋กœ hallucination ๋ฐฉ์ง€
            language_prompts = {
                "Korean": "์ด๊ฒƒ์€ ํ•œ๊ตญ์–ด ๋Œ€ํ™”์ž…๋‹ˆ๋‹ค.",
                "English": "This is an English conversation.",
                "Japanese": "ใ“ใ‚Œใฏๆ—ฅๆœฌ่ชžใฎไผš่ฉฑใงใ™ใ€‚",
                "Chinese": "่ฟ™ๆ˜ฏไธญๆ–‡ๅฏน่ฏใ€‚",
            }
            
            prompt = language_prompts.get(src_lang, "")
            
            transcript = await openai.AsyncClient().audio.transcriptions.create(
                model="whisper-1",
                file=audio_file,
                language=src_lang[:2].lower(),
                prompt=prompt,
                temperature=0.0  # ๋” ๋ณด์ˆ˜์ ์ธ ์ถ”๋ก 
            )
        
        os.unlink(tmp_path)  # ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ
        
        # ๊ฒฐ๊ณผ ํ›„์ฒ˜๋ฆฌ - ๋ฐ˜๋ณต๋˜๋Š” ํŒจํ„ด ์ œ๊ฑฐ
        text = transcript.text.strip()
        
        # ๊ฐ™์€ ๋ฌธ์žฅ์ด ๋ฐ˜๋ณต๋˜๋Š” ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
        sentences = text.split('.')
        if len(sentences) > 1:
            unique_sentences = []
            for sent in sentences:
                sent = sent.strip()
                if sent and (not unique_sentences or sent != unique_sentences[-1]):
                    unique_sentences.append(sent)
            text = '. '.join(unique_sentences)
            if text and not text.endswith('.'):
                text += '.'
        
        # ๋‰ด์Šค ๊ด€๋ จ hallucination ํŒจํ„ด ๊ฐ์ง€ ๋ฐ ์ œ๊ฑฐ
        hallucination_patterns = [
            "MBC ๋‰ด์Šค", "KBS ๋‰ด์Šค", "SBS ๋‰ด์Šค", "JTBC ๋‰ด์Šค", 
            "๋‰ด์Šค๋ฃธ", "๋‰ด์Šค๋ฐ์Šคํฌ", "์•ต์ปค", "๊ธฐ์ž์ž…๋‹ˆ๋‹ค"
        ]
        
        # ์งง์€ ํ…์ŠคํŠธ์—์„œ ๋‰ด์Šค ํŒจํ„ด์ด ๊ฐ์ง€๋˜๋ฉด ๋ฌด์‹œ
        if len(text) < 50 and any(pattern in text for pattern in hallucination_patterns):
            return ""
        
        return text
        
    except Exception as e:
        print(f"STT ์˜ค๋ฅ˜: {e}")
        return ""

# โ”€โ”€โ”€ 4. Gradio ์ŠคํŠธ๋ฆผ ํ•ธ๋“ค๋Ÿฌ (๋™๊ธฐ ๋ฒ„์ „) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def realtime_single_sync(audio, src, tgt, state):
    """๋™๊ธฐ ๋ฒ„์ „์˜ ์‹ค์‹œ๊ฐ„ ๋‹จ์ผ ์–ธ์–ด ๋ฒˆ์—ญ"""
    if state is None:
        state = {"orig": "", "trans": "", "audio_buffer": [], "sample_rate": None}
    
    if audio is None:
        # ์ŠคํŠธ๋ฆผ ์ข…๋ฃŒ ์‹œ ๋‚จ์€ ๋ฒ„ํผ ์ฒ˜๋ฆฌ
        if state["audio_buffer"] and state["sample_rate"]:
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            try:
                # ๋ฒ„ํผ์˜ ์˜ค๋””์˜ค ํ•ฉ์น˜๊ธฐ
                combined_audio = np.concatenate(state["audio_buffer"])
                audio_data = (state["sample_rate"], combined_audio)
                
                text = loop.run_until_complete(process_audio_chunk(audio_data, src))
                if text:
                    state["orig"] = state["orig"] + " " + text if state["orig"] else text
                    trans = loop.run_until_complete(gpt_translate(text, src, tgt))
                    state["trans"] = state["trans"] + " " + trans if state["trans"] else trans
            finally:
                loop.close()
            state["audio_buffer"] = []
        
        return state["orig"], state["trans"], state
    
    # ์˜ค๋””์˜ค ๋ฐ์ดํ„ฐ ๋ฒ„ํผ๋ง
    if isinstance(audio, tuple):
        sample_rate, audio_array = audio
        state["sample_rate"] = sample_rate
        state["audio_buffer"].append(audio_array)
        
        # ๋ฒ„ํผ๊ฐ€ ์ถฉ๋ถ„ํžˆ ์Œ“์˜€์„ ๋•Œ๋งŒ ์ฒ˜๋ฆฌ (์•ฝ 2-3์ดˆ ๋ถ„๋Ÿ‰)
        buffer_duration = len(np.concatenate(state["audio_buffer"])) / sample_rate
        if buffer_duration >= 2.0:  # 2์ดˆ๋งˆ๋‹ค ์ฒ˜๋ฆฌ
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            
            try:
                # ๋ฒ„ํผ์˜ ์˜ค๋””์˜ค ํ•ฉ์น˜๊ธฐ
                combined_audio = np.concatenate(state["audio_buffer"])
                audio_data = (sample_rate, combined_audio)
                
                # STT
                text = loop.run_until_complete(process_audio_chunk(audio_data, src))
                if text:
                    state["orig"] = state["orig"] + " " + text if state["orig"] else text
                    
                    # ๋ฒˆ์—ญ
                    trans = loop.run_until_complete(gpt_translate(text, src, tgt))
                    state["trans"] = state["trans"] + " " + trans if state["trans"] else trans
                
                # ๋ฒ„ํผ ์ดˆ๊ธฐํ™”
                state["audio_buffer"] = []
            finally:
                loop.close()
    
    return state["orig"], state["trans"], state

def realtime_four_sync(audio, src, state):
    """๋™๊ธฐ ๋ฒ„์ „์˜ ์‹ค์‹œ๊ฐ„ 4์–ธ์–ด ๋ฒˆ์—ญ"""
    if state is None:
        state = {"orig": "", "English": "", "Chinese": "", "Thai": "", "Russian": "", 
                 "audio_buffer": [], "sample_rate": None}
    
    if audio is None:
        # ์ŠคํŠธ๋ฆผ ์ข…๋ฃŒ ์‹œ ๋‚จ์€ ๋ฒ„ํผ ์ฒ˜๋ฆฌ
        if state["audio_buffer"] and state["sample_rate"]:
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            try:
                combined_audio = np.concatenate(state["audio_buffer"])
                audio_data = (state["sample_rate"], combined_audio)
                
                text = loop.run_until_complete(process_audio_chunk(audio_data, src))
                if text:
                    state["orig"] = state["orig"] + " " + text if state["orig"] else text
                    
                    tasks = []
                    for lang in FOUR:
                        tasks.append(gpt_translate(text, src, lang))
                    
                    translations = loop.run_until_complete(asyncio.gather(*tasks))
                    
                    for lang, trans in zip(FOUR, translations):
                        state[lang] = state[lang] + " " + trans if state[lang] else trans
            finally:
                loop.close()
            state["audio_buffer"] = []
        
        return (state["orig"], state["English"], state["Chinese"], 
                state["Thai"], state["Russian"], state)
    
    # ์˜ค๋””์˜ค ๋ฐ์ดํ„ฐ ๋ฒ„ํผ๋ง
    if isinstance(audio, tuple):
        sample_rate, audio_array = audio
        state["sample_rate"] = sample_rate
        state["audio_buffer"].append(audio_array)
        
        # ๋ฒ„ํผ๊ฐ€ ์ถฉ๋ถ„ํžˆ ์Œ“์˜€์„ ๋•Œ๋งŒ ์ฒ˜๋ฆฌ
        buffer_duration = len(np.concatenate(state["audio_buffer"])) / sample_rate
        if buffer_duration >= 2.0:  # 2์ดˆ๋งˆ๋‹ค ์ฒ˜๋ฆฌ
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            
            try:
                combined_audio = np.concatenate(state["audio_buffer"])
                audio_data = (sample_rate, combined_audio)
                
                # STT
                text = loop.run_until_complete(process_audio_chunk(audio_data, src))
                if text:
                    state["orig"] = state["orig"] + " " + text if state["orig"] else text
                    
                    # 4๊ฐœ ์–ธ์–ด๋กœ ๋ฒˆ์—ญ
                    tasks = []
                    for lang in FOUR:
                        tasks.append(gpt_translate(text, src, lang))
                    
                    translations = loop.run_until_complete(asyncio.gather(*tasks))
                    
                    for lang, trans in zip(FOUR, translations):
                        state[lang] = state[lang] + " " + trans if state[lang] else trans
                
                state["audio_buffer"] = []
            finally:
                loop.close()
    
    return (state["orig"], state["English"], state["Chinese"], 
            state["Thai"], state["Russian"], state)

# โ”€โ”€โ”€ 5. UI โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
with gr.Blocks(title="SMARTok Demo") as demo:
    with gr.Tabs():
        # ํƒญ 1 โ€“ ์˜ค๋””์˜ค ๋ฒˆ์—ญ
        with gr.TabItem("๐ŸŽ™๏ธ ์˜ค๋””์˜ค"):
            src1 = gr.Dropdown(LANG, value="Korean", label="์ž…๋ ฅ ์–ธ์–ด")
            tgt1 = gr.Dropdown(LANG, value="English", label="์ถœ๋ ฅ ์–ธ์–ด")
            aud1 = gr.Audio(sources=["microphone", "upload"], type="filepath")
            btn1 = gr.Button("๋ฒˆ์—ญ")
            o1 = gr.Textbox(label="์›๋ฌธ")
            t1 = gr.Textbox(label="๋ฒˆ์—ญ")
            a1 = gr.Audio(label="TTS", type="filepath", autoplay=True)
            
            btn1.click(translate_audio, [aud1, src1, tgt1], [o1, t1, a1])

        # ํƒญ 2 โ€“ PDF ๋ฒˆ์—ญ
        with gr.TabItem("๐Ÿ“„ PDF"):
            src2 = gr.Dropdown(LANG, value="Korean", label="์ž…๋ ฅ ์–ธ์–ด")
            tgt2 = gr.Dropdown(LANG, value="English", label="์ถœ๋ ฅ ์–ธ์–ด")
            pdf = gr.File(file_types=[".pdf"])
            btn2 = gr.Button("๋ฒˆ์—ญ")
            o2 = gr.Textbox(label="์ถ”์ถœ ์›๋ฌธ", lines=15)
            t2 = gr.Textbox(label="๋ฒˆ์—ญ ๊ฒฐ๊ณผ", lines=15)
            
            btn2.click(translate_pdf, [pdf, src2, tgt2], [o2, t2])

        # ํƒญ 3 โ€“ ์‹ค์‹œ๊ฐ„ 1์–ธ์–ด
        with gr.TabItem("โฑ๏ธ ์‹ค์‹œ๊ฐ„ 1"):
            src3 = gr.Dropdown(LANG, value="Korean", label="์ž…๋ ฅ ์–ธ์–ด")
            tgt3 = gr.Dropdown(LANG, value="English", label="์ถœ๋ ฅ ์–ธ์–ด")
            
            with gr.Row():
                with gr.Column():
                    gr.Markdown("๐ŸŽค **๋งˆ์ดํฌ ์ž…๋ ฅ**")
                    mic3 = gr.Audio(
                        sources=["microphone"], 
                        streaming=True,
                        type="numpy",  # numpy ํ˜•์‹ ๋ช…์‹œ
                        label="๋งˆ์ดํฌ"
                    )
                    gr.Markdown("๐Ÿ’ก **์‚ฌ์šฉ ๋ฐฉ๋ฒ•**\n- 2-3์ดˆ ์ •๋„ ๋ฌธ์žฅ์„ ๋ง์”€ํ•ด์ฃผ์„ธ์š”\n- ๋„ˆ๋ฌด ์งง๊ฑฐ๋‚˜ ๊ธด ๋ฌธ์žฅ์€ ์ธ์‹์ด ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค")
                
                with gr.Column():
                    o3 = gr.Textbox(label="์›๋ฌธ(์‹ค์‹œ๊ฐ„)", lines=8, interactive=False)
                    t3 = gr.Textbox(label="๋ฒˆ์—ญ(์‹ค์‹œ๊ฐ„)", lines=8, interactive=False)
            
            st3 = gr.State()
            
            # stream ๋ฉ”์„œ๋“œ ์ˆ˜์ •
            mic3.stream(
                realtime_single_sync,
                inputs=[mic3, src3, tgt3, st3],
                outputs=[o3, t3, st3],
                time_limit=30,  # 30์ดˆ ์ œํ•œ
                stream_every=0.5  # 0.5์ดˆ๋งˆ๋‹ค ์ŠคํŠธ๋ฆผ
            )

        # ํƒญ 4 โ€“ ์‹ค์‹œ๊ฐ„ 4์–ธ์–ด
        with gr.TabItem("๐ŸŒ ์‹ค์‹œ๊ฐ„ 4"):
            src4 = gr.Dropdown(LANG, value="Korean", label="์ž…๋ ฅ ์–ธ์–ด")
            
            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("๐ŸŽค **๋งˆ์ดํฌ ์ž…๋ ฅ**")
                    mic4 = gr.Audio(
                        sources=["microphone"], 
                        streaming=True,
                        type="numpy",
                        label="๋งˆ์ดํฌ"
                    )
                    o4 = gr.Textbox(label="์›๋ฌธ", lines=8, interactive=False)
                
                with gr.Column(scale=2):
                    with gr.Row():
                        e4 = gr.Textbox(label="English", lines=8, interactive=False)
                        c4 = gr.Textbox(label="Chinese(็ฎ€ไฝ“)", lines=8, interactive=False)
                    with gr.Row():
                        th4 = gr.Textbox(label="Thai", lines=8, interactive=False)
                        r4 = gr.Textbox(label="Russian", lines=8, interactive=False)
            
            st4 = gr.State()
            
            # stream ๋ฉ”์„œ๋“œ ์ˆ˜์ •
            mic4.stream(
                realtime_four_sync,
                inputs=[mic4, src4, st4],
                outputs=[o4, e4, c4, th4, r4, st4],
                time_limit=30,
                stream_every=0.5
            )

demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)