From 8bbdaf419d316ff6167f2d8eaadfeae651d865e0 Mon Sep 17 00:00:00 2001 From: Antonio Bono Date: Tue, 16 Apr 2024 20:37:50 +0200 Subject: [PATCH] fix py --- hni_cpp/launch/chat_launch.py | 6 +- ...ent_launch.py => experiment_nao_launch.py} | 3 +- hni_cpp/launch/experiment_pc_launch.py | 14 +++++ hni_py/hni_py/chat_client.py | 0 hni_py/hni_py/chat_service.py | 22 +++---- hni_py/hni_py/face_track_client.py | 0 hni_py/hni_py/face_track_server.py | 2 +- hni_py/hni_py/g2stt_service.py | 0 hni_py/hni_py/gstt_service.py | 28 ++++----- hni_py/hni_py/gtts_service.py | 14 +++-- .../transcribe_streaming_mic.py | 0 hni_py/setup.py | 57 +++++++++++++++++++ 12 files changed, 109 insertions(+), 37 deletions(-) rename hni_cpp/launch/{experiment_launch.py => experiment_nao_launch.py} (93%) create mode 100644 hni_cpp/launch/experiment_pc_launch.py mode change 100644 => 100755 hni_py/hni_py/chat_client.py mode change 100644 => 100755 hni_py/hni_py/chat_service.py mode change 100644 => 100755 hni_py/hni_py/face_track_client.py mode change 100644 => 100755 hni_py/hni_py/g2stt_service.py rename hni_py/hni_py/{__pycache__ => }/transcribe_streaming_mic.py (100%) diff --git a/hni_cpp/launch/chat_launch.py b/hni_cpp/launch/chat_launch.py index e021da4..530b23a 100644 --- a/hni_cpp/launch/chat_launch.py +++ b/hni_cpp/launch/chat_launch.py @@ -17,19 +17,19 @@ def generate_launch_description(): ), Node( package='hni_py', - executable='gstt_service.py', + executable='gstt_service', name='gstt_service_node', output='screen' ), Node( package='hni_py', - executable='chat_service.py', + executable='chat_service', name='chat_service_node', output='screen' ), Node( package='hni_py', - executable='gtts_service.py', + executable='gtts_service', name='gtts_service_node', output='screen' ), diff --git a/hni_cpp/launch/experiment_launch.py b/hni_cpp/launch/experiment_nao_launch.py similarity index 93% rename from hni_cpp/launch/experiment_launch.py rename to hni_cpp/launch/experiment_nao_launch.py index cca3243..9fb6476 100644 --- a/hni_cpp/launch/experiment_launch.py +++ b/hni_cpp/launch/experiment_nao_launch.py @@ -2,6 +2,7 @@ from launch.actions import IncludeLaunchDescription from launch_ros.substitutions import FindPackageShare from launch.launch_description_sources import PythonLaunchDescriptionSource +from launch_ros.actions import Node def generate_launch_description(): return LaunchDescription([ @@ -9,7 +10,7 @@ def generate_launch_description(): package='nao_lola_client', executable='nao_lola_client', name='lola_node', - output='screen' + output='screen', ), IncludeLaunchDescription( PythonLaunchDescriptionSource([ diff --git a/hni_cpp/launch/experiment_pc_launch.py b/hni_cpp/launch/experiment_pc_launch.py new file mode 100644 index 0000000..4b6aae6 --- /dev/null +++ b/hni_cpp/launch/experiment_pc_launch.py @@ -0,0 +1,14 @@ +from launch import LaunchDescription +from launch_ros.actions import Node + +def generate_launch_description(): + return LaunchDescription([ + Node( + package='hni_py', executable='face_track_server', name='face_track_server_node', + output='screen', + ), + Node( + package='hni_cpp', executable='chat_action_server', name='chat_action_server', + prefix=['xterm -e'], + ), + ]) \ No newline at end of file diff --git a/hni_py/hni_py/chat_client.py b/hni_py/hni_py/chat_client.py old mode 100644 new mode 100755 diff --git a/hni_py/hni_py/chat_service.py b/hni_py/hni_py/chat_service.py old mode 100644 new mode 100755 index fc48b97..7459b7f --- a/hni_py/hni_py/chat_service.py +++ b/hni_py/hni_py/chat_service.py @@ -18,14 +18,10 @@ import rclpy from rclpy.node import Node -from hri_interfaces.srv import Chat +from hni_interfaces.srv import Chat -import openai -import os - -api_key = os.environ["OPENAI_API_KEY"] - -openai.api_key = api_key +# new +from openai import OpenAI class ChatService(Node): @@ -33,6 +29,7 @@ class ChatService(Node): def __init__(self): super().__init__('chat_service_server') self.srv = self.create_service(Chat, 'chatGPT_service', self.chat_callback) + self.client = OpenAI() self.chat_messages = [ {"role": "system", "content": """You are the sixth version of the Aldebaran NAO umanoid robot. You are not an AI vocal assistant only. The software that makes you work is based on a ROS2 open-source project called 'Open Access NAO' (OAN). @@ -64,16 +61,15 @@ def chat_callback(self, sRequest, sResponse): self.get_logger().info("Incoming request: " + sRequest.question) self.chat_messages.append({"role": "user", "content": sRequest.question}) reply = self.get_response(messages=self.chat_messages) - reply_text = reply['content']; + reply_text = reply.content; self.get_logger().info("Reply: " + reply_text) self.chat_messages.append(reply) sResponse.answer = reply_text return sResponse def get_response(self, messages:list): - response = openai.ChatCompletion.create( - model = "gpt-3.5-turbo", - #model = "gpt-4-turbo-preview", + response = self.client.chat.completions.create( + model = "gpt-3.5-turbo", # gpt-4-turbo-preview messages = messages, temperature = 1.0 # 0.0 - 2.0 ) @@ -83,9 +79,9 @@ def get_response(self, messages:list): def main(args=None): rclpy.init(args=args) - minimal_service = ChatService() + chat_service = ChatService() - rclpy.spin(minimal_service) + rclpy.spin(chat_service) rclpy.shutdown() diff --git a/hni_py/hni_py/face_track_client.py b/hni_py/hni_py/face_track_client.py old mode 100644 new mode 100755 diff --git a/hni_py/hni_py/face_track_server.py b/hni_py/hni_py/face_track_server.py index d819845..7c1bd7b 100644 --- a/hni_py/hni_py/face_track_server.py +++ b/hni_py/hni_py/face_track_server.py @@ -67,7 +67,7 @@ def __init__(self): self.br = CvBridge() # Load the YOLOv8 model - self.model = YOLO('/home/toto/Gdrive/uni/robocup/robocup_ws/src/hri/hri_vision/hri_vision/models/yolov8n-face.pt') + self.model = YOLO('src/hni/hni_py/hni_py/models/yolov8n-face.pt') # Store the track history self.track_history = defaultdict(lambda: []) diff --git a/hni_py/hni_py/g2stt_service.py b/hni_py/hni_py/g2stt_service.py old mode 100644 new mode 100755 diff --git a/hni_py/hni_py/gstt_service.py b/hni_py/hni_py/gstt_service.py index 0bd9aeb..c8ece9b 100755 --- a/hni_py/hni_py/gstt_service.py +++ b/hni_py/hni_py/gstt_service.py @@ -56,16 +56,16 @@ def __init__(self): #model="latest_short", model="latest_long" ) - self.timeout = speech.StreamingRecognitionConfig.VoiceActivityTimeout( - speech_start_timeout=duration1, - speech_end_timeout=duration2 - ) + #self.timeout = speech.StreamingRecognitionConfig.VoiceActivityTimeout( + # speech_start_timeout=duration1, + # speech_end_timeout=duration2 + #) self.streaming_config = speech.StreamingRecognitionConfig( config=self.config, - #single_utterance=True, + single_utterance=True, #interim_results=True, - enable_voice_activity_events=True, voice_activity_timeout=self.timeout + #enable_voice_activity_events=True, voice_activity_timeout=self.timeout, ) self.srv = self.create_service(SetBool, "gstt_service", self.gstt_callback) @@ -74,7 +74,7 @@ def __init__(self): - self.get_logger().info('GSTTService initializedaa') + self.get_logger().info('GSTTService initialized') @@ -107,7 +107,7 @@ def gstt_callback(self, sRequest, sResponse): for response in responses_iterator: #BLOCKING! self.get_logger().info('responses loop') if time.time() - start_time > timeout_seconds: - self.get_logger().error("Timeout: No final result after %d seconds." % timeout_seconds) + self.get_logger().error('Timeout: No final result after %d seconds.' % (timeout_seconds)) sResponse.success = False sResponse.message = "timeout" break # Exit the loop if we've reached the timeout without a final result @@ -126,14 +126,14 @@ def gstt_callback(self, sRequest, sResponse): final_transcript_received = True # Extract the top alternative of the final result top_transcript = result.alternatives[0].transcript - self.get_logger().info(f"Final transcript: {top_transcript}") + self.get_logger().info(f'Final transcript: {top_transcript}') sResponse.success = True sResponse.message = top_transcript break # Exit the loop since we've received a final transcript self.get_logger().info('final result checked') if not final_transcript_received: - self.get_logger().error("No final transcript received.") + self.get_logger().error('No final transcript received.') sResponse.success = False sResponse.message = "No final transcript received." @@ -141,7 +141,7 @@ def gstt_callback(self, sRequest, sResponse): return sResponse except Exception as e: - self.get_logger().error(f"Error during speech recognition: {e}") + self.get_logger().error(f'Error during speech recognition: {e}') sResponse.success = False return sResponse @@ -228,11 +228,11 @@ def __retrieve_text(self, responses): except GoogleAPICallError as e: if e.code == code_pb2.PERMISSION_DENIED: - self.get_logger().error("Permission denied error.") + self.get_logger().error('Permission denied error.') elif e.code == code_pb2.NOT_FOUND: - self.get_logger().error("Resource not found.") + self.get_logger().error('Resource not found.') else: - self.get_logger().error(f"An error occurred: {e}") + self.get_logger().error(f'An error occurred: {e}') return "" diff --git a/hni_py/hni_py/gtts_service.py b/hni_py/hni_py/gtts_service.py index 2fe9474..fc89cea 100755 --- a/hni_py/hni_py/gtts_service.py +++ b/hni_py/hni_py/gtts_service.py @@ -56,6 +56,9 @@ def __init__(self): self.sound_handle_b = SoundClient(self, blocking=False) self.srv = self.create_service(TextToSpeech, "gtts_service", self.gtts_callback) + + self.volume = 0.4 + self.get_logger().info("GTTSService Server initialized.") def gtts_callback(self, sRequest, sResponse): @@ -79,20 +82,21 @@ def gtts_callback(self, sRequest, sResponse): with open("/tmp/output.ogg", "wb") as out: # Write the response to the output file. out.write(response.audio_content) - self.get_logger().info('Audio content written to file "output.ogg"') + self.get_logger().debug('Audio content written to file "output.ogg"') - self.get_logger().info("Playing output.ogg at full volume.") + self.get_logger().info(f'Playing output.ogg at {self.volume*100}% volume.') - self.sound_handle_b.playWave("/tmp/output.ogg") - self.get_logger().info("Playwave stopped") + self.sound_handle_b.playWave("/tmp/output.ogg",self.volume) + + self.get_logger().debug('Playwave stopped') sResponse.success = True else: sResponse.success = False sResponse.debug = "empty text to convert" - self.get_logger().info("empty text to convert") + self.get_logger().warn('empty text to convert') return sResponse diff --git a/hni_py/hni_py/__pycache__/transcribe_streaming_mic.py b/hni_py/hni_py/transcribe_streaming_mic.py similarity index 100% rename from hni_py/hni_py/__pycache__/transcribe_streaming_mic.py rename to hni_py/hni_py/transcribe_streaming_mic.py diff --git a/hni_py/setup.py b/hni_py/setup.py index c2ce04a..d206180 100644 --- a/hni_py/setup.py +++ b/hni_py/setup.py @@ -31,4 +31,61 @@ 'gtts_service = hni_py.gtts_service:main', ], }, + ) + +""" + +import os +from glob import glob +from setuptools import setup + +package_name = 'my_package' + +setup( + name=package_name, + version='1.0.0', + # Packages to export + packages=[package_name], + # Files we want to install, specifically launch files + data_files=[ + # Install marker file in the package index + ('share/ament_index/resource_index/packages', ['resource/' + package_name]), + # Include our package.xml file + (os.path.join('share', package_name), ['package.xml']), + # Include all launch files. + (os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*.launch.py'))), + ], + # This is important as well + install_requires=['setuptools'], + zip_safe=True, + author='ROS 2 Developer', + author_email='ros2@ros.com', + maintainer='ROS 2 Developer', + maintainer_email='ros2@ros.com', + keywords=['foo', 'bar'], + classifiers=[ + 'Intended Audience :: Developers', + 'License :: TODO', + 'Programming Language :: Python', + 'Topic :: Software Development', + ], + description='My awesome package.', + license='TODO', + # Like the CMakeLists add_executable macro, you can add your python + # scripts here. + entry_points={ + 'console_scripts': [ + 'chat_client = hni_py.chat_client:main', + 'chat_service = hni_py.chat_service:main', + 'face_track_client = hni_py.face_track_client:main', + 'face_track_server = hni_py.face_track_server:main', + 'g2stt_service = hni_py.g2stt_service:main', + 'gstt_client = hni_py.gstt_client:main', + 'gstt_service = hni_py.gstt_service:main', + 'gtts_client = hni_py.gtts_client:main', + 'gtts_service = hni_py.gtts_service:main', + ], + }, +) +""" \ No newline at end of file