This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·181 lines (150 loc) · 6.41 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
#!/usr/bin/python3.1
import subprocess
import sys
import os
import re
from functions.api import api
from functions.clean import clean
from functions.about import about
blue = '\033[96m'
white = '\033[37m'
red = '\033[31m'
reset = '\033[0m'
green = '\033[32m'
blink = '\033[5m'
yellow = '\033[33m'
cyan = '\033[1;36m'
rgb_75 = '\x1b[38;5;75m'
models = {
"1": "GPT-4",
"2": "GPT-3.5"
}
models_names = {
"GPT-4": "gpt-4",
"GPT-3.5" : "gpt-3.5-turbo"
}
def options_list():
print(" {}┌──────────────────────────────────────┐{}".format(rgb_75, reset))
print(" {}│{} {}│{}".format(rgb_75, reset, rgb_75, reset))
print(" {}│{} {}╔══════════════════════╗{} {}│{}".format(rgb_75, reset, white, reset, rgb_75, reset))
print(" {}│{} {}║ Welcome to MeeGPT! ║{} {}│{}".format(rgb_75, reset, white, reset, rgb_75, reset))
print(" {}│{} {}╚══════════════════════╝{} {}│{}".format(rgb_75, reset, white, reset, rgb_75, reset))
string = "AI Model: {}".format(model)
if len(string) % 2 != 0:
string += " "
lenght = " " * int((38 - len(string)) / 2)
print(" {}│{}{}{}{}{}{}{}│{}".format(rgb_75, reset, lenght, white, string, reset, lenght, rgb_75, reset))
print(" {}│{} {}│{}".format(rgb_75, reset, rgb_75, reset))
print(" {}│{} {}{}Type your message / select option{} {}│{}".format(rgb_75, reset, white, blink, reset, rgb_75, reset))
print(" {}│{} {}│{}".format(rgb_75, reset, rgb_75, reset))
print(" {}│{} {}1. Change model{} {}│{}".format(rgb_75, reset, white, reset, rgb_75, reset))
print(" {}│{} {}2. About{} {}│{}".format(rgb_75, reset, white, reset, rgb_75, reset))
print(" {}│{} {}│{}".format(rgb_75, reset, rgb_75, reset))
print(" {}│{} {}0. Exit{} {}│{}".format(rgb_75, reset, white, reset, rgb_75, reset))
print(" {}│{} {}│{}".format(rgb_75, reset, rgb_75, reset))
print(" {}└──────────────────────────────────────┘{} \n ".format(rgb_75, reset))
def model_changer():
while True:
print(" {}Supported models:{}".format(rgb_75, reset))
for i, m in models.items():
print(" {}. {}".format(i, m))
print(" ")
global model
answer = input(" {}Enter desired model{}: ".format(yellow, white))
if answer == "0":
break
if answer not in models.keys():
model = "gpt-3.5-turbo"
print(" {}Wrong model!{}".format(red, reset))
print(" ")
continue
model = models_names[models[answer]]
with open(".config", "w") as f:
f.write("model = {}".format(model))
break
def instance_checker():
output = api(query="Yes")
if output == "Error":
return False
try:
if "error" in output.keys() or "invalid_api_key" in output.values():
working = False
else:
working = True
except:
working = False
return working
def main():
clean()
#folder = "."
folder = "/opt/MeeGPT"
if not os.path.isdir(folder):
if os.path.isfile(folder):
os.remove(folder)
os.mkdir(folder)
os.chdir(folder)
_ = subprocess.call("ping wunderwungiel.pl -c 2 > /dev/null 2>&1", shell=True)
if _ != 0:
print(" {}Failed to connect, please\n check your internet connection.{}".format(red, reset))
print()
input(" {}{}Press Enter to continue... {}".format(blink, cyan, reset))
clean()
sys.exit(1)
global model
if not os.path.isfile(".config"):
with open(".config", "w") as f:
f.write("model = gpt-3.5-turbo")
with open(".config", "r") as f:
content = f.read()
model = re.search("model = (.+)", content)
if model:
model = model.group(1)
else:
with open(".config", "w") as f:
f.write("model = gpt-3.5-turbo")
model = "gpt-3.5-turbo"
instance = instance_checker()
if instance == False:
print(" {}I couldn't find working API endpoint...{}".format(red, reset))
input(" \n {}{}Press Enter to exit... {}".format(blink, cyan, reset))
sys.exit(0)
options_list()
while True:
try:
answer = input(" {}Your message:{} ".format(yellow, white))
except KeyboardInterrupt:
print(" {}KeyboardInterrupt, exiting...{}".format(red, reset))
sys.exit(0)
supported = ["1", "2", "0"]
print(" ")
if answer.isnumeric():
if answer not in supported:
print(" {}Wrong number, select a correct one!{}".format(red, reset))
print(" ")
continue
if answer == "1":
model_changer()
clean()
options_list()
continue
elif answer == "2":
about()
clean()
options_list()
continue
elif answer == "0":
sys.exit(0)
else:
result = api(query=answer, model="gpt-3.5-turbo")
print(" {}══════════════════════════════════════{}\n ".format(white, reset))
ai_answer = result['choices'][0]['message']
if isinstance(ai_answer, dict):
ai_answer = ai_answer['content']
ai_answer = ai_answer.splitlines() # Podzielenie ciągu na linie
ai_answer = [' ' + line for line in ai_answer]
ai_answer = '\n'.join(ai_answer)
ai_answer = " {}ChatGPT:{}".format(yellow, reset) + white + ai_answer + reset
print(ai_answer)
print("\n {}══════════════════════════════════════{}\n ".format(white, reset))
if __name__ == "__main__":
main()