forked from Likhithsai2580/JARVIS-RE-J4E
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
199 lines (177 loc) · 5.65 KB
/
main.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Internal filename: main.py
# Bytecode version: 3.10.0rc2 (3439)
# Source timestamp: 1970-01-01 00:00:00 UTC (0)
import sys
import os
import json
import threading
import asyncio
import base64
from time import sleep
from random import choice
import pyautogui
import mtranslate as mt
import eel
from dotenv import load_dotenv, set_key
from threading import Lock
from playsound import playsound
from Backend.Extra import AnswerModifier, QueryModifier, LoadMessages, GuiMessagesConverter
from Backend.Automation import Automation, professional_responses
from Backend.RSE_R import RealTimeChatBotAI
from Backend.Chatbot import ChatBotAI
from Backend.AutoModel import Model
from Backend.ChatGpt import ChatGptAI as ChatGptAI
from Backend.TTS2 import TTS
from Backend.RSE import Alert
Alert("JARVIS IS ONLINE NOW")
#Lock set by kaushik
# import Backend.Security
#remove this to unlock
# Load environment variables
load_dotenv()
state = 'Available...'
messages = LoadMessages()
WEBCAM = False
js_messageslist = []
working: list[threading.Thread] = []
InputLanguage = os.environ['InputLanguage']
Assistantname = os.environ['AssistantName']
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()
Query = QueryModifier(Query)
if state != 'Available...':
return
state = 'Thinking...'
Decision = Model(Query)
if 'general' in Decision or 'realtime' in Decision:
if Decision[0] == 'general':
if WEBCAM:
python_call_to_capture()
sleep(0.5)
Answer = AnswerModifier(ChatGptAI(Query))
else:
Answer = AnswerModifier(ChatBotAI(Query))
state = 'Answering...'
TTS(Answer)
else:
state = 'Searching...'
Answer = AnswerModifier(RealTimeChatBotAI(Query))
response = Answer
state = 'Answering...'
TTS(response)
elif 'open webcam' in Decision:
# playsound(r'Start.mp3')
python_call_to_start_video()
playsound(r'Start.mp3')
print('Video Started')
WEBCAM = True
elif 'close webcam' in Decision:
# playsound(r'Stop.mp3')
print('Video Stopped')
python_call_to_stop_video()
playsound(r'Stop.mp3')
WEBCAM = False
else:
state = 'Automation...'
asyncio.run(Automation(Decision, print))
response = choice(professional_responses)
state = 'Answering...'
with open('ChatLog.json', 'w') as f:
json.dump(messages + [{'role': 'assistant', 'content': response}], f, indent=4)
TTS(response)
state = 'Listening...'
@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=MainExecution, args=(transcription,), daemon=True)
work.start()
working.append(work)
else:
if working[0].is_alive():
return
working.pop()
work = threading.Thread(target=MainExecution, args=(transcription,), daemon=True)
work.start()
working.append(work)
@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 js_page(cpage=None):
"""Navigates to the specified page."""
if cpage == 'home':
eel.openHome()
elif cpage == 'settings':
eel.openSettings()
@eel.expose
def js_setvalues(GeminiApi, HuggingFaceApi, GroqApi, AssistantName, Username):
"""Sets API keys and user preferences."""
print(f'GeminiApi = {GeminiApi!r} HuggingFaceApi = {HuggingFaceApi!r} GroqApi = {GroqApi!r} AssistantName = {AssistantName!r} Username = {Username!r}')
if GeminiApi:
set_key('.env', 'CohereAPI', GeminiApi)
if HuggingFaceApi:
set_key('.env', 'HuggingFaceAPI', HuggingFaceApi)
if GroqApi:
set_key('.env', 'GroqAPI', GroqApi)
if AssistantName:
set_key('.env', 'AssistantName', AssistantName)
if Username:
set_key('.env', 'NickName', Username)
@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 Assistantname
@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(r'web')
eel.start('spider.html', port=44444)