Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,21 +6,22 @@ import modelscope_studio.components.antd as antd
|
|
6 |
import modelscope_studio.components.antdx as antdx
|
7 |
import modelscope_studio.components.base as ms
|
8 |
import modelscope_studio.components.pro as pro
|
9 |
-
|
10 |
from config import DEFAULT_LOCALE, DEFAULT_SETTINGS, DEFAULT_THEME, DEFAULT_SUGGESTIONS, save_history, get_text, user_config, bot_config, welcome_config, api_key, MODEL_OPTIONS_MAP
|
11 |
from ui_components.logo import Logo
|
12 |
from ui_components.settings_header import SettingsHeader
|
13 |
from ui_components.thinking_button import ThinkingButton
|
|
|
14 |
|
15 |
-
|
16 |
-
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1")
|
17 |
|
18 |
|
19 |
def format_history(history, sys_prompt):
|
20 |
-
messages = [{
|
21 |
-
|
22 |
-
|
23 |
-
}]
|
|
|
24 |
for item in history:
|
25 |
if item["role"] == "user":
|
26 |
messages.append({"role": "user", "content": item["content"]})
|
@@ -72,18 +73,14 @@ class Gradio_Events:
|
|
72 |
state: gr.update(value=state_value),
|
73 |
}
|
74 |
try:
|
75 |
-
response =
|
76 |
model=model,
|
77 |
messages=messages,
|
78 |
stream=True,
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
"thinking_budget":
|
84 |
-
settings.get("thinking_budget", 1) * 1024
|
85 |
-
} if enable_thinking else {})
|
86 |
-
})
|
87 |
start_time = time.time()
|
88 |
reasoning_content = ""
|
89 |
answer_content = ""
|
@@ -91,10 +88,12 @@ class Gradio_Events:
|
|
91 |
is_answering = False
|
92 |
contents = [None, None]
|
93 |
for chunk in response:
|
94 |
-
if not chunk.choices
|
|
|
|
|
95 |
pass
|
96 |
else:
|
97 |
-
delta = chunk.choices[0].
|
98 |
if hasattr(
|
99 |
delta,
|
100 |
'reasoning_content') and delta.reasoning_content:
|
@@ -111,23 +110,22 @@ class Gradio_Events:
|
|
111 |
}
|
112 |
is_thinking = True
|
113 |
reasoning_content += delta.reasoning_content
|
114 |
-
|
115 |
-
if
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
answer_content += delta.content
|
131 |
|
132 |
if contents[0]:
|
133 |
contents[0]["content"] = reasoning_content
|
@@ -136,11 +134,14 @@ class Gradio_Events:
|
|
136 |
history[-1]["content"] = [
|
137 |
content for content in contents if content
|
138 |
]
|
|
|
139 |
history[-1]["loading"] = False
|
140 |
yield {
|
141 |
chatbot: gr.update(value=history),
|
142 |
state: gr.update(value=state_value)
|
143 |
}
|
|
|
|
|
144 |
history[-1]["status"] = "done"
|
145 |
cost_time = "{:.2f}".format(time.time() - start_time)
|
146 |
history[-1]["footer"] = get_text(f"{cost_time}s",
|
@@ -150,6 +151,7 @@ class Gradio_Events:
|
|
150 |
state: gr.update(value=state_value),
|
151 |
}
|
152 |
except Exception as e:
|
|
|
153 |
history[-1]["loading"] = False
|
154 |
history[-1]["status"] = "done"
|
155 |
history[-1]["content"] += [{
|
|
|
6 |
import modelscope_studio.components.antdx as antdx
|
7 |
import modelscope_studio.components.base as ms
|
8 |
import modelscope_studio.components.pro as pro
|
9 |
+
import dashscope
|
10 |
from config import DEFAULT_LOCALE, DEFAULT_SETTINGS, DEFAULT_THEME, DEFAULT_SUGGESTIONS, save_history, get_text, user_config, bot_config, welcome_config, api_key, MODEL_OPTIONS_MAP
|
11 |
from ui_components.logo import Logo
|
12 |
from ui_components.settings_header import SettingsHeader
|
13 |
from ui_components.thinking_button import ThinkingButton
|
14 |
+
from dashscope import Generation
|
15 |
|
16 |
+
dashscope.api_key = api_key
|
|
|
17 |
|
18 |
|
19 |
def format_history(history, sys_prompt):
|
20 |
+
# messages = [{
|
21 |
+
# "role": "system",
|
22 |
+
# "content": sys_prompt,
|
23 |
+
# }]
|
24 |
+
messages = []
|
25 |
for item in history:
|
26 |
if item["role"] == "user":
|
27 |
messages.append({"role": "user", "content": item["content"]})
|
|
|
73 |
state: gr.update(value=state_value),
|
74 |
}
|
75 |
try:
|
76 |
+
response = Generation.call(
|
77 |
model=model,
|
78 |
messages=messages,
|
79 |
stream=True,
|
80 |
+
result_format='message',
|
81 |
+
incremental_output=True,
|
82 |
+
enable_thinking=enable_thinking,
|
83 |
+
thinking_budget=settings.get("thinking_budget", 1) * 1024)
|
|
|
|
|
|
|
|
|
84 |
start_time = time.time()
|
85 |
reasoning_content = ""
|
86 |
answer_content = ""
|
|
|
88 |
is_answering = False
|
89 |
contents = [None, None]
|
90 |
for chunk in response:
|
91 |
+
if (not chunk.output.choices[0].message.get("content")
|
92 |
+
and not chunk.output.choices[0].message.get(
|
93 |
+
"reasoning_content")):
|
94 |
pass
|
95 |
else:
|
96 |
+
delta = chunk.output.choices[0].message
|
97 |
if hasattr(
|
98 |
delta,
|
99 |
'reasoning_content') and delta.reasoning_content:
|
|
|
110 |
}
|
111 |
is_thinking = True
|
112 |
reasoning_content += delta.reasoning_content
|
113 |
+
if hasattr(delta, 'content') and delta.content:
|
114 |
+
if not is_answering:
|
115 |
+
thought_cost_time = "{:.2f}".format(time.time() -
|
116 |
+
start_time)
|
117 |
+
if contents[0]:
|
118 |
+
contents[0]["options"]["title"] = get_text(
|
119 |
+
f"End of Thought ({thought_cost_time}s)",
|
120 |
+
f"已深度思考 (用时{thought_cost_time}s)")
|
121 |
+
contents[0]["options"]["status"] = "done"
|
122 |
+
contents[1] = {
|
123 |
+
"type": "text",
|
124 |
+
"content": "",
|
125 |
+
}
|
126 |
+
|
127 |
+
is_answering = True
|
128 |
+
answer_content += delta.content
|
|
|
129 |
|
130 |
if contents[0]:
|
131 |
contents[0]["content"] = reasoning_content
|
|
|
134 |
history[-1]["content"] = [
|
135 |
content for content in contents if content
|
136 |
]
|
137 |
+
|
138 |
history[-1]["loading"] = False
|
139 |
yield {
|
140 |
chatbot: gr.update(value=history),
|
141 |
state: gr.update(value=state_value)
|
142 |
}
|
143 |
+
print("model: ", model, "-", "reasoning_content: ",
|
144 |
+
reasoning_content, "\n", "content: ", answer_content)
|
145 |
history[-1]["status"] = "done"
|
146 |
cost_time = "{:.2f}".format(time.time() - start_time)
|
147 |
history[-1]["footer"] = get_text(f"{cost_time}s",
|
|
|
151 |
state: gr.update(value=state_value),
|
152 |
}
|
153 |
except Exception as e:
|
154 |
+
print("model: ", model, "-", "Error: ", e)
|
155 |
history[-1]["loading"] = False
|
156 |
history[-1]["status"] = "done"
|
157 |
history[-1]["content"] += [{
|