Skip to content

Commit 17bdb6e

Browse files
authored
29 update constants format (#30)
* Refactor constants into classes for better organization and clarity * Fix method signature in JsonWrapper and update error handling in HTTP server * Refactor constants into classes for better organization and clarity * Fix method signature in JsonWrapper and update error handling in HTTP server * Enhance documentation and improve code readability across multiple modules * Remove redundant methods and comments from VRChatOSC class for improved clarity * Refactor WhisperTranscriber initialization and silence duration; update constant references in main and add static methods to configuration classes * Refactor constant references to improve code organization and readability across multiple modules
1 parent ce3bde3 commit 17bdb6e

9 files changed

+102
-65
lines changed

classes/osc.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ def __init__(self, ip: str, port: int):
2626

2727
self.client = udp_client.SimpleUDPClient(ip, port)
2828

29-
def send_message(self, message: str):
30-
"""
31-
Sends a message to the chatbox.
32-
Args:
33-
message (str): The message to be sent to the chatbox.
34-
"""
35-
36-
self.client.send_message("/chatbox/input", [message, True])
37-
self.client.send_message("/chatbox/typing", False)
38-
3929
def set_typing_indicator(self, typing: bool):
4030
"""
4131
Sets the typing indicator status in the chatbox.
@@ -45,3 +35,13 @@ def set_typing_indicator(self, typing: bool):
4535
"""
4636

4737
self.client.send_message("/chatbox/typing", typing)
38+
39+
def send_message(self, message: str):
40+
"""
41+
Sends a message to the chatbox.
42+
Args:
43+
message (str): The message to be sent to the chatbox.
44+
"""
45+
46+
self.client.send_message("/chatbox/input", [message, True])
47+
self.client.send_message("/chatbox/typing", False)

classes/whisper.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class WhisperTranscriber:
1919

2020
def __init__(self):
2121
"""Initialize the WhisperTranscriber with the base Whisper model."""
22-
self.model = whisper.load_model("base")
22+
self.model = whisper.load("base")
2323

2424
def transcribe_file(self, audio_file_path):
2525
"""
@@ -53,7 +53,7 @@ def get_speech_input(self):
5353
rate = 16000
5454
chunk = 1024
5555
silence_threshold = -40 # Silence threshold in dB
56-
silence_duration = 1000 # Duration of silence in ms (1 second)
56+
silence_duration = 1 # Duration of silence in seconds
5757

5858
# Open the audio stream
5959
stream = p.open(format=audio_format,
@@ -96,7 +96,7 @@ def get_speech_input(self):
9696
# Save the recorded data to a WAV file
9797
with wave.open('temp.wav', 'wb') as wf:
9898
wf.setnchannels(channels)
99-
wf.setsampwidth(p.get_sample_size(format))
99+
wf.setsampwidth(p.get_sample_size(audio_format))
100100
wf.setframerate(rate)
101101
wf.writeframes(b''.join(frames))
102102

@@ -115,5 +115,4 @@ def get_speech_input(self):
115115

116116
if text not in unwanted_responses:
117117
return text
118-
else:
119-
return ""
118+
return ""

constants.py

+68-37
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,68 @@
1-
"""
2-
Constants module for Nova AI project.
3-
This module contains all the configuration constants used throughout the
4-
project.
5-
Constants:
6-
Network Settings:
7-
LOCAL_IP (str): Local IP address of the computer
8-
VRC_PORT (int): VRChat port number, default is 9000
9-
Audio Settings:
10-
AUDIO_OUTPUT_INDEX (int): Index number for audio output device
11-
(VB-Audio Cable B)
12-
Language Model Settings:
13-
MODEL_ID (str): Identifier for the language model being used
14-
LM_TEMPERATURE (float): Temperature setting for language model output
15-
generation
16-
File Paths:
17-
HISTORY_PATH (str): Path to the history JSON file
18-
Note:
19-
Make sure to adjust LOCAL_IP and AUDIO_OUTPUT_INDEX according to your
20-
system configuration.
21-
"""
22-
23-
# Network Settings
24-
25-
LOCAL_IP = "192.168.0.195" # Your computers local IP
26-
VRC_PORT = 9000 # VR Chat VRC_PORT, 9000 is the default
27-
28-
# Audio Settings
29-
# The index of the audio output device (VB-Audio Cable B)
30-
AUDIO_OUTPUT_INDEX = 10
31-
32-
# LM Settings
33-
# The model ID of the LM
34-
MODEL_ID = "lmstudio-community/Meta-Llama-3.1-8B-Instruct-GGUF"
35-
LM_TEMPERATURE = 0.5 # The temperature of the LM
36-
37-
HISTORY_PATH = "json_files/history.json"
1+
@staticmethod
2+
class Network:
3+
"""
4+
Class representing network configuration parameters.
5+
Attributes:
6+
LOCAL_IP (str): The local IP address of the computer running the
7+
application.
8+
VRC_PORT (int): The port number used for VRChat communication. Default
9+
is 9000.
10+
"""
11+
LOCAL_IP = "192.168.0.195"
12+
VRC_PORT = 9000
13+
14+
15+
@staticmethod
16+
class Audio:
17+
"""
18+
A class containing audio device configuration constants.
19+
This class defines constants for audio input and output device indices,
20+
specifically for VB-Audio Cable virtual audio devices.
21+
Attributes:
22+
AUDIO_OUTPUT_INDEX (int): The device index for audio output,
23+
configured for VB-Audio Cable B Input (default: 10)
24+
AUDIO_INPUT_INDEX (int): The device index for audio input,
25+
configured for VB-Audio Cable A Output (default: 16)
26+
"""
27+
AUDIO_OUTPUT_INDEX = 10
28+
AUDIO_INPUT_INDEX = 16
29+
30+
31+
@staticmethod
32+
class Voice:
33+
"""
34+
A class that defines constants for voice-related configurations in
35+
Text-to-Speech (TTS) operations.
36+
Attributes:
37+
VOICE_NAME (str): The default voice name used for text-to-speech
38+
synthesis. Currently set to "Zira".
39+
"""
40+
VOICE_NAME = "Zira"
41+
42+
43+
@staticmethod
44+
class LanguageModel:
45+
"""
46+
A class representing configuration settings for a language model.
47+
This class contains constant values that define the behavior and identity
48+
of the language model being used.
49+
Attributes:
50+
MODEL_ID (str): The identifier for the specific language model being
51+
used. Currently set to Meta-Llama 3.1 8B Instruct GGUF model.
52+
LM_TEMPERATURE (float): The temperature parameter for the language
53+
model's output. Controls randomness in text generation (0.0 to 1.0,
54+
where higher means more random).
55+
"""
56+
MODEL_ID = "lmstudio-community/Meta-Llama-3.1-8B-Instruct-GGUF"
57+
LM_TEMPERATURE = 0.5
58+
59+
60+
@staticmethod
61+
class FilePaths:
62+
"""
63+
A class containing file path constants used in the application.
64+
Attributes:
65+
HISTORY_PATH (str): The path to the JSON file storing history data.
66+
Default is "json_files/history.json"
67+
"""
68+
HISTORY_PATH = "json_files/history.json"

debug_function_library.py

Whitespace-only changes.

http_server.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ def add_message(message):
7171
to the history.
7272
"""
7373

74-
json_wrapper.JsonWrapper.write(constant.HISTORY_PATH, message)
74+
json_wrapper.JsonWrapper.write(
75+
constant.FilePaths.HISTORY_PATH,
76+
message
77+
)
7578

7679
return f"Added '{message}' to history."
7780

@@ -83,7 +86,8 @@ def logs():
8386
str: The logs read from the history path.
8487
"""
8588

86-
history = json_wrapper.JsonWrapper.read_json(constant.HISTORY_PATH)
89+
history = json_wrapper.JsonWrapper.read_json(
90+
constant.FilePaths.HISTORY_PATH)
8791
logs = history.toString()
8892

8993
return logs
@@ -107,7 +111,7 @@ def reset_logs():
107111
cleared.
108112
"""
109113

110-
json_wrapper.JsonWrapper.write(constant.HISTORY_PATH, [])
114+
json_wrapper.JsonWrapper.write(constant.FilePaths.HISTORY_PATH, [])
111115

112116
return "Logs Cleared"
113117

main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def main():
3333
Exception: If an error occurs during the execution of nova.run_code().
3434
"""
3535

36-
osc = VRChatOSC(constant.LOCAL_IP, constant.VRC_PORT)
36+
osc = VRChatOSC(constant.Network.LOCAL_IP, constant.Network.VRC_PORT)
3737
while True:
3838
try:
3939
print("Program Starting...")

nova.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def initialize_components():
4242
- openai_client (OpenAI): An instance of the OpenAI client.
4343
"""
4444

45-
osc = VRChatOSC(constant.LOCAL_IP, constant.VRC_PORT)
45+
osc = VRChatOSC(constant.Network.LOCAL_IP, constant.Network.VRC_PORT)
4646
transcriber = WhisperTranscriber()
4747
system_prompt = SystemPrompt.get_full_prompt("normal")
4848
now = datetime.datetime.now()
@@ -135,15 +135,15 @@ def process_completion(completion, osc):
135135
osc.send_message(sentence)
136136
play_tts(
137137
sentence,
138-
output_device_index=constant.AUDIO_OUTPUT_INDEX
138+
output_device_index=constant.Audio.AUDIO_OUTPUT_INDEX
139139
)
140140
buffer = sentence_chunks[0]
141141
if buffer:
142142
osc.set_typing_indicator(True)
143143
full_response += f" {buffer}"
144144
print(f"AI: {buffer}")
145145
osc.send_message(buffer)
146-
play_tts(buffer, output_device_index=constant.AUDIO_OUTPUT_INDEX)
146+
play_tts(buffer, output_device_index=constant.Audio.AUDIO_OUTPUT_INDEX)
147147
osc.set_typing_indicator(False)
148148
return full_response
149149

@@ -176,9 +176,9 @@ def run_code():
176176
while True:
177177
# Creates model parameters
178178
completion = openai_client.chat.completions.create(
179-
model=constant.MODEL_ID,
179+
model=constant.LanguageModel.MODEL_ID,
180180
messages=history,
181-
temperature=constant.LM_TEMPERATURE,
181+
temperature=constant.LanguageModel.LM_TEMPERATURE,
182182
stream=True,
183183
)
184184

@@ -189,12 +189,12 @@ def run_code():
189189
full_response = process_completion(completion, osc)
190190
new_message["content"] = full_response
191191

192-
JsonWrapper.write(constant.HISTORY_PATH, new_message)
192+
JsonWrapper.write(constant.FilePaths.HISTORY_PATH, new_message)
193193

194194
# Get user speech input
195195
user_speech = ""
196196
while not user_speech:
197197
user_speech = transcriber.get_speech_input()
198198

199199
print(f"HUMAN: {user_speech}")
200-
JsonWrapper.write(constant.HISTORY_PATH, user_speech)
200+
JsonWrapper.write(constant.FilePaths.HISTORY_PATH, user_speech)

nova_placement.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
time.sleep(15)
3838

3939
# Set up OSC
40-
osc_client = udp_client.SimpleUDPClient(constant.LOCAL_IP, constant.VRC_PORT)
40+
osc_client = udp_client.SimpleUDPClient(
41+
constant.Network.LOCAL_IP,
42+
constant.Network.VRC_PORT
43+
)
4144
osc_client.send_message("/chatbox/input", ["Positioning...", True])
4245

4346
##########################################################################

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ translate
1010
googletrans
1111
requests
1212
sounddevice
13-
soundfile
13+
soundfile

0 commit comments

Comments
 (0)