seawolf2357 commited on
Commit
1bbc09a
ยท
verified ยท
1 Parent(s): f297463

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -14
app.py CHANGED
@@ -18,7 +18,6 @@ from fastrtc import (
18
  )
19
  from gradio.utils import get_space
20
  from openai.types.beta.realtime import ResponseAudioTranscriptDoneEvent
21
- from websockets.exceptions import ConnectionClosedOK
22
 
23
  load_dotenv()
24
 
@@ -28,7 +27,9 @@ SAMPLE_RATE = 24000
28
 
29
 
30
  class OpenAIHandler(AsyncStreamHandler):
31
- def __init__(self) -> None:
 
 
32
  super().__init__(
33
  expected_layout="mono",
34
  output_sample_rate=SAMPLE_RATE,
@@ -41,18 +42,16 @@ class OpenAIHandler(AsyncStreamHandler):
41
  def copy(self):
42
  return OpenAIHandler()
43
 
44
- async def start_up(self):
 
 
45
  """Connect to realtime API. Run forever in separate thread to keep connection open."""
46
  self.client = openai.AsyncOpenAI()
47
  async with self.client.beta.realtime.connect(
48
  model="gpt-4o-mini-realtime-preview-2024-12-17"
49
  ) as conn:
50
- # ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ ์ถ”๊ฐ€ ๋ฐ turn_detection ์„ค์ •
51
  await conn.session.update(
52
- session={
53
- "system_prompt": "๋„ˆ์˜ ์ด๋ฆ„์€ '๋น„๋“œ๋ž˜ํ”„ํŠธ'์ด๋‹ค. ๋‹น์‹ ์€ ๋„์›€์ด ๋˜๋Š” ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•ญ์ƒ ์ •ํ™•ํ•˜๊ณ  ์นœ์ ˆํ•˜๊ฒŒ ์‘๋‹ตํ•˜์„ธ์š”.",
54
- "turn_detection": {"type": "server_vad"},
55
- }
56
  )
57
  self.connection = conn
58
  async for event in self.connection:
@@ -74,11 +73,7 @@ class OpenAIHandler(AsyncStreamHandler):
74
  _, array = frame
75
  array = array.squeeze()
76
  audio_message = base64.b64encode(array.tobytes()).decode("utf-8")
77
- try:
78
- await self.connection.input_audio_buffer.append(audio=audio_message) # type: ignore
79
- except ConnectionClosedOK:
80
- # ์—ฐ๊ฒฐ์ด ์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ๋œ ๊ฒฝ์šฐ ์˜ˆ์™ธ๋ฅผ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.
81
- pass
82
 
83
  async def emit(self) -> tuple[int, np.ndarray] | AdditionalOutputs | None:
84
  return await wait_for_item(self.output_queue)
@@ -143,4 +138,4 @@ if __name__ == "__main__":
143
  else:
144
  import uvicorn
145
 
146
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
18
  )
19
  from gradio.utils import get_space
20
  from openai.types.beta.realtime import ResponseAudioTranscriptDoneEvent
 
21
 
22
  load_dotenv()
23
 
 
27
 
28
 
29
  class OpenAIHandler(AsyncStreamHandler):
30
+ def __init__(
31
+ self,
32
+ ) -> None:
33
  super().__init__(
34
  expected_layout="mono",
35
  output_sample_rate=SAMPLE_RATE,
 
42
  def copy(self):
43
  return OpenAIHandler()
44
 
45
+ async def start_up(
46
+ self,
47
+ ):
48
  """Connect to realtime API. Run forever in separate thread to keep connection open."""
49
  self.client = openai.AsyncOpenAI()
50
  async with self.client.beta.realtime.connect(
51
  model="gpt-4o-mini-realtime-preview-2024-12-17"
52
  ) as conn:
 
53
  await conn.session.update(
54
+ session={"turn_detection": {"type": "server_vad"}}
 
 
 
55
  )
56
  self.connection = conn
57
  async for event in self.connection:
 
73
  _, array = frame
74
  array = array.squeeze()
75
  audio_message = base64.b64encode(array.tobytes()).decode("utf-8")
76
+ await self.connection.input_audio_buffer.append(audio=audio_message) # type: ignore
 
 
 
 
77
 
78
  async def emit(self) -> tuple[int, np.ndarray] | AdditionalOutputs | None:
79
  return await wait_for_item(self.output_queue)
 
138
  else:
139
  import uvicorn
140
 
141
+ uvicorn.run(app, host="0.0.0.0", port=7860)