-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
288 lines (280 loc) · 13.7 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from termcolor import colored
from sys import platform
import configparser
import requests
import openai
import os
def clear():
if platform == "linux" or platform == "linux2" or platform == "unix":
os.system("clear")
elif platform == "win32":
os.system("cls")
else:
os.system("clear")
colors = ['red', 'yellow', 'green', 'cyan', 'light_blue', 'magenta']
color_index = 0
bannercli =""" /$$ /$$ /$$
|__/ | $$|__/
/$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$$$$$$| $$ /$$
/$$__ $$ /$$__ $$ /$$__ $$| $$__ $$ |____ $$| $$ /$$$$$$ /$$_____/| $$| $$
| $$ \ $$| $$ \ $$| $$$$$$$$| $$ \ $$ /$$$$$$$| $$|______/| $$ | $$| $$
| $$ | $$| $$ | $$| $$_____/| $$ | $$ /$$__ $$| $$ | $$ | $$| $$
| $$$$$$/| $$$$$$$/| $$$$$$$| $$ | $$| $$$$$$$| $$ | $$$$$$$| $$| $$
\______/ | $$____/ \_______/|__/ |__/ \_______/|__/ \_______/|__/|__/
| $$
| $$
|__/ """
def banner():
for i in range(len(bannercli)):
color = colors[i % len(colors)]
print(colored(bannercli[i], color), end="")
print(colored("\n\nAuthor: ", "red") + "@avencores")
print(colored("Telegram channel: ", "red") + "@hzfnews")
clear()
banner()
config = configparser.ConfigParser()
config.read('config.ini')
if 'openai-token' not in config:
token = input(colored("\nEnter your token ", "yellow") + colored(">> ", "green"))
config['openai-token'] = {'token': token}
with open('config.ini', 'w') as configfile:
config.write(configfile)
clear()
print(colored("Please reload this application!", "cyan"))
exit()
else:
token = config['openai-token']['token']
def menu():
while True:
openai.api_key = token
clear()
banner()
print(colored("\n[1] ", "cyan") + "ChatGPT")
print(colored("[2] ", "cyan") + "DALLE-2")
print(colored("[3] ", "cyan") + "Whisper")
print(colored("\n[4] ", "cyan") + "Change openai token")
print(colored("[5] ", "cyan") + "Change openai session key")
print(colored("\n[6] ", "cyan") + "Check token balance")
print(colored("\n[0] ", "cyan") + "Exit")
choice = input(colored("\nEnter your choice (0-6) ", "yellow") + colored(">> ", "green"))
if choice == "0":
clear()
print(colored("Bye!", "cyan"))
exit()
if choice == "1":
clear()
banner()
print(colored("\n[1] ", "cyan") + "ChatGPT 4.0 (32K)")
print(colored("[2] ", "cyan") + "ChatGPT 4.0")
print(colored("[3] ", "cyan") + "ChatGPT 3.5 (16K)")
print(colored("[4] ", "cyan") + "ChatGPT 3.5")
choice = input(colored("\nEnter your choice (1-4) ", "yellow") + colored(">> ", "green"))
if choice == "1":
clear()
banner()
print(colored("\nChatGPT Model: ", "light_blue") + "4.0 (32K)")
resp = input(colored("\nEnter your request (leave blank to keep existing) ", "yellow") + colored(">> ", "green"))
if resp:
try:
response = openai.ChatCompletion.create(
model="gpt-4-32k",
messages=[{"role": "user", "content": resp}])
output = response["choices"][0]["message"]["content"]
print(colored(f"\nResponse from ChatGPT 4.0 (32K) ", "magenta") + colored(">> ", "green") + output)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
except Exception as e:
clear()
banner()
print(colored("\nERROR ", "yellow") + colored(">> ", "green")) + str(e)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
menu()
else:
menu()
if choice == "2":
clear()
banner()
print(colored("\nChatGPT Model: ", "light_blue") + "4.0")
resp = input(colored("\nEnter your request (leave blank to keep existing) ", "yellow") + colored(">> ", "green"))
if resp:
try:
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": resp}])
output = response["choices"][0]["message"]["content"]
print(colored(f"\nResponse from ChatGPT 4.0 ", "magenta") + colored(">> ", "green") + output)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
except Exception as e:
clear()
banner()
print(colored("\nERROR ", "yellow") + colored(">> ", "green")) + str(e)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
menu()
else:
menu()
if choice == "3":
clear()
banner()
print(colored("\nChatGPT Model: ", "light_blue") + "3.5 (16K)")
resp = input(colored("\nEnter your request (leave blank to keep existing) ", "yellow") + colored(">> ", "green"))
if resp:
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-16k",
messages=[{"role": "user", "content": resp}])
output = response["choices"][0]["message"]["content"]
print(colored(f"\nResponse from ChatGPT 3.5 (16K) ", "magenta") + colored(">> ", "green") + output)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
except Exception as e:
clear()
banner()
print(colored("\nERROR ", "yellow") + colored(">> ", "green")) + str(e)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
menu()
else:
menu()
if choice == "4":
clear()
banner()
print(colored("\nChatGPT Model: ", "light_blue") + "3.5")
resp = input(colored("\nEnter your request (leave blank to keep existing) ", "yellow") + colored(">> ", "green"))
if resp:
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": resp}])
output = response["choices"][0]["message"]["content"]
print(colored(f"\nResponse from ChatGPT 3.5 ", "magenta") + colored(">> ", "green") + output)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
except Exception as e:
clear()
banner()
print(colored("\nERROR ", "yellow") + colored(">> ", "green")) + str(e)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
menu()
else:
menu()
else:
clear()
banner()
print(colored("\nInvalid choice. Please try again.", "light_blue"))
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
elif choice == "2":
clear()
banner()
resp = input(colored("\nEnter your request (leave blank to keep existing) ", "yellow") + colored(">> ", "green"))
if resp:
try:
response = openai.Image.create(
prompt=resp,
n=1,
size="1024x1024")
output = response['data'][0]['url']
print(colored(f"\nResponse from DALLE-2 ", "magenta") + colored(">> ", "green") + output)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
except Exception as e:
clear()
banner()
print(colored("\nERROR ", "yellow") + colored(">> ", "green")) + str(e)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
menu()
elif choice == "3":
clear()
banner()
print(colored("\nFile uploads are currently limited to 25 MB and the following input file types are supported: mp3, mp4, mpeg, mpga, m4a, wav, and webm.", "green"))
resp = input(colored("\nEnter the full path to the file (leave blank to keep existing) ", "yellow") + colored(">> ", "green"))
if resp:
try:
fileaudio = open(f"{resp}", "rb")
response = openai.Audio.transcribe("whisper-1", fileaudio)
fileaudio.close()
output = response["text"]
print(colored(f"\nResponse from Whisper ", "magenta") + colored(">> ", "green") + output)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
except Exception as e:
clear()
banner()
print(colored("\nERROR ", "yellow") + colored(">> ", "green")) + str(e)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
menu()
elif choice == "4":
clear()
banner()
new_token = input(colored("\nEnter new token (leave blank to keep existing) ", "yellow") + colored(">> ", "green"))
if new_token:
config['openai-token']['token'] = new_token
with open('config.ini', 'w') as configfile:
config.write(configfile)
clear()
print(colored("Please reload this application!", "cyan"))
exit()
elif choice == "5":
clear()
banner()
print(colored("\nRead this article: ", "light_blue") + "https://lmmsoft.github.io/openai_update_check_balance_api/")
session_key = input(colored("\nEnter your session-key (leave blank to keep existing) ", "yellow") + colored(">> ", "green"))
if session_key:
config['openai-key'] = {'session_key': session_key}
with open('config.ini', 'w') as configfile:
config.write(configfile)
clear()
print(colored("Please reload this application!", "cyan"))
exit()
elif choice == "6":
clear()
banner()
try:
if 'openai-key' not in config:
clear()
banner()
print(colored("\nRead this article: ", "light_blue") + "https://lmmsoft.github.io/openai_update_check_balance_api/")
session_key = input(colored("\nEnter your session-key ", "yellow") + colored(">> ", "green"))
config['openai-key'] = {'session_key': session_key}
with open('config.ini', 'w') as configfile:
config.write(configfile)
clear()
print(colored("Please reload this application!", "cyan"))
exit()
else:
session_key = config['openai-key']['session_key']
url = "https://api.openai.com/dashboard/billing/credit_grants"
headers = {
"Content-Type": "application/json",
f"Authorization": f"Bearer {session_key}"
}
response = requests.get(url, headers=headers)
data = response.json()
balance = data['total_used']
totalbalance = data['total_granted']
totalavailable = data['total_available']
print(colored("\nTotal token balance: ", "yellow") + str(totalbalance))
print(colored("Used token balance: ", "yellow") + str(balance))
print(colored("Total balance of token: ", "yellow") + str(totalavailable))
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
except Exception as e:
clear()
banner()
print(colored("\nERROR ", "yellow") + colored(">> ", "green")) + str(e)
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
menu()
else:
clear()
banner()
print(colored("\nInvalid choice. Please try again.", "light_blue"))
print(colored("\nPress ENTER to return to the main menu", "cyan"))
input()
menu()