Alina Lozovskaya
commited on
Commit
·
4036cdd
1
Parent(s):
ce72bd9
Move initialization to config.py
Browse files
src/reachy_mini_conversation_demo/main.py
CHANGED
|
@@ -3,7 +3,6 @@ from __future__ import annotations
|
|
| 3 |
import asyncio
|
| 4 |
import json
|
| 5 |
import logging
|
| 6 |
-
import os
|
| 7 |
import random
|
| 8 |
import sys
|
| 9 |
import time
|
|
@@ -11,7 +10,6 @@ import warnings
|
|
| 11 |
import threading
|
| 12 |
|
| 13 |
import numpy as np
|
| 14 |
-
from dotenv import load_dotenv
|
| 15 |
from openai import AsyncOpenAI
|
| 16 |
|
| 17 |
from fastrtc import AdditionalOutputs, AsyncStreamHandler, wait_for_item
|
|
@@ -20,6 +18,7 @@ from websockets import ConnectionClosedError, ConnectionClosedOK
|
|
| 20 |
from reachy_mini import ReachyMini
|
| 21 |
from reachy_mini.utils import create_head_pose
|
| 22 |
|
|
|
|
| 23 |
from reachy_mini_conversation_demo.head_tracker import HeadTracker
|
| 24 |
from reachy_mini_conversation_demo.prompts import SESSION_INSTRUCTIONS
|
| 25 |
from reachy_mini_conversation_demo.tools import (
|
|
@@ -32,10 +31,8 @@ from reachy_mini_conversation_demo.movement import MovementManager
|
|
| 32 |
from reachy_mini_conversation_demo.gstreamer import GstPlayer, GstRecorder
|
| 33 |
from reachy_mini_conversation_demo.vision import VisionManager, init_vision, init_camera
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
|
| 39 |
logging.basicConfig(
|
| 40 |
level=getattr(logging, LOG_LEVEL, logging.INFO),
|
| 41 |
format="%(asctime)s %(levelname)s %(name)s:%(lineno)d | %(message)s",
|
|
@@ -51,47 +48,30 @@ logging.getLogger("aiortc").setLevel(logging.ERROR)
|
|
| 51 |
logging.getLogger("fastrtc").setLevel(logging.ERROR)
|
| 52 |
logging.getLogger("aioice").setLevel(logging.WARNING)
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
SAMPLE_RATE = int(os.getenv("SAMPLE_RATE", "24000"))
|
| 57 |
-
SIM = os.getenv("SIM", "false").lower() in ("true", "1", "yes", "on")
|
| 58 |
-
VISION_ENABLED = os.getenv("VISION_ENABLED", "false").lower() in (
|
| 59 |
-
"true",
|
| 60 |
-
"1",
|
| 61 |
-
"yes",
|
| 62 |
-
"on",
|
| 63 |
-
)
|
| 64 |
-
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-realtime-preview")
|
| 65 |
-
|
| 66 |
-
HEAD_TRACKING = os.getenv("HEAD_TRACKING", "false").lower() in (
|
| 67 |
-
"true",
|
| 68 |
-
"1",
|
| 69 |
-
"yes",
|
| 70 |
-
"on",
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
API_KEY = os.getenv("OPENAI_API_KEY")
|
| 74 |
-
if not API_KEY:
|
| 75 |
-
logger.error("OPENAI_API_KEY not set! Please add it to your .env file.")
|
| 76 |
-
raise RuntimeError("OPENAI_API_KEY missing")
|
| 77 |
masked = (API_KEY[:6] + "..." + API_KEY[-4:]) if len(API_KEY) >= 12 else "<short>"
|
| 78 |
logger.info("OPENAI_API_KEY loaded (prefix): %s", masked)
|
| 79 |
|
| 80 |
# init camera
|
| 81 |
-
|
| 82 |
-
camera = init_camera(camera_index=CAMERA_INDEX, simulation=SIM)
|
| 83 |
|
| 84 |
# Vision manager initialization with proper error handling
|
| 85 |
vision_manager: VisionManager | None = None
|
| 86 |
-
|
| 87 |
if camera and camera.isOpened() and VISION_ENABLED:
|
| 88 |
vision_manager = init_vision(camera=camera)
|
| 89 |
|
| 90 |
# hardware / IO
|
| 91 |
current_robot = ReachyMini()
|
| 92 |
|
| 93 |
-
head_tracker: HeadTracker = None
|
| 94 |
-
|
| 95 |
if HEAD_TRACKING and not SIM:
|
| 96 |
head_tracker = HeadTracker()
|
| 97 |
logger.info("Head tracking enabled")
|
|
@@ -106,7 +86,6 @@ movement_manager = MovementManager(
|
|
| 106 |
robot_is_speaking = asyncio.Event()
|
| 107 |
speaking_queue = asyncio.Queue()
|
| 108 |
|
| 109 |
-
|
| 110 |
# tool deps
|
| 111 |
deps = Deps(
|
| 112 |
reachy_mini=current_robot,
|
|
@@ -421,9 +400,9 @@ stop_event = threading.Event()
|
|
| 421 |
|
| 422 |
async def main():
|
| 423 |
openai = OpenAIRealtimeHandler()
|
| 424 |
-
recorder = GstRecorder()
|
| 425 |
recorder.record()
|
| 426 |
-
player = GstPlayer()
|
| 427 |
player.play()
|
| 428 |
|
| 429 |
movement_manager.set_neutral()
|
|
|
|
| 3 |
import asyncio
|
| 4 |
import json
|
| 5 |
import logging
|
|
|
|
| 6 |
import random
|
| 7 |
import sys
|
| 8 |
import time
|
|
|
|
| 10 |
import threading
|
| 11 |
|
| 12 |
import numpy as np
|
|
|
|
| 13 |
from openai import AsyncOpenAI
|
| 14 |
|
| 15 |
from fastrtc import AdditionalOutputs, AsyncStreamHandler, wait_for_item
|
|
|
|
| 18 |
from reachy_mini import ReachyMini
|
| 19 |
from reachy_mini.utils import create_head_pose
|
| 20 |
|
| 21 |
+
from reachy_mini_conversation_demo.config import config
|
| 22 |
from reachy_mini_conversation_demo.head_tracker import HeadTracker
|
| 23 |
from reachy_mini_conversation_demo.prompts import SESSION_INSTRUCTIONS
|
| 24 |
from reachy_mini_conversation_demo.tools import (
|
|
|
|
| 31 |
from reachy_mini_conversation_demo.gstreamer import GstPlayer, GstRecorder
|
| 32 |
from reachy_mini_conversation_demo.vision import VisionManager, init_vision, init_camera
|
| 33 |
|
| 34 |
+
# logging
|
| 35 |
+
LOG_LEVEL = config.LOG_LEVEL
|
|
|
|
|
|
|
| 36 |
logging.basicConfig(
|
| 37 |
level=getattr(logging, LOG_LEVEL, logging.INFO),
|
| 38 |
format="%(asctime)s %(levelname)s %(name)s:%(lineno)d | %(message)s",
|
|
|
|
| 48 |
logging.getLogger("fastrtc").setLevel(logging.ERROR)
|
| 49 |
logging.getLogger("aioice").setLevel(logging.WARNING)
|
| 50 |
|
| 51 |
+
# Config values
|
| 52 |
+
SAMPLE_RATE = config.SAMPLE_RATE
|
| 53 |
+
SIM = config.SIM
|
| 54 |
+
VISION_ENABLED = config.VISION_ENABLED
|
| 55 |
+
MODEL_NAME = config.MODEL_NAME
|
| 56 |
+
HEAD_TRACKING = config.HEAD_TRACKING
|
| 57 |
+
API_KEY = config.OPENAI_API_KEY
|
| 58 |
|
| 59 |
+
# Key preview in logs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
masked = (API_KEY[:6] + "..." + API_KEY[-4:]) if len(API_KEY) >= 12 else "<short>"
|
| 61 |
logger.info("OPENAI_API_KEY loaded (prefix): %s", masked)
|
| 62 |
|
| 63 |
# init camera
|
| 64 |
+
camera = init_camera(camera_index=0, simulation=SIM)
|
|
|
|
| 65 |
|
| 66 |
# Vision manager initialization with proper error handling
|
| 67 |
vision_manager: VisionManager | None = None
|
|
|
|
| 68 |
if camera and camera.isOpened() and VISION_ENABLED:
|
| 69 |
vision_manager = init_vision(camera=camera)
|
| 70 |
|
| 71 |
# hardware / IO
|
| 72 |
current_robot = ReachyMini()
|
| 73 |
|
| 74 |
+
head_tracker: HeadTracker | None = None
|
|
|
|
| 75 |
if HEAD_TRACKING and not SIM:
|
| 76 |
head_tracker = HeadTracker()
|
| 77 |
logger.info("Head tracking enabled")
|
|
|
|
| 86 |
robot_is_speaking = asyncio.Event()
|
| 87 |
speaking_queue = asyncio.Queue()
|
| 88 |
|
|
|
|
| 89 |
# tool deps
|
| 90 |
deps = Deps(
|
| 91 |
reachy_mini=current_robot,
|
|
|
|
| 400 |
|
| 401 |
async def main():
|
| 402 |
openai = OpenAIRealtimeHandler()
|
| 403 |
+
recorder = GstRecorder(sample_rate=SAMPLE_RATE)
|
| 404 |
recorder.record()
|
| 405 |
+
player = GstPlayer(sample_rate=SAMPLE_RATE)
|
| 406 |
player.play()
|
| 407 |
|
| 408 |
movement_manager.set_neutral()
|