-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjarvis.py
174 lines (148 loc) · 4.44 KB
/
jarvis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import threading
from backend.modules.automodel import Operate
from backend.modules.basic.listenpy import Listen
import os
import mtranslate as mt
from threading import Lock
import os
import eel
import pyautogui
import base64
from backend.modules.extra import GuiMessagesConverter, LoadMessages
from dotenv import load_dotenv
def get_api():
try:
with open('config/config.json') as config_file:
config = json.load(config_file)
API = config.get('GROQ_API')
if API is None:
raise ValueError("GROQ_API URL not found in config file")
return API
except FileNotFoundError:
print("Config file not found.")
except json.JSONDecodeError:
print("Error decoding JSON in config file.")
except Exception as e:
print(f"Error reading config file: {e}")
return None
os.environ['GROQ_API'] = get_api()
def run_docker():
import os
os.chdir("backend/AI/Perplexica")
os.system("docker compose up -d")
thread = threading.Thread(target=run_docker)
thread.start()
load_dotenv()
state = 'Available...'
messages = LoadMessages()
WEBCAM = False
js_messageslist = []
working: list[threading.Thread] = []
InputLanguage = os.environ['InputLanguage']
Username = os.environ['NickName']
lock = Lock()
def UniversalTranslator(Text: str) -> str:
"""Translates text to English."""
english_translation = mt.translate(Text, 'en', 'auto')
return english_translation.capitalize()
def MainExecution(Query: str):
"""Main execution function for handling user queries."""
global WEBCAM, state
Query = UniversalTranslator(Query) if 'en' not in InputLanguage.lower() else Query.capitalize()
if state != 'Available...':
return
state = 'Thinking...'
Decision = Operate(Query)
if 'realtime-webcam' in Decision:
python_call_to_start_video()
print('Video Started')
WEBCAM = True
elif 'close_webcam' in Decision:
print('Video Stopped')
python_call_to_stop_video()
WEBCAM = False
return Decision
@eel.expose
def js_messages():
"""Fetches new messages to update the GUI."""
global messages, js_messageslist
with lock:
messages = LoadMessages()
if js_messageslist != messages:
new_messages = GuiMessagesConverter(messages[len(js_messageslist):])
js_messageslist = messages
return new_messages
return []
@eel.expose
def js_state(stat=None):
"""Updates or retrieves the current state."""
global state
if stat:
state = stat
return state
@eel.expose
def js_mic(transcription):
"""Handles microphone input."""
print(transcription)
if not working:
work = threading.Thread(target=process_input, args=(transcription,), daemon=True)
work.start()
working.append(work)
else:
if working[0].is_alive():
return
working.pop()
work = threading.Thread(target=process_input, args=(transcription,), daemon=True)
work.start()
working.append(work)
def process_input(transcription):
global WEBCAM
result = MainExecution(transcription)
if result == "close_webcam":
print('Video Stopped')
python_call_to_stop_video()
WEBCAM = False
@eel.expose
def python_call_to_start_video():
"""Starts the video capture."""
eel.startVideo()
@eel.expose
def python_call_to_stop_video():
"""Stops the video capture."""
eel.stopVideo()
@eel.expose
def python_call_to_capture():
"""Captures an image from the video."""
eel.capture()
@eel.expose
def handle_captured_image(image_data):
"""Handles the captured image data from the web interface."""
js_capture(image_data)
@eel.expose
def js_page(cpage=None):
"""Navigates to the specified page."""
if cpage == 'home':
eel.openHome()
elif cpage == 'settings':
eel.openSettings()
@eel.expose
def setup():
"""Sets up the GUI window."""
pyautogui.hotkey('win', 'up')
@eel.expose
def js_language():
"""Returns the input language."""
return str(InputLanguage)
@eel.expose
def js_assistantname():
"""Returns the assistant's name."""
return "JARVIS"
@eel.expose
def js_capture(image_data):
"""Saves the captured image."""
image_bytes = base64.b64decode(image_data.split(',')[1])
with open('capture.png', 'wb') as f:
f.write(image_bytes)
# Initialize Eel and start the application
eel.init('web')
eel.start('spider.html', port=44444)