zxsipola123456 commited on
Commit
3ab0624
·
verified ·
1 Parent(s): 787c4e8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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')
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 {
30
+ width: 150px !important
31
+ height: 50px !important
32
+ font-size: 16px !important
33
+ color: white !important
34
+ border: none !important
35
+ border-radius: 5px !important
36
+ cursor: pointer !important
37
+ }
38
+ #warning {background-color: #4CAF50}
39
+ .fixed-size-button button:hover {
40
+ background-color: #45a049 ; /* 浅绿色背景 */
41
+ }
42
+
43
+ /* 可滚动的输出框 */
44
+ .output-box {
45
+ width: 100% !important
46
+ height: 200px !important
47
+ overflow-y: auto !important
48
+ border: 1px solid #ccc !important
49
+ padding: 10px !important
50
+ box-sizing: border-box !important
51
+ }
52
+ """
53
+ # Gradio 前端设计
54
+ app = gr.Blocks()
55
+
56
+ with app:
57
+ gr.Markdown("# <center>api-key验证</center>")
58
+ gr.Markdown(
59
+ "### <center>中转key购买地址[here](https://buy.sipola.cn),ai文案生成可使用中转key,请访问 [here](https://ai.sipola.cn)</center>")
60
+ with gr.Row(variant='panel'):
61
+ with gr.Column():
62
+ transmit_api_key = gr.Textbox(type='password', label='中转API-Key', placeholder='请在此填写您的中转API Key')
63
+ transmit_btn_text = gr.Button("点击验证中转key", elem_id="warning", elem_classes="fixed-size-button")
64
+
65
+ with gr.Column():
66
+ transmit_output_textbox = gr.Textbox(label="显示key状态及可访问的模型列表", elem_classes="output-box", lines=5,max_lines=5)
67
+ transmit_btn_text.click(validate_tran_api_key,transmit_api_key,transmit_output_textbox)
68
+
69
+ with gr.Row(variant='panel'):
70
+ with gr.Column():
71
+ official_api_key = gr.Textbox(type='password', label='官方API Key', placeholder='请在此输入你的官方API Key')
72
+ official_btn_text = gr.Button("点击验证官方key", elem_id="warning", elem_classes="fixed-size-button")
73
+
74
+ with gr.Column():
75
+ official_output_textbox = gr.Textbox(label="显示key状态及可访问的模型列表", elem_classes="output-box", lines=5, max_lines=5 )
76
+ official_btn_text.click(validate_offi_api_key, official_api_key, official_btn_text)
77
+
78
+
79
+ #添加页面底部
80
+ gr.HTML('''
81
+ <div class="footer">
82
+ <center><p>Power by sipola </p></center>
83
+ </div>
84
+ ''')
85
+
86
+ app.launch(show_error=True)