curry tang
commited on
Commit
·
03b5791
1
Parent(s):
9105300
update
Browse files
app.py
CHANGED
@@ -1,7 +1,11 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from langchain_core.messages import HumanMessage, AIMessage
|
3 |
from llm import DeepSeekLLM, OpenRouterLLM, TongYiLLM
|
4 |
from config import settings
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
deep_seek_llm = DeepSeekLLM(api_key=settings.deep_seek_api_key)
|
@@ -14,13 +18,28 @@ def init_chat():
|
|
14 |
|
15 |
|
16 |
def predict(message, history, chat):
|
|
|
17 |
if chat is None:
|
18 |
chat = init_chat()
|
19 |
history_messages = []
|
20 |
for human, assistant in history:
|
21 |
history_messages.append(HumanMessage(content=human))
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
response_message = ''
|
26 |
for chunk in chat.stream(history_messages):
|
@@ -63,120 +82,48 @@ with gr.Blocks() as app:
|
|
63 |
|
64 |
@gr.render(inputs=provider)
|
65 |
def show_model_config_panel(_provider):
|
66 |
-
|
67 |
-
with gr.Column():
|
68 |
-
model = gr.Dropdown(
|
69 |
-
label='模型',
|
70 |
-
choices=deep_seek_llm.support_models,
|
71 |
-
value=deep_seek_llm.default_model
|
72 |
-
)
|
73 |
-
temperature = gr.Slider(
|
74 |
-
minimum=0.0,
|
75 |
-
maximum=1.0,
|
76 |
-
step=0.1,
|
77 |
-
value=deep_seek_llm.default_temperature,
|
78 |
-
label="Temperature",
|
79 |
-
key="temperature",
|
80 |
-
)
|
81 |
-
max_tokens = gr.Slider(
|
82 |
-
minimum=1024,
|
83 |
-
maximum=1024 * 20,
|
84 |
-
step=128,
|
85 |
-
value=deep_seek_llm.default_max_tokens,
|
86 |
-
label="Max Tokens",
|
87 |
-
key="max_tokens",
|
88 |
-
)
|
89 |
-
model.change(
|
90 |
-
fn=update_chat,
|
91 |
-
inputs=[provider, chat_engine, model, temperature, max_tokens],
|
92 |
-
outputs=[chat_engine],
|
93 |
-
)
|
94 |
-
temperature.change(
|
95 |
-
fn=update_chat,
|
96 |
-
inputs=[provider, chat_engine, model, temperature, max_tokens],
|
97 |
-
outputs=[chat_engine],
|
98 |
-
)
|
99 |
-
max_tokens.change(
|
100 |
-
fn=update_chat,
|
101 |
-
inputs=[provider, chat_engine, model, temperature, max_tokens],
|
102 |
-
outputs=[chat_engine],
|
103 |
-
)
|
104 |
if _provider == 'OpenRouter':
|
105 |
-
|
106 |
-
model = gr.Dropdown(
|
107 |
-
label='模型',
|
108 |
-
choices=open_router_llm.support_models,
|
109 |
-
value=open_router_llm.default_model
|
110 |
-
)
|
111 |
-
temperature = gr.Slider(
|
112 |
-
minimum=0.0,
|
113 |
-
maximum=1.0,
|
114 |
-
step=0.1,
|
115 |
-
value=open_router_llm.default_temperature,
|
116 |
-
label="Temperature",
|
117 |
-
key="temperature",
|
118 |
-
)
|
119 |
-
max_tokens = gr.Slider(
|
120 |
-
minimum=1024,
|
121 |
-
maximum=1024 * 20,
|
122 |
-
step=128,
|
123 |
-
value=open_router_llm.default_max_tokens,
|
124 |
-
label="Max Tokens",
|
125 |
-
key="max_tokens",
|
126 |
-
)
|
127 |
-
model.change(
|
128 |
-
fn=update_chat,
|
129 |
-
inputs=[provider, chat_engine, model, temperature, max_tokens],
|
130 |
-
outputs=[chat_engine],
|
131 |
-
)
|
132 |
-
temperature.change(
|
133 |
-
fn=update_chat,
|
134 |
-
inputs=[provider, chat_engine, model, temperature, max_tokens],
|
135 |
-
outputs=[chat_engine],
|
136 |
-
)
|
137 |
-
max_tokens.change(
|
138 |
-
fn=update_chat,
|
139 |
-
inputs=[provider, chat_engine, model, temperature, max_tokens],
|
140 |
-
outputs=[chat_engine],
|
141 |
-
)
|
142 |
if _provider == 'Tongyi':
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
temperature = gr.Slider(
|
150 |
-
minimum=0.0,
|
151 |
-
maximum=1.0,
|
152 |
-
step=0.1,
|
153 |
-
value=tongyi_llm.default_temperature,
|
154 |
-
label="Temperature",
|
155 |
-
key="temperature",
|
156 |
-
)
|
157 |
-
max_tokens = gr.Slider(
|
158 |
-
minimum=1000,
|
159 |
-
maximum=2000,
|
160 |
-
step=100,
|
161 |
-
value=tongyi_llm.default_max_tokens,
|
162 |
-
label="Max Tokens",
|
163 |
-
key="max_tokens",
|
164 |
-
)
|
165 |
-
model.change(
|
166 |
-
fn=update_chat,
|
167 |
-
inputs=[provider, chat_engine, model, temperature, max_tokens],
|
168 |
-
outputs=[chat_engine],
|
169 |
)
|
170 |
-
temperature.
|
171 |
-
|
172 |
-
|
173 |
-
|
|
|
|
|
|
|
174 |
)
|
175 |
-
max_tokens.
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
|
|
|
|
179 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
|
182 |
app.launch(debug=settings.debug, show_api=False)
|
|
|
1 |
import gradio as gr
|
2 |
+
from gradio import FileData
|
3 |
from langchain_core.messages import HumanMessage, AIMessage
|
4 |
from llm import DeepSeekLLM, OpenRouterLLM, TongYiLLM
|
5 |
from config import settings
|
6 |
+
import base64
|
7 |
+
from PIL import Image
|
8 |
+
import io
|
9 |
|
10 |
|
11 |
deep_seek_llm = DeepSeekLLM(api_key=settings.deep_seek_api_key)
|
|
|
18 |
|
19 |
|
20 |
def predict(message, history, chat):
|
21 |
+
file_len = len(message.files)
|
22 |
if chat is None:
|
23 |
chat = init_chat()
|
24 |
history_messages = []
|
25 |
for human, assistant in history:
|
26 |
history_messages.append(HumanMessage(content=human))
|
27 |
+
if assistant is not None:
|
28 |
+
history_messages.append(AIMessage(content=assistant))
|
29 |
+
|
30 |
+
if file_len == 0:
|
31 |
+
history_messages.append(HumanMessage(content=message.text))
|
32 |
+
else:
|
33 |
+
file = message.files[0]
|
34 |
+
with Image.open(file.path) as img:
|
35 |
+
buffer = io.BytesIO()
|
36 |
+
img = img.convert('RGB')
|
37 |
+
img.save(buffer, format="JPEG")
|
38 |
+
image_data = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
39 |
+
history_messages.append(HumanMessage(content=[
|
40 |
+
{"type": "text", "text": message.text},
|
41 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}}
|
42 |
+
]))
|
43 |
|
44 |
response_message = ''
|
45 |
for chunk in chat.stream(history_messages):
|
|
|
82 |
|
83 |
@gr.render(inputs=provider)
|
84 |
def show_model_config_panel(_provider):
|
85 |
+
_support_llm = deep_seek_llm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
if _provider == 'OpenRouter':
|
87 |
+
_support_llm = open_router_llm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
if _provider == 'Tongyi':
|
89 |
+
_support_llm = tongyi_llm
|
90 |
+
with gr.Column():
|
91 |
+
model = gr.Dropdown(
|
92 |
+
label='模型',
|
93 |
+
choices=_support_llm.support_models,
|
94 |
+
value=_support_llm.default_model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
)
|
96 |
+
temperature = gr.Slider(
|
97 |
+
minimum=0.0,
|
98 |
+
maximum=1.0,
|
99 |
+
step=0.1,
|
100 |
+
value=_support_llm.default_temperature,
|
101 |
+
label="Temperature",
|
102 |
+
key="temperature",
|
103 |
)
|
104 |
+
max_tokens = gr.Slider(
|
105 |
+
minimum=1024,
|
106 |
+
maximum=_support_llm.default_max_tokens,
|
107 |
+
step=128,
|
108 |
+
value=_support_llm.default_max_tokens,
|
109 |
+
label="Max Tokens",
|
110 |
+
key="max_tokens",
|
111 |
)
|
112 |
+
model.change(
|
113 |
+
fn=update_chat,
|
114 |
+
inputs=[provider, chat_engine, model, temperature, max_tokens],
|
115 |
+
outputs=[chat_engine],
|
116 |
+
)
|
117 |
+
temperature.change(
|
118 |
+
fn=update_chat,
|
119 |
+
inputs=[provider, chat_engine, model, temperature, max_tokens],
|
120 |
+
outputs=[chat_engine],
|
121 |
+
)
|
122 |
+
max_tokens.change(
|
123 |
+
fn=update_chat,
|
124 |
+
inputs=[provider, chat_engine, model, temperature, max_tokens],
|
125 |
+
outputs=[chat_engine],
|
126 |
+
)
|
127 |
|
128 |
|
129 |
app.launch(debug=settings.debug, show_api=False)
|
llm.py
CHANGED
@@ -55,11 +55,12 @@ class DeepSeekLLM(BaseLLM):
|
|
55 |
_support_models = ['deepseek-chat', 'deepseek-coder']
|
56 |
_base_url = 'https://api.deepseek.com/v1'
|
57 |
_default_model = 'deepseek-chat'
|
|
|
58 |
|
59 |
|
60 |
class OpenRouterLLM(BaseLLM):
|
61 |
_support_models = [
|
62 |
-
'anthropic/claude-3.5-sonnet', 'openai/gpt-4o',
|
63 |
'nvidia/nemotron-4-340b-instruct', 'deepseek/deepseek-coder',
|
64 |
'google/gemini-flash-1.5', 'deepseek/deepseek-chat',
|
65 |
'liuhaotian/llava-yi-34b', 'qwen/qwen-110b-chat',
|
@@ -68,10 +69,11 @@ class OpenRouterLLM(BaseLLM):
|
|
68 |
]
|
69 |
_base_url = 'https://openrouter.ai/api/v1'
|
70 |
_default_model = 'anthropic/claude-3.5-sonnet'
|
|
|
71 |
|
72 |
|
73 |
class TongYiLLM(BaseLLM):
|
74 |
_support_models = ['qwen-turbo', 'qwen-plus', 'qwen-max', 'qwen-long']
|
75 |
_default_model = 'qwen-turbo'
|
76 |
_base_url = 'https://dashscope.aliyuncs.com/compatible-mode/v1'
|
77 |
-
_default_max_tokens: int =
|
|
|
55 |
_support_models = ['deepseek-chat', 'deepseek-coder']
|
56 |
_base_url = 'https://api.deepseek.com/v1'
|
57 |
_default_model = 'deepseek-chat'
|
58 |
+
_default_max_tokens = 4096
|
59 |
|
60 |
|
61 |
class OpenRouterLLM(BaseLLM):
|
62 |
_support_models = [
|
63 |
+
'openai/gpt-4o-mini', 'anthropic/claude-3.5-sonnet', 'openai/gpt-4o',
|
64 |
'nvidia/nemotron-4-340b-instruct', 'deepseek/deepseek-coder',
|
65 |
'google/gemini-flash-1.5', 'deepseek/deepseek-chat',
|
66 |
'liuhaotian/llava-yi-34b', 'qwen/qwen-110b-chat',
|
|
|
69 |
]
|
70 |
_base_url = 'https://openrouter.ai/api/v1'
|
71 |
_default_model = 'anthropic/claude-3.5-sonnet'
|
72 |
+
_default_max_tokens = 16 * 1024
|
73 |
|
74 |
|
75 |
class TongYiLLM(BaseLLM):
|
76 |
_support_models = ['qwen-turbo', 'qwen-plus', 'qwen-max', 'qwen-long']
|
77 |
_default_model = 'qwen-turbo'
|
78 |
_base_url = 'https://dashscope.aliyuncs.com/compatible-mode/v1'
|
79 |
+
_default_max_tokens: int = 32 * 1024
|