simondby commited on
Commit
aba28a5
·
1 Parent(s): d65b470

update api key

Browse files
Files changed (3) hide show
  1. app.py +4 -1
  2. modules/utils.py +18 -0
  3. requirements.txt +1 -0
app.py CHANGED
@@ -51,7 +51,10 @@ else:
51
  and not os.path.exists("api_key.txt")
52
  and os.environ.get('OPENAI_API_KEY') != None
53
  ):
54
- my_api_key = os.environ.get('OPENAI_API_KEY')
 
 
 
55
  if os.path.exists("auth.json"):
56
  authflag = True
57
  with open("auth.json", "r", encoding='utf-8') as f:
 
51
  and not os.path.exists("api_key.txt")
52
  and os.environ.get('OPENAI_API_KEY') != None
53
  ):
54
+ api_keys = os.environ.get("api_keys") or ''
55
+ ls_keys = [key.strip() for key in api_keys.splitlines() if key.strip()]
56
+ default_key = os.environ.get('OPENAI_API_KEY')
57
+ my_api_key = api_picker(ls_keys, default_key)
58
  if os.path.exists("auth.json"):
59
  authflag = True
60
  with open("auth.json", "r", encoding='utf-8') as f:
modules/utils.py CHANGED
@@ -14,6 +14,7 @@ import sys
14
  import subprocess
15
  import pytz
16
  import pymysql
 
17
 
18
  import gradio as gr
19
  from pypinyin import lazy_pinyin
@@ -511,6 +512,23 @@ def get_proxies():
511
 
512
  return proxies
513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514
  def ding_send(message):
515
  headers = {'Content-Type': 'application/json;charset=utf-8'}
516
  data = {
 
14
  import subprocess
15
  import pytz
16
  import pymysql
17
+ import random
18
 
19
  import gradio as gr
20
  from pypinyin import lazy_pinyin
 
512
 
513
  return proxies
514
 
515
+ def api_picker(keys,backup_key):
516
+ url = 'https://api.openai.com/v1/models'
517
+ headers = {"Content-Type": "application/json"}
518
+ random.shuffle(keys)
519
+ for key in keys:
520
+ try:
521
+ headers['Authorization'] = f"Bearer {key}"
522
+ response = requests.get(url, headers=headers, proxies=get_proxies())
523
+ if response.status_code == 200:
524
+ return key
525
+ break
526
+ except:
527
+ continue
528
+ else:
529
+ headers['Authorization'] = f"Bearer {backup_key}"
530
+ return backup_key
531
+
532
  def ding_send(message):
533
  headers = {'Content-Type': 'application/json;charset=utf-8'}
534
  data = {
requirements.txt CHANGED
@@ -12,3 +12,4 @@ langchain
12
  markdown
13
  pytz
14
  pymysql==1.0.2
 
 
12
  markdown
13
  pytz
14
  pymysql==1.0.2
15
+ random