Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,327 +1,408 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
from openai import OpenAI
|
4 |
-
import numpy as np
|
5 |
-
import threading
|
6 |
-
import queue
|
7 |
-
import time
|
8 |
-
import json
|
9 |
-
import websocket
|
10 |
-
import base64
|
11 |
-
import pyaudio
|
12 |
-
import wave
|
13 |
-
import io
|
14 |
-
from typing import Generator, Tuple
|
15 |
-
import asyncio
|
16 |
-
import edge_tts
|
17 |
-
|
18 |
-
# OpenAI API ํค ์ค์
|
19 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
20 |
-
client = OpenAI(api_key=OPENAI_API_KEY)
|
21 |
-
|
22 |
-
class RealtimeTranslator:
|
23 |
-
def __init__(self):
|
24 |
-
self.is_recording = False
|
25 |
-
self.audio_queue = queue.Queue()
|
26 |
-
self.text_queue = queue.Queue()
|
27 |
-
self.translation_queue = queue.Queue()
|
28 |
-
self.current_text = ""
|
29 |
-
self.detected_language = None
|
30 |
-
|
31 |
-
def detect_language(self, text: str) -> str:
|
32 |
-
"""ํ
์คํธ์ ์ธ์ด๋ฅผ ๊ฐ์งํฉ๋๋ค."""
|
33 |
-
korean_chars = sum(1 for char in text if ord('๊ฐ') <= ord(char) <= ord('ํฃ'))
|
34 |
-
total_chars = len(text.replace(" ", ""))
|
35 |
-
|
36 |
-
if total_chars > 0:
|
37 |
-
korean_ratio = korean_chars / total_chars
|
38 |
-
if korean_ratio > 0.3:
|
39 |
-
return "ko"
|
40 |
-
return "en"
|
41 |
-
|
42 |
-
def process_audio_chunk(self, audio_chunk):
|
43 |
-
"""์ค๋์ค ์ฒญํฌ๋ฅผ ์ฒ๋ฆฌํ์ฌ ํ
์คํธ๋ก ๋ณํ"""
|
44 |
-
try:
|
45 |
-
# ์ค๋์ค ์ฒญํฌ๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
|
46 |
-
import tempfile
|
47 |
-
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
48 |
-
# WAV ํ์ผ๋ก ์ ์ฅ
|
49 |
-
import wave
|
50 |
-
with wave.open(tmp_file.name, 'wb') as wav_file:
|
51 |
-
wav_file.setnchannels(1)
|
52 |
-
wav_file.setsampwidth(2)
|
53 |
-
wav_file.setframerate(16000)
|
54 |
-
wav_file.writeframes(audio_chunk)
|
55 |
-
|
56 |
-
# Whisper API ํธ์ถ
|
57 |
-
with open(tmp_file.name, "rb") as audio_file:
|
58 |
-
transcript = client.audio.transcriptions.create(
|
59 |
-
model="whisper-1",
|
60 |
-
file=audio_file,
|
61 |
-
language=None,
|
62 |
-
prompt="์ค์๊ฐ ๋ํ๋ฅผ ๋ฒ์ญํฉ๋๋ค."
|
63 |
-
)
|
64 |
-
|
65 |
-
return transcript.text
|
66 |
-
|
67 |
-
except Exception as e:
|
68 |
-
print(f"์์ฑ ์ธ์ ์ค๋ฅ: {e}")
|
69 |
-
return ""
|
70 |
-
|
71 |
-
def translate_stream(self, text: str, source_lang: str) -> str:
|
72 |
-
"""ํ
์คํธ๋ฅผ ์ค์๊ฐ์ผ๋ก ๋ฒ์ญ"""
|
73 |
-
try:
|
74 |
-
if not text or text.strip() == "":
|
75 |
-
return ""
|
76 |
-
|
77 |
-
# ๋ฒ์ญ ํ๋กฌํํธ
|
78 |
-
if source_lang == "ko":
|
79 |
-
messages = [
|
80 |
-
{"role": "system", "content": "์ค์๊ฐ ํต์ญ์ฌ์
๋๋ค. ํ๊ตญ์ด๋ฅผ ์์ด๋ก ์ฆ์ ๋ฒ์ญํฉ๋๋ค."},
|
81 |
-
{"role": "user", "content": text}
|
82 |
-
]
|
83 |
-
else:
|
84 |
-
messages = [
|
85 |
-
{"role": "system", "content": "์ค์๊ฐ ํต์ญ์ฌ์
๋๋ค. ์์ด๋ฅผ ํ๊ตญ์ด๋ก ์ฆ์ ๋ฒ์ญํฉ๋๋ค."},
|
86 |
-
{"role": "user", "content": text}
|
87 |
-
]
|
88 |
-
|
89 |
-
# ์คํธ๋ฆฌ๋ฐ ์๋ต
|
90 |
-
stream = client.chat.completions.create(
|
91 |
-
model="gpt-4o-mini",
|
92 |
-
messages=messages,
|
93 |
-
stream=True,
|
94 |
-
temperature=0.3,
|
95 |
-
max_tokens=150
|
96 |
-
)
|
97 |
-
|
98 |
-
translated = ""
|
99 |
-
for chunk in stream:
|
100 |
-
if chunk.choices[0].delta.content:
|
101 |
-
translated += chunk.choices[0].delta.content
|
102 |
-
|
103 |
-
return translated
|
104 |
-
|
105 |
-
except Exception as e:
|
106 |
-
print(f"๋ฒ์ญ ์ค๋ฅ: {e}")
|
107 |
-
return ""
|
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 |
-
elem_id="audio-stream"
|
165 |
-
)
|
166 |
-
|
167 |
-
with gr.Column(scale=1):
|
168 |
-
status_text = gr.Textbox(
|
169 |
-
label="๐ ์ํ",
|
170 |
-
value="๐ด ๋ง์ดํฌ๋ฅผ ์ผ๊ณ ๋ง์ํด์ฃผ์ธ์",
|
171 |
-
interactive=False
|
172 |
-
)
|
173 |
-
|
174 |
-
with gr.Row():
|
175 |
-
with gr.Column():
|
176 |
-
korean_output = gr.Textbox(
|
177 |
-
label="๐ฐ๐ท ํ๊ตญ์ด",
|
178 |
-
placeholder="ํ๊ตญ์ด๊ฐ ์ค์๊ฐ์ผ๋ก ํ์๋ฉ๋๋ค",
|
179 |
-
lines=8,
|
180 |
-
interactive=False,
|
181 |
-
elem_id="korean-text"
|
182 |
-
)
|
183 |
-
|
184 |
-
with gr.Column():
|
185 |
-
english_output = gr.Textbox(
|
186 |
-
label="๐บ๐ธ English",
|
187 |
-
placeholder="English translation appears here in real-time",
|
188 |
-
lines=8,
|
189 |
-
interactive=False,
|
190 |
-
elem_id="english-text"
|
191 |
-
)
|
192 |
-
|
193 |
-
# ์คํธ๋ฆฌ๋ฐ ์ด๋ฒคํธ ์ค์
|
194 |
-
audio_input.stream(
|
195 |
-
fn=process_stream,
|
196 |
-
inputs=[audio_input],
|
197 |
-
outputs=[status_text, korean_output, english_output],
|
198 |
-
show_progress=False
|
199 |
-
)
|
200 |
-
|
201 |
-
gr.Markdown(
|
202 |
-
"""
|
203 |
-
---
|
204 |
-
|
205 |
-
### ๐ก ์ฌ์ฉ ํ:
|
206 |
-
- ๋ช
ํํ๊ฒ ๋งํ ์๋ก ์ธ์๋ฅ ์ด ๋์์ง๋๋ค
|
207 |
-
- ๋ฌธ์ฅ์ด ๋๋ ๋๊น์ง ์ ์ ๋ฉ์ถ๋ฉด ๋ ์ ํํ ๋ฒ์ญ์ด ๋ฉ๋๋ค
|
208 |
-
- ํ๊ตญ์ด์ ์์ด๋ฅผ ์๋์ผ๋ก ๊ฐ์งํฉ๋๋ค
|
209 |
-
|
210 |
-
### โ๏ธ ๊ธฐ์ ์ฌ์:
|
211 |
-
- **์์ฑ ์ธ์**: OpenAI Whisper (์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ)
|
212 |
-
- **๋ฒ์ญ**: GPT-4 (์คํธ๋ฆฌ๋ฐ ๋ชจ๋)
|
213 |
-
- **์ง์ฐ ์๊ฐ**: ~1-2์ด
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import React, { useState, useRef, useEffect } from 'react';
|
2 |
+
import { Mic, MicOff, Volume2, Globe, Loader2 } from 'lucide-react';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
const RealtimeVoiceTranslator = () => {
|
5 |
+
const [isConnected, setIsConnected] = useState(false);
|
6 |
+
const [isRecording, setIsRecording] = useState(false);
|
7 |
+
const [transcript, setTranscript] = useState('');
|
8 |
+
const [translation, setTranslation] = useState('');
|
9 |
+
const [sourceLanguage, setSourceLanguage] = useState('ko');
|
10 |
+
const [targetLanguage, setTargetLanguage] = useState('en');
|
11 |
+
const [ephemeralKey, setEphemeralKey] = useState('');
|
12 |
+
const [logs, setLogs] = useState([]);
|
13 |
+
const [isLoading, setIsLoading] = useState(false);
|
14 |
|
15 |
+
const pcRef = useRef(null);
|
16 |
+
const dcRef = useRef(null);
|
17 |
+
const audioElRef = useRef(null);
|
18 |
+
|
19 |
+
// ๋ก๊ทธ ์ถ๊ฐ ํจ์
|
20 |
+
const addLog = (message, type = 'info') => {
|
21 |
+
const timestamp = new Date().toLocaleTimeString();
|
22 |
+
setLogs(prev => [...prev, { message, type, timestamp }]);
|
23 |
+
};
|
24 |
+
|
25 |
+
// Ephemeral key ๊ฐ์ ธ์ค๊ธฐ (์๋ฎฌ๋ ์ด์
)
|
26 |
+
const getEphemeralKey = async () => {
|
27 |
+
// ์ค์ ๊ตฌํ์์๋ ์๋ฒ์์ ephemeral key๋ฅผ ๊ฐ์ ธ์์ผ ํฉ๋๋ค
|
28 |
+
addLog('Ephemeral key๊ฐ ํ์ํฉ๋๋ค. ์๋ฒ ์๋ํฌ์ธํธ๋ฅผ ๊ตฌํํด์ฃผ์ธ์.', 'warning');
|
29 |
+
return 'YOUR_EPHEMERAL_KEY';
|
30 |
+
};
|
31 |
+
|
32 |
+
// WebRTC ์ฐ๊ฒฐ ์ด๊ธฐํ
|
33 |
+
const initializeConnection = async () => {
|
34 |
+
try {
|
35 |
+
setIsLoading(true);
|
36 |
+
addLog('์ฐ๊ฒฐ ์ด๊ธฐํ ์ค...', 'info');
|
37 |
+
|
38 |
+
// Ephemeral key ๊ฐ์ ธ์ค๊ธฐ
|
39 |
+
const key = await getEphemeralKey();
|
40 |
+
setEphemeralKey(key);
|
41 |
+
|
42 |
+
// Peer connection ์์ฑ
|
43 |
+
const pc = new RTCPeerConnection({
|
44 |
+
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
45 |
+
});
|
46 |
+
pcRef.current = pc;
|
47 |
+
|
48 |
+
// ์๊ฒฉ ์ค๋์ค ์คํธ๋ฆผ ์ค์
|
49 |
+
const audioEl = audioElRef.current;
|
50 |
+
pc.ontrack = (e) => {
|
51 |
+
audioEl.srcObject = e.streams[0];
|
52 |
+
addLog('์ค๋์ค ์คํธ๋ฆผ ์ฐ๊ฒฐ๋จ', 'success');
|
53 |
+
};
|
54 |
+
|
55 |
+
// ๋ก์ปฌ ์ค๋์ค ํธ๋ ์ถ๊ฐ
|
56 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
57 |
+
stream.getTracks().forEach(track => {
|
58 |
+
pc.addTrack(track, stream);
|
59 |
+
});
|
60 |
+
|
61 |
+
// ๋ฐ์ดํฐ ์ฑ๋ ์ค์
|
62 |
+
const dc = pc.createDataChannel('oai-events');
|
63 |
+
dcRef.current = dc;
|
64 |
+
|
65 |
+
dc.addEventListener('open', () => {
|
66 |
+
addLog('๋ฐ์ดํฐ ์ฑ๋ ์ด๋ฆผ', 'success');
|
67 |
+
setIsConnected(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
// ์ธ์
์ค์
|
70 |
+
const sessionConfig = {
|
71 |
+
type: 'session.update',
|
72 |
+
session: {
|
73 |
+
modalities: ['text', 'audio'],
|
74 |
+
instructions: `You are a helpful translator. Translate between ${sourceLanguage === 'ko' ? 'Korean' : 'English'} and ${targetLanguage === 'ko' ? 'Korean' : 'English'}.`,
|
75 |
+
voice: 'alloy',
|
76 |
+
input_audio_format: 'pcm16',
|
77 |
+
output_audio_format: 'pcm16',
|
78 |
+
input_audio_transcription: {
|
79 |
+
model: 'whisper-1'
|
80 |
+
},
|
81 |
+
turn_detection: {
|
82 |
+
type: 'server_vad',
|
83 |
+
threshold: 0.5,
|
84 |
+
prefix_padding_ms: 300,
|
85 |
+
silence_duration_ms: 500
|
86 |
+
}
|
87 |
+
}
|
88 |
+
};
|
89 |
+
dc.send(JSON.stringify(sessionConfig));
|
90 |
+
});
|
91 |
+
|
92 |
+
dc.addEventListener('message', (e) => {
|
93 |
+
const event = JSON.parse(e.data);
|
94 |
+
handleRealtimeEvent(event);
|
95 |
+
});
|
96 |
+
|
97 |
+
dc.addEventListener('error', (error) => {
|
98 |
+
addLog(`๋ฐ์ดํฐ ์ฑ๋ ์ค๋ฅ: ${error}`, 'error');
|
99 |
+
});
|
100 |
+
|
101 |
+
// SDP ์คํผ ์์ฑ ๋ฐ ์ฐ๊ฒฐ
|
102 |
+
const offer = await pc.createOffer();
|
103 |
+
await pc.setLocalDescription(offer);
|
104 |
+
|
105 |
+
// API ์ฐ๊ฒฐ (์ค์ ๊ตฌํ ์ ์ฌ์ฉ)
|
106 |
+
/*
|
107 |
+
const baseUrl = 'https://api.openai.com/v1/realtime';
|
108 |
+
const model = 'gpt-4o-realtime-preview-2025-06-03';
|
109 |
+
const response = await fetch(`${baseUrl}?model=${model}`, {
|
110 |
+
method: 'POST',
|
111 |
+
body: offer.sdp,
|
112 |
+
headers: {
|
113 |
+
'Authorization': `Bearer ${key}`,
|
114 |
+
'Content-Type': 'application/sdp'
|
115 |
}
|
116 |
+
});
|
117 |
+
|
118 |
+
const answerSdp = await response.text();
|
119 |
+
const answer = {
|
120 |
+
type: 'answer',
|
121 |
+
sdp: answerSdp
|
122 |
+
};
|
123 |
+
await pc.setRemoteDescription(answer);
|
124 |
+
*/
|
125 |
+
|
126 |
+
addLog('WebRTC ์ฐ๊ฒฐ ์๋ฎฌ๋ ์ด์
์๋ฃ', 'warning');
|
127 |
+
setIsConnected(true);
|
128 |
+
setIsLoading(false);
|
129 |
+
|
130 |
+
} catch (error) {
|
131 |
+
addLog(`์ฐ๊ฒฐ ์ค๋ฅ: ${error.message}`, 'error');
|
132 |
+
setIsLoading(false);
|
133 |
+
}
|
134 |
+
};
|
135 |
+
|
136 |
+
// Realtime ์ด๋ฒคํธ ์ฒ๋ฆฌ
|
137 |
+
const handleRealtimeEvent = (event) => {
|
138 |
+
switch (event.type) {
|
139 |
+
case 'conversation.item.created':
|
140 |
+
if (event.item.role === 'user') {
|
141 |
+
setTranscript(event.item.content?.[0]?.transcript || '');
|
142 |
+
addLog(`์ฌ์ฉ์: ${event.item.content?.[0]?.transcript}`, 'info');
|
143 |
}
|
144 |
+
break;
|
145 |
+
|
146 |
+
case 'conversation.item.input_audio_transcription.completed':
|
147 |
+
setTranscript(event.transcript);
|
148 |
+
addLog(`์ ์ฌ ์๋ฃ: ${event.transcript}`, 'info');
|
149 |
+
// ๋ฒ์ญ ์์ฒญ
|
150 |
+
requestTranslation(event.transcript);
|
151 |
+
break;
|
152 |
+
|
153 |
+
case 'response.audio_transcript.delta':
|
154 |
+
setTranslation(prev => prev + event.delta);
|
155 |
+
break;
|
156 |
+
|
157 |
+
case 'response.audio_transcript.done':
|
158 |
+
addLog(`๋ฒ์ญ ์๋ฃ: ${translation}`, 'success');
|
159 |
+
break;
|
160 |
+
|
161 |
+
case 'error':
|
162 |
+
addLog(`์ค๋ฅ: ${event.error.message}`, 'error');
|
163 |
+
break;
|
164 |
+
|
165 |
+
default:
|
166 |
+
console.log('Unhandled event:', event);
|
167 |
+
}
|
168 |
+
};
|
169 |
+
|
170 |
+
// ๋ฒ์ญ ์์ฒญ
|
171 |
+
const requestTranslation = (text) => {
|
172 |
+
if (!dcRef.current || dcRef.current.readyState !== 'open') return;
|
173 |
+
|
174 |
+
const message = {
|
175 |
+
type: 'conversation.item.create',
|
176 |
+
item: {
|
177 |
+
type: 'message',
|
178 |
+
role: 'user',
|
179 |
+
content: [{
|
180 |
+
type: 'input_text',
|
181 |
+
text: `Translate this to ${targetLanguage === 'ko' ? 'Korean' : 'English'}: "${text}"`
|
182 |
+
}]
|
183 |
+
}
|
184 |
+
};
|
185 |
+
|
186 |
+
dcRef.current.send(JSON.stringify(message));
|
187 |
|
188 |
+
// ์๋ต ์์ฑ ์์ฒญ
|
189 |
+
dcRef.current.send(JSON.stringify({ type: 'response.create' }));
|
190 |
+
};
|
191 |
+
|
192 |
+
// ๋
น์ ์์/์ค์ง
|
193 |
+
const toggleRecording = () => {
|
194 |
+
if (!isConnected) {
|
195 |
+
addLog('๋จผ์ ์ฐ๊ฒฐ์ ์์ํด์ฃผ์ธ์', 'warning');
|
196 |
+
return;
|
197 |
+
}
|
198 |
+
|
199 |
+
setIsRecording(!isRecording);
|
200 |
|
201 |
+
if (!isRecording) {
|
202 |
+
addLog('๋
น์ ์์', 'info');
|
203 |
+
// ์์ฑ ์
๋ ฅ ์์์ ์ํ ์ด๋ฒคํธ ์ ์ก
|
204 |
+
if (dcRef.current && dcRef.current.readyState === 'open') {
|
205 |
+
dcRef.current.send(JSON.stringify({
|
206 |
+
type: 'input_audio_buffer.commit'
|
207 |
+
}));
|
208 |
+
}
|
209 |
+
} else {
|
210 |
+
addLog('๋
น์ ์ค์ง', 'info');
|
211 |
+
}
|
212 |
+
};
|
213 |
+
|
214 |
+
// ์ธ์ด ์ ํ
|
215 |
+
const swapLanguages = () => {
|
216 |
+
setSourceLanguage(targetLanguage);
|
217 |
+
setTargetLanguage(sourceLanguage);
|
218 |
+
addLog(`์ธ์ด ์ ํ: ${targetLanguage} โ ${sourceLanguage}`, 'info');
|
219 |
+
};
|
220 |
+
|
221 |
+
// ์ฐ๊ฒฐ ์ข
๋ฃ
|
222 |
+
const disconnect = () => {
|
223 |
+
if (pcRef.current) {
|
224 |
+
pcRef.current.close();
|
225 |
+
pcRef.current = null;
|
226 |
+
}
|
227 |
+
setIsConnected(false);
|
228 |
+
setIsRecording(false);
|
229 |
+
addLog('์ฐ๊ฒฐ ์ข
๋ฃ๋จ', 'info');
|
230 |
+
};
|
231 |
+
|
232 |
+
// ์ปดํฌ๋ํธ ์ธ๋ง์ดํธ ์ ์ ๋ฆฌ
|
233 |
+
useEffect(() => {
|
234 |
+
return () => {
|
235 |
+
if (pcRef.current) {
|
236 |
+
pcRef.current.close();
|
237 |
+
}
|
238 |
+
};
|
239 |
+
}, []);
|
240 |
+
|
241 |
+
return (
|
242 |
+
<div className="min-h-screen bg-gray-50 p-4">
|
243 |
+
<div className="max-w-4xl mx-auto">
|
244 |
+
<h1 className="text-3xl font-bold text-center mb-8 text-gray-800">
|
245 |
+
์ค์๊ฐ ์์ฑ ๋ฒ์ญ๊ธฐ
|
246 |
+
</h1>
|
247 |
+
|
248 |
+
{/* ์ฐ๊ฒฐ ์ํ */}
|
249 |
+
<div className="bg-white rounded-lg shadow-md p-6 mb-6">
|
250 |
+
<div className="flex items-center justify-between mb-4">
|
251 |
+
<h2 className="text-xl font-semibold">์ฐ๊ฒฐ ์ํ</h2>
|
252 |
+
<div className="flex items-center gap-2">
|
253 |
+
<div className={`w-3 h-3 rounded-full ${isConnected ? 'bg-green-500' : 'bg-red-500'}`} />
|
254 |
+
<span className="text-sm">{isConnected ? '์ฐ๊ฒฐ๋จ' : '์ฐ๊ฒฐ ์๋จ'}</span>
|
255 |
+
</div>
|
256 |
+
</div>
|
257 |
+
|
258 |
+
<div className="flex gap-4">
|
259 |
+
{!isConnected ? (
|
260 |
+
<button
|
261 |
+
onClick={initializeConnection}
|
262 |
+
disabled={isLoading}
|
263 |
+
className="flex items-center gap-2 px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 disabled:bg-gray-400"
|
264 |
+
>
|
265 |
+
{isLoading ? (
|
266 |
+
<>
|
267 |
+
<Loader2 className="w-4 h-4 animate-spin" />
|
268 |
+
์ฐ๊ฒฐ ์ค...
|
269 |
+
</>
|
270 |
+
) : (
|
271 |
+
'์ฐ๊ฒฐ ์์'
|
272 |
+
)}
|
273 |
+
</button>
|
274 |
+
) : (
|
275 |
+
<button
|
276 |
+
onClick={disconnect}
|
277 |
+
className="px-4 py-2 bg-red-500 text-white rounded-lg hover:bg-red-600"
|
278 |
+
>
|
279 |
+
์ฐ๊ฒฐ ์ข
๋ฃ
|
280 |
+
</button>
|
281 |
+
)}
|
282 |
+
</div>
|
283 |
+
</div>
|
284 |
+
|
285 |
+
{/* ์ธ์ด ์ ํ */}
|
286 |
+
<div className="bg-white rounded-lg shadow-md p-6 mb-6">
|
287 |
+
<div className="flex items-center justify-between gap-4">
|
288 |
+
<div className="flex-1">
|
289 |
+
<label className="block text-sm font-medium mb-2">์
๋ ฅ ์ธ์ด</label>
|
290 |
+
<select
|
291 |
+
value={sourceLanguage}
|
292 |
+
onChange={(e) => setSourceLanguage(e.target.value)}
|
293 |
+
className="w-full p-2 border rounded-lg"
|
294 |
+
>
|
295 |
+
<option value="ko">ํ๊ตญ์ด</option>
|
296 |
+
<option value="en">์์ด</option>
|
297 |
+
</select>
|
298 |
+
</div>
|
299 |
|
300 |
+
<button
|
301 |
+
onClick={swapLanguages}
|
302 |
+
className="mt-6 p-2 hover:bg-gray-100 rounded-lg"
|
303 |
+
>
|
304 |
+
<Globe className="w-6 h-6" />
|
305 |
+
</button>
|
306 |
|
307 |
+
<div className="flex-1">
|
308 |
+
<label className="block text-sm font-medium mb-2">์ถ๋ ฅ ์ธ์ด</label>
|
309 |
+
<select
|
310 |
+
value={targetLanguage}
|
311 |
+
onChange={(e) => setTargetLanguage(e.target.value)}
|
312 |
+
className="w-full p-2 border rounded-lg"
|
313 |
+
>
|
314 |
+
<option value="en">์์ด</option>
|
315 |
+
<option value="ko">ํ๊ตญ์ด</option>
|
316 |
+
</select>
|
317 |
+
</div>
|
318 |
+
</div>
|
319 |
+
</div>
|
320 |
+
|
321 |
+
{/* ๋
น์ ์ปจํธ๋กค */}
|
322 |
+
<div className="bg-white rounded-lg shadow-md p-6 mb-6">
|
323 |
+
<div className="flex flex-col items-center">
|
324 |
+
<button
|
325 |
+
onClick={toggleRecording}
|
326 |
+
disabled={!isConnected}
|
327 |
+
className={`p-6 rounded-full transition-colors ${
|
328 |
+
isRecording
|
329 |
+
? 'bg-red-500 hover:bg-red-600'
|
330 |
+
: 'bg-blue-500 hover:bg-blue-600 disabled:bg-gray-400'
|
331 |
+
} text-white`}
|
332 |
+
>
|
333 |
+
{isRecording ? (
|
334 |
+
<MicOff className="w-8 h-8" />
|
335 |
+
) : (
|
336 |
+
<Mic className="w-8 h-8" />
|
337 |
+
)}
|
338 |
+
</button>
|
339 |
+
<p className="mt-4 text-sm text-gray-600">
|
340 |
+
{isRecording ? '๋
น์ ์ค... ํด๋ฆญํ์ฌ ์ค์ง' : 'ํด๋ฆญํ์ฌ ๋
น์ ์์'}
|
341 |
+
</p>
|
342 |
+
</div>
|
343 |
+
</div>
|
344 |
+
|
345 |
+
{/* ๊ฒฐ๊ณผ ํ์ */}
|
346 |
+
<div className="bg-white rounded-lg shadow-md p-6 mb-6">
|
347 |
+
<h3 className="text-lg font-semibold mb-4">๋ฒ์ญ ๊ฒฐ๊ณผ</h3>
|
348 |
+
|
349 |
+
<div className="space-y-4">
|
350 |
+
<div>
|
351 |
+
<label className="block text-sm font-medium mb-2">์๋ณธ ํ
์คํธ</label>
|
352 |
+
<div className="p-4 bg-gray-50 rounded-lg min-h-[80px]">
|
353 |
+
{transcript || <span className="text-gray-400">์์ฑ ์
๋ ฅ์ ๊ธฐ๋ค๋ฆฌ๋ ์ค...</span>}
|
354 |
+
</div>
|
355 |
+
</div>
|
356 |
+
|
357 |
+
<div>
|
358 |
+
<label className="block text-sm font-medium mb-2">๋ฒ์ญ๋ ํ
์คํธ</label>
|
359 |
+
<div className="p-4 bg-blue-50 rounded-lg min-h-[80px]">
|
360 |
+
{translation || <span className="text-gray-400">๋ฒ์ญ ๊ฒฐ๊ณผ๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค...</span>}
|
361 |
+
</div>
|
362 |
+
</div>
|
363 |
+
</div>
|
364 |
+
</div>
|
365 |
+
|
366 |
+
{/* ๋ก๊ทธ */}
|
367 |
+
<div className="bg-white rounded-lg shadow-md p-6">
|
368 |
+
<h3 className="text-lg font-semibold mb-4">ํ๋ ๋ก๊ทธ</h3>
|
369 |
+
<div className="h-48 overflow-y-auto bg-gray-50 rounded-lg p-4 text-sm">
|
370 |
+
{logs.length === 0 ? (
|
371 |
+
<p className="text-gray-400">๋ก๊ทธ๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค...</p>
|
372 |
+
) : (
|
373 |
+
logs.map((log, index) => (
|
374 |
+
<div
|
375 |
+
key={index}
|
376 |
+
className={`mb-2 ${
|
377 |
+
log.type === 'error' ? 'text-red-600' :
|
378 |
+
log.type === 'success' ? 'text-green-600' :
|
379 |
+
log.type === 'warning' ? 'text-yellow-600' :
|
380 |
+
'text-gray-700'
|
381 |
+
}`}
|
382 |
+
>
|
383 |
+
<span className="text-gray-500">[{log.timestamp}]</span> {log.message}
|
384 |
+
</div>
|
385 |
+
))
|
386 |
+
)}
|
387 |
+
</div>
|
388 |
+
</div>
|
389 |
+
|
390 |
+
{/* ์ค๋์ค ์๋ฆฌ๋จผํธ (์จ๊น) */}
|
391 |
+
<audio ref={audioElRef} autoPlay style={{ display: 'none' }} />
|
392 |
+
|
393 |
+
{/* ์ฌ์ฉ ์๋ด */}
|
394 |
+
<div className="mt-6 p-4 bg-yellow-50 rounded-lg">
|
395 |
+
<h4 className="font-semibold text-yellow-800 mb-2">โ ๏ธ ์ค์ ์๋ด</h4>
|
396 |
+
<ul className="text-sm text-yellow-700 space-y-1">
|
397 |
+
<li>โข ์ด ์ ํ๋ฆฌ์ผ์ด์
์ ์ฌ์ฉํ๋ ค๋ฉด ์๋ฒ์์ ephemeral key๋ฅผ ์์ฑํ๋ ์๋ํฌ์ธํธ๊ฐ ํ์ํฉ๋๋ค.</li>
|
398 |
+
<li>โข OpenAI API ํค๊ฐ ํ์ํ๋ฉฐ, ํด๋ผ์ด์ธํธ ์ธก์์ ์ง์ ์ฌ์ฉํ๋ฉด ์ ๋ฉ๋๋ค.</li>
|
399 |
+
<li>โข ์ค์ ๊ตฌํ ์ ์ฃผ์ ์ฒ๋ฆฌ๋ API ์ฐ๊ฒฐ ์ฝ๋๋ฅผ ํ์ฑํํด์ผ ํฉ๋๋ค.</li>
|
400 |
+
<li>โข ๋ง์ดํฌ ๊ถํ์ด ํ์ํฉ๋๋ค.</li>
|
401 |
+
</ul>
|
402 |
+
</div>
|
403 |
+
</div>
|
404 |
+
</div>
|
405 |
+
);
|
406 |
+
};
|
407 |
+
|
408 |
+
export default RealtimeVoiceTranslator;
|