|
1 | | -import sys |
2 | | -import os |
3 | 1 | 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 |
9 | 2 |
|
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 | | -) |
15 | 3 | from operate.config import Config |
| 4 | +from operate.utils.operating_system import OperatingSystem |
16 | 5 | from operate.utils.style import ( |
| 6 | + ANSI_BLUE, |
| 7 | + ANSI_BRIGHT_MAGENTA, |
17 | 8 | ANSI_GREEN, |
18 | | - ANSI_RESET, |
19 | | - ANSI_YELLOW, |
20 | 9 | ANSI_RED, |
21 | | - ANSI_BRIGHT_MAGENTA, |
22 | | - ANSI_BLUE, |
23 | | - style, |
| 10 | + ANSI_RESET, |
24 | 11 | ) |
25 | | -from operate.utils.operating_system import OperatingSystem |
26 | | -from operate.models.apis import get_next_action |
27 | 12 |
|
28 | 13 | # Load configuration |
29 | 14 | config = Config() |
|
32 | 17 | VERBOSE = config.verbose |
33 | 18 |
|
34 | 19 |
|
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 | | - |
133 | 20 | def operate(operations): |
134 | 21 | if VERBOSE: |
135 | 22 | print("[Self Operating Computer][operate]") |
|
0 commit comments