Spaces:
Running
Running
File size: 4,841 Bytes
404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 404b247 be22f80 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
import os
import gradio as gr
from openai import OpenAI
from utils import EN_US
ZH2EN = {
"请先在设置中配置有效 API 密钥": "Please set valid api keys in settings first.",
"⚙️ 设置": "⚙️ Settings",
"模型选择": "Select a model",
"API 密钥": "API key",
"系统提示词": "System prompt",
"最大 token 数": "Max new tokens",
"温度参数": "Temperature",
"Top-P 采样": "Top P sampling",
}
def _L(zh_txt: str):
return ZH2EN[zh_txt] if EN_US else zh_txt
def predict(msg, history, system_prompt, model, api_url, api_key, max_tk, temp, top_p):
try:
if not api_key:
raise ValueError(_L("请先在设置中配置有效 API 密钥"))
msgs = [{"role": "system", "content": system_prompt}]
for user, assistant in history:
msgs.append({"role": "user", "content": user})
msgs.append({"role": "system", "content": assistant})
msgs.append({"role": "user", "content": msg})
client = OpenAI(api_key=api_key, base_url=api_url)
response = client.chat.completions.create(
model=model,
messages=msgs,
max_tokens=max_tk,
temperature=temp,
top_p=top_p,
stream=False,
).to_dict()["choices"][0]["message"]["content"]
except Exception as e:
response = f"{e}"
return response
def deepseek(message, history, model, api_key, system_prompt, max_tk, temp, top_p):
response = predict(
message,
history,
system_prompt,
model,
"https://api.deepseek.com",
api_key,
max_tk,
temp,
top_p,
)
outputs = []
for new_token in response:
outputs.append(new_token)
yield "".join(outputs)
def kimi(message, history, model, api_key, system_prompt, max_tk, temp, top_p):
response = predict(
message,
history,
system_prompt,
model,
"https://api.moonshot.cn/v1",
api_key,
max_tk,
temp,
top_p,
)
outputs = []
for new_token in response:
outputs.append(new_token)
yield "".join(outputs)
def LLM_APIs():
with gr.Blocks() as apis:
with gr.Tab("DeepSeek"):
with gr.Accordion(label=_L("⚙️ 设置"), open=False) as ds_acc:
ds_model = gr.Dropdown(
choices=["deepseek-chat", "deepseek-reasoner"],
value="deepseek-chat",
label=_L("模型选择"),
)
ds_key = gr.Textbox(
os.getenv("ds_api_key"),
type="password",
label=_L("API 密钥"),
)
ds_sys = gr.Textbox(
"You are a useful assistant. first recognize user request and then reply carfuly and thinking",
label=_L("系统提示词"),
)
ds_maxtk = gr.Slider(0, 32000, 10000, label=_L("最大 token 数"))
ds_temp = gr.Slider(0, 1, 0.3, label=_L("温度参数"))
ds_topp = gr.Slider(0, 1, 0.95, label=_L("Top-P 采样"))
gr.ChatInterface(
deepseek,
additional_inputs=[
ds_model,
ds_key,
ds_sys,
ds_maxtk,
ds_temp,
ds_topp,
],
)
with gr.Tab("Kimi"):
with gr.Accordion(label=_L("⚙️ 设置"), open=False) as kimi_acc:
kimi_model = gr.Dropdown(
choices=["moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k"],
value="moonshot-v1-32k",
label=_L("模型选择"),
)
kimi_key = gr.Textbox(
os.getenv("kimi_api_key"),
type="password",
label=_L("API 密钥"),
)
kimi_sys = gr.Textbox(
"You are a useful assistant. first recognize user request and then reply carfuly and thinking",
label=_L("系统提示词"),
)
kimi_maxtk = gr.Slider(0, 32000, 10000, label=_L("最大 token 数"))
kimi_temp = gr.Slider(0, 1, 0.3, label=_L("温度参数"))
kimi_topp = gr.Slider(0, 1, 0.95, label=_L("Top-P 采样"))
gr.ChatInterface(
kimi,
additional_inputs=[
kimi_model,
kimi_key,
kimi_sys,
kimi_maxtk,
kimi_temp,
kimi_topp,
],
)
return apis.queue()
|