danegergo commited on
Commit
e4de5ab
·
1 Parent(s): 0adaf09

fixed shared transcript issue

Browse files
Files changed (1) hide show
  1. app.py +5 -6
app.py CHANGED
@@ -31,7 +31,6 @@ WEBSOCKET_HEADERS = {
31
  "OpenAI-Beta": "realtime=v1"
32
  }
33
 
34
- transcription = ""
35
  css = """
36
  """
37
 
@@ -45,6 +44,7 @@ class WebSocketClient:
45
  self.queue = asyncio.Queue(maxsize=10)
46
  self.loop = None
47
  self.client_id = client_id
 
48
 
49
  async def connect(self):
50
  try:
@@ -66,15 +66,14 @@ class WebSocketClient:
66
  self.loop.run_until_complete(self.connect())
67
 
68
  def process_websocket_message(self, message: Data):
69
- global transcription
70
  message_object = json.loads(message)
71
  if message_object["type"] != "error":
72
  print(f"{LogColors.OK}Received message: {LogColors.ENDC} {message}")
73
  if message_object["type"] == "conversation.item.input_audio_transcription.delta":
74
  delta = message_object["delta"]
75
- transcription += delta
76
  elif message_object["type"] == "conversation.item.input_audio_transcription.completed":
77
- transcription += ' ' if len(transcription) and transcription[-1] != ' ' else ''
78
  else:
79
  print(f"{LogColors.ERROR}Error: {message}{LogColors.ENDC}")
80
 
@@ -130,7 +129,7 @@ def send_audio_chunk(new_chunk: gr.Audio, client_id: str):
130
  return "Connection is being established, please try again in a few seconds."
131
  sr, y = new_chunk
132
  connections[client_id].enqueue_audio_chunk(sr, y)
133
- return transcription
134
 
135
  def create_new_websocket_connection():
136
  client_id = str(uuid.uuid4())
@@ -143,7 +142,7 @@ if __name__ == "__main__":
143
  gr.Markdown(f"# Realtime transcription demo")
144
  with gr.Row():
145
  with gr.Column():
146
- output_textbox = gr.Textbox(label="Transcription", value="", lines=7, interactive=False, autoscroll=True)
147
  with gr.Row():
148
  with gr.Column(scale=5):
149
  audio_input = gr.Audio(streaming=True, format="wav")
 
31
  "OpenAI-Beta": "realtime=v1"
32
  }
33
 
 
34
  css = """
35
  """
36
 
 
44
  self.queue = asyncio.Queue(maxsize=10)
45
  self.loop = None
46
  self.client_id = client_id
47
+ self.transcript = ""
48
 
49
  async def connect(self):
50
  try:
 
66
  self.loop.run_until_complete(self.connect())
67
 
68
  def process_websocket_message(self, message: Data):
 
69
  message_object = json.loads(message)
70
  if message_object["type"] != "error":
71
  print(f"{LogColors.OK}Received message: {LogColors.ENDC} {message}")
72
  if message_object["type"] == "conversation.item.input_audio_transcription.delta":
73
  delta = message_object["delta"]
74
+ self.transcript += delta
75
  elif message_object["type"] == "conversation.item.input_audio_transcription.completed":
76
+ self.transcript += ' ' if len(self.transcript) and self.transcript[-1] != ' ' else ''
77
  else:
78
  print(f"{LogColors.ERROR}Error: {message}{LogColors.ENDC}")
79
 
 
129
  return "Connection is being established, please try again in a few seconds."
130
  sr, y = new_chunk
131
  connections[client_id].enqueue_audio_chunk(sr, y)
132
+ return connections[client_id].transcript
133
 
134
  def create_new_websocket_connection():
135
  client_id = str(uuid.uuid4())
 
142
  gr.Markdown(f"# Realtime transcription demo")
143
  with gr.Row():
144
  with gr.Column():
145
+ output_textbox = gr.Textbox(label="Transcript", value="", lines=7, interactive=False, autoscroll=True)
146
  with gr.Row():
147
  with gr.Column(scale=5):
148
  audio_input = gr.Audio(streaming=True, format="wav")