Skip to content

Commit

Permalink
fix py
Browse files Browse the repository at this point in the history
  • Loading branch information
antbono committed Apr 17, 2024
1 parent 9f9879e commit 8bbdaf4
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 37 deletions.
6 changes: 3 additions & 3 deletions hni_cpp/launch/chat_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
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([
Node(
package='nao_lola_client',
executable='nao_lola_client',
name='lola_node',
output='screen'
output='screen',
),
IncludeLaunchDescription(
PythonLaunchDescriptionSource([
Expand Down
14 changes: 14 additions & 0 deletions hni_cpp/launch/experiment_pc_launch.py
Original file line number Diff line number Diff line change
@@ -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'],
),
])
Empty file modified hni_py/hni_py/chat_client.py
100644 → 100755
Empty file.
22 changes: 9 additions & 13 deletions hni_py/hni_py/chat_service.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@
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):

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).
Expand Down Expand Up @@ -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
)
Expand All @@ -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()

Expand Down
Empty file modified hni_py/hni_py/face_track_client.py
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion hni_py/hni_py/face_track_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: [])
Expand Down
Empty file modified hni_py/hni_py/g2stt_service.py
100644 → 100755
Empty file.
28 changes: 14 additions & 14 deletions hni_py/hni_py/gstt_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -74,7 +74,7 @@ def __init__(self):



self.get_logger().info('GSTTService initializedaa')
self.get_logger().info('GSTTService initialized')



Expand Down Expand Up @@ -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
Expand All @@ -126,22 +126,22 @@ 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."

self.get_logger().debug('GSTTService complete request')
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

Expand Down Expand Up @@ -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 ""


Expand Down
14 changes: 9 additions & 5 deletions hni_py/hni_py/gtts_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

Expand Down
File renamed without changes.
57 changes: 57 additions & 0 deletions hni_py/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
},
)
"""

0 comments on commit 8bbdaf4

Please sign in to comment.