kakamond commited on
Commit
863081f
·
verified ·
1 Parent(s): f783626

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -104
app.py CHANGED
@@ -1,104 +1,102 @@
1
- import gradio as gr
2
- from openai import OpenAI
3
-
4
-
5
- def predict(message, history, system_prompt, model, api_url, api_key):
6
- # Format history with a given chat template
7
- msgs = [{"role": "system", "content": system_prompt}]
8
- for user, assistant in history:
9
- msgs.append({"role": "user", "content": user})
10
- msgs.append({"role": "system", "content": assistant})
11
-
12
- msgs.append({"role": "user", "content": message})
13
- try:
14
- client = OpenAI(api_key=api_key, base_url=api_url)
15
- response = client.chat.completions.create(
16
- model=model,
17
- messages=msgs,
18
- temperature=0.3,
19
- stream=False,
20
- ).to_dict()["choices"][0]["message"]["content"]
21
-
22
- except Exception as e:
23
- response = f"{e}"
24
-
25
- return response
26
-
27
-
28
- def deepseek(message, history, model, api_key, system_prompt):
29
- response = predict(
30
- message,
31
- history,
32
- system_prompt,
33
- model,
34
- "https://api.deepseek.com",
35
- api_key,
36
- )
37
- outputs = []
38
- for new_token in response:
39
- outputs.append(new_token)
40
- yield "".join(outputs)
41
-
42
-
43
- def kimi(message, history, model, api_key, system_prompt):
44
- response = predict(
45
- message,
46
- history,
47
- system_prompt,
48
- model,
49
- "https://api.moonshot.cn/v1",
50
- api_key,
51
- )
52
- outputs = []
53
- for new_token in response:
54
- outputs.append(new_token)
55
- yield "".join(outputs)
56
-
57
-
58
- if __name__ == "__main__":
59
- with gr.Blocks() as demo: # Create Gradio interface
60
- with gr.Tab("DeepSeek"):
61
- with gr.Accordion(label="⚙️ Settings", open=False) as ds_acc:
62
- ds_model = gr.Dropdown(
63
- choices=["deepseek-chat", "deepseek-r1"],
64
- value="deepseek-chat",
65
- label="Select a model",
66
- )
67
- ds_key = gr.Textbox(
68
- "sk-78bc040108044779828597256dacc7bd",
69
- type="password",
70
- label="API key",
71
- )
72
- ds_sys = gr.Textbox(
73
- "You are a useful assistant. first recognize user request and then reply carfuly and thinking",
74
- label="System prompt",
75
- )
76
- gr.ChatInterface(
77
- deepseek,
78
- title="DeepSeek API 部署实例",
79
- additional_inputs=[ds_model, ds_key, ds_sys],
80
- )
81
-
82
- with gr.Tab("Kimi"):
83
- with gr.Accordion(label="⚙️ Settings", open=False) as kimi_acc:
84
- kimi_model = gr.Dropdown(
85
- choices=["moonshot-v1-32k"],
86
- value="moonshot-v1-32k",
87
- label="Select a model",
88
- )
89
- kimi_key = gr.Textbox(
90
- "sk-r1r5DEgoqaZn0CP6PfJ8u7DfKCmf28kK64cimXCsFs8JUj8a",
91
- type="password",
92
- label="API key",
93
- )
94
- kimi_sys = gr.Textbox(
95
- "You are a useful assistant. first recognize user request and then reply carfuly and thinking",
96
- label="System prompt",
97
- )
98
- gr.ChatInterface(
99
- kimi,
100
- title="KimiChat API 部署实例",
101
- additional_inputs=[kimi_model, kimi_key, kimi_sys],
102
- )
103
-
104
- demo.queue().launch()
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+
4
+
5
+ def predict(message, history, system_prompt, model, api_url, api_key):
6
+ # Format history with a given chat template
7
+ msgs = [{"role": "system", "content": system_prompt}]
8
+ for user, assistant in history:
9
+ msgs.append({"role": "user", "content": user})
10
+ msgs.append({"role": "system", "content": assistant})
11
+
12
+ msgs.append({"role": "user", "content": message})
13
+ try:
14
+ client = OpenAI(api_key=api_key, base_url=api_url)
15
+ response = client.chat.completions.create(
16
+ model=model,
17
+ messages=msgs,
18
+ temperature=0.3,
19
+ stream=False,
20
+ ).to_dict()["choices"][0]["message"]["content"]
21
+
22
+ except Exception as e:
23
+ response = f"{e}"
24
+
25
+ return response
26
+
27
+
28
+ def deepseek(message, history, model, api_key, system_prompt):
29
+ response = predict(
30
+ message,
31
+ history,
32
+ system_prompt,
33
+ model,
34
+ "https://api.deepseek.com",
35
+ api_key,
36
+ )
37
+ outputs = []
38
+ for new_token in response:
39
+ outputs.append(new_token)
40
+ yield "".join(outputs)
41
+
42
+
43
+ def kimi(message, history, model, api_key, system_prompt):
44
+ response = predict(
45
+ message,
46
+ history,
47
+ system_prompt,
48
+ model,
49
+ "https://api.moonshot.cn/v1",
50
+ api_key,
51
+ )
52
+ outputs = []
53
+ for new_token in response:
54
+ outputs.append(new_token)
55
+ yield "".join(outputs)
56
+
57
+
58
+ if __name__ == "__main__":
59
+ with gr.Blocks() as demo: # Create Gradio interface
60
+ with gr.Tab("DeepSeek"):
61
+ with gr.Accordion(label="⚙️ Settings (设置)", open=False) as ds_acc:
62
+ ds_model = gr.Dropdown(
63
+ choices=["deepseek-chat", "deepseek-reasoner"],
64
+ value="deepseek-chat",
65
+ label="Select a model (模型选择)",
66
+ )
67
+ ds_key = gr.Textbox(
68
+ "sk-78bc040108044779828597256dacc7bd",
69
+ type="password",
70
+ label="API key (输入密钥)",
71
+ )
72
+ ds_sys = gr.Textbox(
73
+ "You are a useful assistant. first recognize user request and then reply carfuly and thinking",
74
+ label="System prompt (初始咒语)",
75
+ )
76
+ gr.ChatInterface(
77
+ deepseek,
78
+ additional_inputs=[ds_model, ds_key, ds_sys],
79
+ )
80
+
81
+ with gr.Tab("Kimi"):
82
+ with gr.Accordion(label="⚙️ Settings (设置)", open=False) as kimi_acc:
83
+ kimi_model = gr.Dropdown(
84
+ choices=["moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k"],
85
+ value="moonshot-v1-32k",
86
+ label="Select a model (模型选择)",
87
+ )
88
+ kimi_key = gr.Textbox(
89
+ "sk-r1r5DEgoqaZn0CP6PfJ8u7DfKCmf28kK64cimXCsFs8JUj8a",
90
+ type="password",
91
+ label="API key (输入密钥)",
92
+ )
93
+ kimi_sys = gr.Textbox(
94
+ "You are a useful assistant. first recognize user request and then reply carfuly and thinking",
95
+ label="System prompt (初始咒语)",
96
+ )
97
+ gr.ChatInterface(
98
+ kimi,
99
+ additional_inputs=[kimi_model, kimi_key, kimi_sys],
100
+ )
101
+
102
+ demo.queue().launch()