Skip to content

Commit 2025fa0

Browse files
committed
refactor: moved main to main.py and added some type annotations
1 parent 10bb8bf commit 2025fa0

File tree

3 files changed

+128
-121
lines changed

3 files changed

+128
-121
lines changed

operate/config.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import os
22
import sys
3+
4+
import google.generativeai as genai
35
from dotenv import load_dotenv
46
from openai import OpenAI
57
from prompt_toolkit.shortcuts import input_dialog
6-
import google.generativeai as genai
78

89

910
class Config:
@@ -72,7 +73,7 @@ def initialize_google(self):
7273

7374
return model
7475

75-
def validation(self, model, voice_mode):
76+
def validation(self, model: str, voice_mode):
7677
"""
7778
Validate the input parameters for the dialog operation.
7879
"""
@@ -119,3 +120,4 @@ def prompt_and_save_api_key(self, key_name, key_description):
119120
def save_api_key_to_env(key_name, key_value):
120121
with open(".env", "a") as file:
121122
file.write(f"\n{key_name}='{key_value}'")
123+
file.write(f"\n{key_name}='{key_value}'")

operate/main.py

+120-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,126 @@
22
Self-Operating Computer
33
"""
44
import argparse
5-
from operate.utils.style import ANSI_BRIGHT_MAGENTA
6-
from operate.operate import main
5+
import asyncio
6+
import os
7+
import platform
8+
import sys
9+
10+
from prompt_toolkit import prompt
11+
from prompt_toolkit.shortcuts import message_dialog
12+
13+
from operate.config import Config
14+
from operate.exceptions import ModelNotRecognizedException
15+
from operate.models.apis import get_next_action
16+
from operate.models.prompts import USER_QUESTION, get_system_prompt
17+
from operate.utils.style import (
18+
ANSI_BRIGHT_MAGENTA,
19+
ANSI_GREEN,
20+
ANSI_RED,
21+
ANSI_RESET,
22+
ANSI_YELLOW,
23+
style,
24+
)
25+
26+
config = Config()
27+
28+
29+
def main(model: str, terminal_prompt: str, voice_mode: bool = False):
30+
"""
31+
Main function for the Self-Operating Computer.
32+
33+
Parameters:
34+
- model: The model used for generating responses.
35+
- terminal_prompt: A string representing the prompt provided in the terminal.
36+
- voice_mode: A boolean indicating whether to enable voice mode.
37+
38+
Returns:
39+
None
40+
"""
41+
42+
mic = None
43+
# Initialize `WhisperMic`, if `voice_mode` is True
44+
45+
config.validation(model, voice_mode)
46+
47+
if voice_mode:
48+
try:
49+
from whisper_mic import WhisperMic
50+
51+
# Initialize WhisperMic if import is successful
52+
mic = WhisperMic()
53+
except ImportError:
54+
print(
55+
"Voice mode requires the 'whisper_mic' module. Please install it using 'pip install -r requirements-audio.txt'"
56+
)
57+
sys.exit(1)
58+
59+
# Skip message dialog if prompt was given directly
60+
if not terminal_prompt:
61+
message_dialog(
62+
title="Self-Operating Computer",
63+
text="An experimental framework to enable multimodal models to operate computers",
64+
style=style,
65+
).run()
66+
67+
else:
68+
print("Running direct prompt...")
69+
70+
# # Clear the console
71+
if platform.system() == "Windows":
72+
os.system("cls")
73+
else:
74+
print("\033c", end="")
75+
76+
if terminal_prompt: # Skip objective prompt if it was given as an argument
77+
objective = terminal_prompt
78+
elif voice_mode:
79+
print(
80+
f"{ANSI_GREEN}[Self-Operating Computer]{ANSI_RESET} Listening for your command... (speak now)"
81+
)
82+
try:
83+
objective = mic.listen()
84+
except Exception as e:
85+
print(f"{ANSI_RED}Error in capturing voice input: {e}{ANSI_RESET}")
86+
return # Exit if voice input fails
87+
else:
88+
print(f"{ANSI_GREEN}[Self-Operating Computer]\n{ANSI_RESET}{USER_QUESTION}")
89+
print(f"{ANSI_YELLOW}[User]{ANSI_RESET}")
90+
objective = prompt(style=style)
91+
92+
system_prompt = get_system_prompt(model, objective)
93+
system_message = {"role": "system", "content": system_prompt}
94+
messages = [system_message]
95+
96+
loop_count = 0
97+
98+
session_id = None
99+
100+
while True:
101+
if VERBOSE:
102+
print("[Self Operating Computer] loop_count", loop_count)
103+
try:
104+
operations, session_id = asyncio.run(
105+
get_next_action(model, messages, objective, session_id)
106+
)
107+
108+
stop = operate(operations)
109+
if stop:
110+
break
111+
112+
loop_count += 1
113+
if loop_count > 10:
114+
break
115+
except ModelNotRecognizedException as e:
116+
print(
117+
f"{ANSI_GREEN}[Self-Operating Computer]{ANSI_RED}[Error] -> {e} {ANSI_RESET}"
118+
)
119+
break
120+
except Exception as e:
121+
print(
122+
f"{ANSI_GREEN}[Self-Operating Computer]{ANSI_RED}[Error] -> {e} {ANSI_RESET}"
123+
)
124+
break
7125

8126

9127
def main_entry():

operate/operate.py

+4-117
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
1-
import sys
2-
import os
31
import time
4-
import asyncio
5-
from prompt_toolkit.shortcuts import message_dialog
6-
from prompt_toolkit import prompt
7-
from operate.exceptions import ModelNotRecognizedException
8-
import platform
92

10-
# from operate.models.prompts import USER_QUESTION, get_system_prompt
11-
from operate.models.prompts import (
12-
USER_QUESTION,
13-
get_system_prompt,
14-
)
153
from operate.config import Config
4+
from operate.utils.operating_system import OperatingSystem
165
from operate.utils.style import (
6+
ANSI_BLUE,
7+
ANSI_BRIGHT_MAGENTA,
178
ANSI_GREEN,
18-
ANSI_RESET,
19-
ANSI_YELLOW,
209
ANSI_RED,
21-
ANSI_BRIGHT_MAGENTA,
22-
ANSI_BLUE,
23-
style,
10+
ANSI_RESET,
2411
)
25-
from operate.utils.operating_system import OperatingSystem
26-
from operate.models.apis import get_next_action
2712

2813
# Load configuration
2914
config = Config()
@@ -32,104 +17,6 @@
3217
VERBOSE = config.verbose
3318

3419

35-
def main(model, terminal_prompt, voice_mode=False):
36-
"""
37-
Main function for the Self-Operating Computer.
38-
39-
Parameters:
40-
- model: The model used for generating responses.
41-
- terminal_prompt: A string representing the prompt provided in the terminal.
42-
- voice_mode: A boolean indicating whether to enable voice mode.
43-
44-
Returns:
45-
None
46-
"""
47-
48-
mic = None
49-
# Initialize `WhisperMic`, if `voice_mode` is True
50-
51-
config.validation(model, voice_mode)
52-
53-
if voice_mode:
54-
try:
55-
from whisper_mic import WhisperMic
56-
57-
# Initialize WhisperMic if import is successful
58-
mic = WhisperMic()
59-
except ImportError:
60-
print(
61-
"Voice mode requires the 'whisper_mic' module. Please install it using 'pip install -r requirements-audio.txt'"
62-
)
63-
sys.exit(1)
64-
65-
# Skip message dialog if prompt was given directly
66-
if not terminal_prompt:
67-
message_dialog(
68-
title="Self-Operating Computer",
69-
text="An experimental framework to enable multimodal models to operate computers",
70-
style=style,
71-
).run()
72-
73-
else:
74-
print("Running direct prompt...")
75-
76-
# # Clear the console
77-
if platform.system() == "Windows":
78-
os.system("cls")
79-
else:
80-
print("\033c", end="")
81-
82-
if terminal_prompt: # Skip objective prompt if it was given as an argument
83-
objective = terminal_prompt
84-
elif voice_mode:
85-
print(
86-
f"{ANSI_GREEN}[Self-Operating Computer]{ANSI_RESET} Listening for your command... (speak now)"
87-
)
88-
try:
89-
objective = mic.listen()
90-
except Exception as e:
91-
print(f"{ANSI_RED}Error in capturing voice input: {e}{ANSI_RESET}")
92-
return # Exit if voice input fails
93-
else:
94-
print(f"{ANSI_GREEN}[Self-Operating Computer]\n{ANSI_RESET}{USER_QUESTION}")
95-
print(f"{ANSI_YELLOW}[User]{ANSI_RESET}")
96-
objective = prompt(style=style)
97-
98-
system_prompt = get_system_prompt(model, objective)
99-
system_message = {"role": "system", "content": system_prompt}
100-
messages = [system_message]
101-
102-
loop_count = 0
103-
104-
session_id = None
105-
106-
while True:
107-
if VERBOSE:
108-
print("[Self Operating Computer] loop_count", loop_count)
109-
try:
110-
operations, session_id = asyncio.run(
111-
get_next_action(model, messages, objective, session_id)
112-
)
113-
114-
stop = operate(operations)
115-
if stop:
116-
break
117-
118-
loop_count += 1
119-
if loop_count > 10:
120-
break
121-
except ModelNotRecognizedException as e:
122-
print(
123-
f"{ANSI_GREEN}[Self-Operating Computer]{ANSI_RED}[Error] -> {e} {ANSI_RESET}"
124-
)
125-
break
126-
except Exception as e:
127-
print(
128-
f"{ANSI_GREEN}[Self-Operating Computer]{ANSI_RED}[Error] -> {e} {ANSI_RESET}"
129-
)
130-
break
131-
132-
13320
def operate(operations):
13421
if VERBOSE:
13522
print("[Self Operating Computer][operate]")

0 commit comments

Comments
 (0)