Skip to content

Commit

Permalink
Use connect_with_callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jerhadf committed Oct 31, 2024
1 parent 3bca621 commit b9e13a3
Showing 1 changed file with 46 additions and 20 deletions.
66 changes: 46 additions & 20 deletions computer_use_demo/voice_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,62 @@ def render_voice_controls(self):
st.write("Click 🎤 to start speaking")

async def start_voice_connection(self):
options = ChatConnectOptions(
config_id=os.getenv("HUME_CONFIG_ID"),
secret_key=os.getenv("HUME_SECRET_KEY")
)

try:
async with self.hume_client.empathic_voice.chat.connect() as socket:
# Initialize options for WebSocket connection
options = ChatConnectOptions(
config_id=os.getenv("HUME_CONFIG_ID"),
secret_key=os.getenv("HUME_SECRET_KEY")
)

# Connect with callbacks pattern
async with self.hume_client.empathic_voice.chat.connect_with_callbacks(
options=options,
on_open=self._on_socket_open,
on_message=self._on_socket_message,
on_close=self._on_socket_close,
on_error=self._on_socket_error
) as socket:
self.socket = socket

# Set up message handler
async def on_message(msg):
if msg.type == "user_message":
await self.handle_voice_input(msg.message.content)

# Subscribe to socket messages
await socket.subscribe(on_message)

# Start microphone interface when recording
# Start microphone interface when recording is enabled
while True:
if st.session_state.is_recording:
await MicrophoneInterface.start(
socket,
allow_user_interrupt=True,
byte_stream=self.byte_stream
)
try:
await MicrophoneInterface.start(
socket,
allow_user_interrupt=True,
byte_stream=self.byte_stream
)
except Exception as e:
st.error(f"Microphone error: {str(e)}")
st.session_state.is_recording = False
await asyncio.sleep(0.1)

except Exception as e:
st.error(f"Voice connection error: {str(e)}")
st.session_state.is_recording = False

async def _on_socket_open(self):
"""Called when WebSocket connection opens"""
print("Voice connection opened")

Check failure on line 105 in computer_use_demo/voice_interface.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (T201)

computer_use_demo/voice_interface.py:105:9: T201 `print` found

async def _on_socket_message(self, message):
"""Handle incoming WebSocket messages"""
if message.type == "user_message":
await self.handle_voice_input(message.message.content)
elif message.type == "audio_output":
# Handle audio output if needed
pass

async def _on_socket_close(self):
"""Called when WebSocket connection closes"""
print("Voice connection closed")

Check failure on line 117 in computer_use_demo/voice_interface.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (T201)

computer_use_demo/voice_interface.py:117:9: T201 `print` found

async def _on_socket_error(self, error):
"""Handle WebSocket errors"""
st.error(f"Voice connection error: {str(error)}")
st.session_state.is_recording = False

async def start(self):
self.init_streamlit_state()
await self.start_voice_connection()

Check failure on line 126 in computer_use_demo/voice_interface.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

computer_use_demo/voice_interface.py:126:44: W292 No newline at end of file

0 comments on commit b9e13a3

Please sign in to comment.