Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,28 @@
|
|
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')
|
@@ -82,8 +104,16 @@ with app:
|
|
82 |
with gr.Column():
|
83 |
official_output_textbox = gr.Textbox(label="显示key状态及可访问的模型列表", lines=5, max_lines=5, elem_classes="custom-textbox" )
|
84 |
official_btn_text.click(validate_offi_api_key, official_api_key, official_output_textbox)
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
#添加页面底部
|
88 |
gr.HTML('''
|
89 |
<div class="footer">
|
|
|
1 |
from openai import OpenAI
|
2 |
import gradio as gr
|
3 |
|
4 |
+
def generate_response(question, model_name, endpoint, api_key):
|
5 |
+
# 创建 OpenAI 客户端
|
6 |
+
client = OpenAI(api_key=api_key, base_url=endpoint)
|
7 |
+
messages = [
|
8 |
+
{"role": "user", "content": question}
|
9 |
+
]
|
10 |
+
|
11 |
+
try:
|
12 |
+
response = client.chat.completions.create(
|
13 |
+
model=model_name,
|
14 |
+
messages=messages,
|
15 |
+
max_tokens=100,
|
16 |
+
temperature=0.1
|
17 |
+
)
|
18 |
+
return response.choices[0].message.content
|
19 |
+
except Exception as e:
|
20 |
+
return f"响应生成失败: {str(e)}"
|
21 |
+
|
22 |
+
base_url_options=["https://lmzh.top/v1","https://api.openai.com"]
|
23 |
+
model_options = ["gpt-3.5-turbo", "gpt-4o-mini", "gpt-4o"]
|
24 |
+
|
25 |
+
|
26 |
def validate_tran_api_key(api_key):
|
27 |
if api_key == '':
|
28 |
raise gr.Error('请输入您的中转API Key')
|
|
|
104 |
with gr.Column():
|
105 |
official_output_textbox = gr.Textbox(label="显示key状态及可访问的模型列表", lines=5, max_lines=5, elem_classes="custom-textbox" )
|
106 |
official_btn_text.click(validate_offi_api_key, official_api_key, official_output_textbox)
|
107 |
+
with gr.Row(variant='panel'):
|
108 |
+
with gr.Column():
|
109 |
+
question=gr.Textbox(label="请输入你的问题: ")
|
110 |
+
model_name=gr.Dropdown(label="选择模型", choices=model_options, value="gpt-3.5-turbo")
|
111 |
+
endpoint=gr.Dropdown(label="选择接口地址", choices=base_url_options, value="https://lmzh.top/v1")
|
112 |
+
api_key=gr.Textbox(type='password', label='api-key', placeholder='请在此填写您的API Key')
|
113 |
+
message_btn = gr.Button("发送", variant="primary", elem_classes="custom-button")
|
114 |
+
with gr.Column():
|
115 |
+
output_textbox = gr.Textbox(label="显示问题答案", lines=5, max_lines=5, elem_classes="custom-textbox" )
|
116 |
+
message_btn.click(generate_response,inputs=[question,mode_name,endpoint,api_key],outputs=message_textbox)
|
117 |
#添加页面底部
|
118 |
gr.HTML('''
|
119 |
<div class="footer">
|