zxsipola123456 commited on
Commit
2a912ac
·
verified ·
1 Parent(s): d2822af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -5
app.py CHANGED
@@ -1,29 +1,56 @@
1
  from openai import OpenAI
2
  import gradio as gr
3
 
4
- # 验证API Key的函数
5
  def validate_tran_api_key(api_key):
6
  if api_key == '':
7
  raise gr.Error('请输入您的中转API Key')
8
  try:
9
  client = OpenAI(api_key=api_key, base_url='https://lmzh.top/v1')
10
- # 调用OpenAI API来验证Key
11
  response = client.models.list()
12
  models_list = [model.id for model in response.data]
13
- return f"API key is valid.\nModels: {', '.join(models_list)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  except Exception as e:
15
  return f"Error: {str(e)}"
16
 
17
  def validate_offi_api_key(api_key):
18
  if api_key == '':
19
- raise gr.Error('请输入您的中转API Key')
20
  try:
21
  client = OpenAI(api_key=api_key, base_url='https://api.openai.com/v1')
22
  response = client.models.list()
23
  models_list = [model.id for model in response.data]
24
- return f"API key is valid.\nModels: {', '.join(models_list)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  except Exception as e:
26
  return f"Error: {str(e)}"
 
27
  css = """
28
  /* 更新CSS 样式 */
29
  .fixed-size-button button {
 
1
  from openai import OpenAI
2
  import gradio as gr
3
 
 
4
  def validate_tran_api_key(api_key):
5
  if api_key == '':
6
  raise gr.Error('请输入您的中转API Key')
7
  try:
8
  client = OpenAI(api_key=api_key, base_url='https://lmzh.top/v1')
 
9
  response = client.models.list()
10
  models_list = [model.id for model in response.data]
11
+
12
+ # 进行简单的聊天测试
13
+ try:
14
+ test_response = client.chat.completions.create(
15
+ model="gpt-4o-mini",
16
+ messages=[
17
+ {"role": "user", "content": "This is a test"}
18
+ ],
19
+ max_tokens=5
20
+ )
21
+ quota_status = "Quota is available."
22
+ except Exception as e:
23
+ quota_status = "Quota might be exhausted or an error occurred."
24
+
25
+ return f"API key is valid.\n{quota_status}\nModels: {', '.join(models_list)}"
26
  except Exception as e:
27
  return f"Error: {str(e)}"
28
 
29
  def validate_offi_api_key(api_key):
30
  if api_key == '':
31
+ raise gr.Error('请输入您的官方API Key')
32
  try:
33
  client = OpenAI(api_key=api_key, base_url='https://api.openai.com/v1')
34
  response = client.models.list()
35
  models_list = [model.id for model in response.data]
36
+
37
+ # 进行简单的聊天测试
38
+ try:
39
+ test_response = client.chat.create(
40
+ model="davinci", # 使用一个合适的模型名称进行测试,如'gpt-4o-mini'等
41
+ messages=[
42
+ {"role": "user", "content": "This is a test"}
43
+ ],
44
+ max_tokens=5
45
+ )
46
+ quota_status = "Quota is available."
47
+ except Exception as e:
48
+ quota_status = "Quota might be exhausted or an error occurred."
49
+
50
+ return f"API key is valid.\n{quota_status}\nModels: {', '.join(models_list)}"
51
  except Exception as e:
52
  return f"Error: {str(e)}"
53
+
54
  css = """
55
  /* 更新CSS 样式 */
56
  .fixed-size-button button {