Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -27,7 +27,7 @@ client = OpenAI(api_key = key)
|
|
27 |
def genUsageStats(do_reset=False):
|
28 |
result = []
|
29 |
for user in unames:
|
30 |
-
|
31 |
fp = dataDir + user + '_log.txt'
|
32 |
if os.path.exists(fp):
|
33 |
with open(fp) as f:
|
@@ -35,12 +35,11 @@ def genUsageStats(do_reset=False):
|
|
35 |
if do_reset:
|
36 |
os.remove(fp)
|
37 |
for line in dataList:
|
38 |
-
(u,
|
39 |
-
|
40 |
else:
|
41 |
total = 0
|
42 |
-
result.append([user, str(
|
43 |
-
|
44 |
return result
|
45 |
|
46 |
def clear():
|
@@ -54,7 +53,9 @@ def setModel(val):
|
|
54 |
|
55 |
def chat(prompt, user_window, pwd_window, past, response, gptModel):
|
56 |
user_window = user_window.lower()
|
|
|
57 |
if user_window == unames[0] and pwd_window == pwdList[0]:
|
|
|
58 |
if prompt == 'stats':
|
59 |
response = genUsageStats()
|
60 |
return [past, response, None]
|
@@ -66,14 +67,17 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel):
|
|
66 |
completion = client.chat.completions.create(model=gptModel,
|
67 |
messages=past)
|
68 |
reply = completion.choices[0].message.content
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
72 |
past.append({"role":"assistant", "content": reply})
|
73 |
try:
|
74 |
dataFile = dataDir + user_window + '_log.txt'
|
75 |
with open(dataFile, 'a') as f:
|
76 |
-
f.write(f'{user_window}: {
|
77 |
# with open(dataFile) as f:
|
78 |
# response += '\n' + f.read()
|
79 |
except Exception as e:
|
|
|
27 |
def genUsageStats(do_reset=False):
|
28 |
result = []
|
29 |
for user in unames:
|
30 |
+
tokens = 0
|
31 |
fp = dataDir + user + '_log.txt'
|
32 |
if os.path.exists(fp):
|
33 |
with open(fp) as f:
|
|
|
35 |
if do_reset:
|
36 |
os.remove(fp)
|
37 |
for line in dataList:
|
38 |
+
(u, t) = line.split(':')
|
39 |
+
tokens += int(t)
|
40 |
else:
|
41 |
total = 0
|
42 |
+
result.append([user, str(tokens)])
|
|
|
43 |
return result
|
44 |
|
45 |
def clear():
|
|
|
53 |
|
54 |
def chat(prompt, user_window, pwd_window, past, response, gptModel):
|
55 |
user_window = user_window.lower()
|
56 |
+
isBoss = False
|
57 |
if user_window == unames[0] and pwd_window == pwdList[0]:
|
58 |
+
isBoss = True
|
59 |
if prompt == 'stats':
|
60 |
response = genUsageStats()
|
61 |
return [past, response, None]
|
|
|
67 |
completion = client.chat.completions.create(model=gptModel,
|
68 |
messages=past)
|
69 |
reply = completion.choices[0].message.content
|
70 |
+
tokens = completion.usage.total_tokens
|
71 |
+
response += "\n\nYOU: " + prompt + "\nGPT: " + reply
|
72 |
+
if isBoss:
|
73 |
+
response += f"\n{gptModel}: {tokens} tokens"
|
74 |
+
if tokens > 40000:
|
75 |
+
response += "\n\nTHIS DIALOG IS GETTING TOO LONG. PLEASE RESTART CONVERSATION SOON."
|
76 |
past.append({"role":"assistant", "content": reply})
|
77 |
try:
|
78 |
dataFile = dataDir + user_window + '_log.txt'
|
79 |
with open(dataFile, 'a') as f:
|
80 |
+
f.write(f'{user_window}: {tokens}\n')
|
81 |
# with open(dataFile) as f:
|
82 |
# response += '\n' + f.read()
|
83 |
except Exception as e:
|