Woocy commited on
Commit
f34f13a
1 Parent(s): d7f3200

Upload shared.py

Browse files
Files changed (1) hide show
  1. shared.py +55 -0
shared.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from modules.presets import COMPLETION_URL, BALANCE_API_URL, USAGE_API_URL, API_HOST
2
+ import os
3
+ import queue
4
+
5
+ class State:
6
+ interrupted = False
7
+ multi_api_key = False
8
+ completion_url = COMPLETION_URL
9
+ balance_api_url = BALANCE_API_URL
10
+ usage_api_url = USAGE_API_URL
11
+
12
+ def interrupt(self):
13
+ self.interrupted = True
14
+
15
+ def recover(self):
16
+ self.interrupted = False
17
+
18
+ def set_api_host(self, api_host):
19
+ self.completion_url = f"https://{api_host}/v1/chat/completions"
20
+ self.balance_api_url = f"https://{api_host}/dashboard/billing/credit_grants"
21
+ self.usage_api_url = f"https://{api_host}/dashboard/billing/usage"
22
+ os.environ["OPENAI_API_BASE"] = f"https://{api_host}/v1"
23
+
24
+ def reset_api_host(self):
25
+ self.completion_url = COMPLETION_URL
26
+ self.balance_api_url = BALANCE_API_URL
27
+ self.usage_api_url = USAGE_API_URL
28
+ os.environ["OPENAI_API_BASE"] = f"https://{API_HOST}/v1"
29
+ return API_HOST
30
+
31
+ def reset_all(self):
32
+ self.interrupted = False
33
+ self.completion_url = COMPLETION_URL
34
+
35
+ def set_api_key_queue(self, api_key_list):
36
+ self.multi_api_key = True
37
+ self.api_key_queue = queue.Queue()
38
+ for api_key in api_key_list:
39
+ self.api_key_queue.put(api_key)
40
+
41
+ def switching_api_key(self, func):
42
+ if not hasattr(self, "api_key_queue"):
43
+ return func
44
+
45
+ def wrapped(*args, **kwargs):
46
+ api_key = self.api_key_queue.get()
47
+ args[0].api_key = api_key
48
+ ret = func(*args, **kwargs)
49
+ self.api_key_queue.put(api_key)
50
+ return ret
51
+
52
+ return wrapped
53
+
54
+
55
+ state = State()