Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,225 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
+
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 |
+
from modelscope_studio.components.pro.chatbot import (ChatbotBotConfig,
|
| 10 |
+
ChatbotPromptsConfig,
|
| 11 |
+
ChatbotUserConfig,
|
| 12 |
+
ChatbotWelcomeConfig)
|
| 13 |
+
from openai import OpenAI
|
| 14 |
+
|
| 15 |
+
gw_api_key = os.getenv("GW_API_KEY")
|
| 16 |
+
|
| 17 |
+
client = OpenAI(
|
| 18 |
+
base_url='http://api-test.geniuworks.com/v2',
|
| 19 |
+
api_key=gw_api_key', # ModelScope Token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
+
model = "xinyuan-32b-v0609"
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def prompt_select(e: gr.EventData):
|
| 26 |
+
return gr.update(value=e._data["payload"][0]["value"]["description"])
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def clear():
|
| 30 |
+
return gr.update(value=None)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def retry(chatbot_value, e: gr.EventData):
|
| 34 |
+
index = e._data["payload"][0]["index"]
|
| 35 |
+
chatbot_value = chatbot_value[:index]
|
| 36 |
+
|
| 37 |
+
yield gr.update(loading=True), gr.update(value=chatbot_value), gr.update(
|
| 38 |
+
disabled=True)
|
| 39 |
+
for chunk in submit(None, chatbot_value):
|
| 40 |
+
yield chunk
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def cancel(chatbot_value):
|
| 44 |
+
chatbot_value[-1]["loading"] = False
|
| 45 |
+
chatbot_value[-1]["status"] = "done"
|
| 46 |
+
chatbot_value[-1]["footer"] = "Chat completion paused"
|
| 47 |
+
return gr.update(value=chatbot_value), gr.update(loading=False), gr.update(
|
| 48 |
+
disabled=False)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def format_history(history):
|
| 52 |
+
messages = []
|
| 53 |
+
for item in history:
|
| 54 |
+
if item["role"] == "user":
|
| 55 |
+
messages.append({"role": "user", "content": item["content"]})
|
| 56 |
+
elif item["role"] == "assistant":
|
| 57 |
+
# ignore thought message
|
| 58 |
+
messages.append({
|
| 59 |
+
"role": "assistant",
|
| 60 |
+
"content": item["content"][-1]["content"]
|
| 61 |
+
})
|
| 62 |
+
return messages
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def submit(sender_value, chatbot_value):
|
| 66 |
+
if sender_value is not None:
|
| 67 |
+
chatbot_value.append({
|
| 68 |
+
"role": "user",
|
| 69 |
+
"content": sender_value,
|
| 70 |
+
})
|
| 71 |
+
history_messages = format_history(chatbot_value)
|
| 72 |
+
chatbot_value.append({
|
| 73 |
+
"role": "assistant",
|
| 74 |
+
"content": [],
|
| 75 |
+
"loading": True,
|
| 76 |
+
"status": "pending"
|
| 77 |
+
})
|
| 78 |
+
yield {
|
| 79 |
+
sender: gr.update(value=None, loading=True),
|
| 80 |
+
clear_btn: gr.update(disabled=True),
|
| 81 |
+
chatbot: gr.update(value=chatbot_value)
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
response = client.chat.completions.create(model=model,
|
| 86 |
+
messages=history_messages,
|
| 87 |
+
stream=True,
|
| 88 |
+
max_tokens=2048,
|
| 89 |
+
temperature=0.6,
|
| 90 |
+
top_p=0.95,
|
| 91 |
+
)
|
| 92 |
+
thought_done = False
|
| 93 |
+
start_time = time.time()
|
| 94 |
+
message_content = chatbot_value[-1]["content"]
|
| 95 |
+
# thought content
|
| 96 |
+
message_content.append({
|
| 97 |
+
"copyable": False,
|
| 98 |
+
"editable": False,
|
| 99 |
+
"type": "tool",
|
| 100 |
+
"content": "",
|
| 101 |
+
"options": {
|
| 102 |
+
"title": "Thinking..."
|
| 103 |
+
}
|
| 104 |
+
})
|
| 105 |
+
# content
|
| 106 |
+
message_content.append({
|
| 107 |
+
"type": "text",
|
| 108 |
+
"content": "",
|
| 109 |
+
})
|
| 110 |
+
for chunk in response:
|
| 111 |
+
reasoning_content = chunk.choices[0].delta.reasoning_content
|
| 112 |
+
content = chunk.choices[0].delta.content
|
| 113 |
+
chatbot_value[-1]["loading"] = False
|
| 114 |
+
message_content[-2]["content"] += reasoning_content or ""
|
| 115 |
+
message_content[-1]["content"] += content or ""
|
| 116 |
+
|
| 117 |
+
if content and not thought_done:
|
| 118 |
+
thought_done = True
|
| 119 |
+
thought_cost_time = "{:.2f}".format(time.time() - start_time)
|
| 120 |
+
message_content[-2]["options"][
|
| 121 |
+
"title"] = f"End of Thought ({thought_cost_time}s)"
|
| 122 |
+
message_content[-2]["options"]["status"] = "done"
|
| 123 |
+
|
| 124 |
+
yield {chatbot: gr.update(value=chatbot_value)}
|
| 125 |
+
|
| 126 |
+
chatbot_value[-1]["footer"] = "{:.2f}".format(time.time() -
|
| 127 |
+
start_time) + 's'
|
| 128 |
+
chatbot_value[-1]["status"] = "done"
|
| 129 |
+
yield {
|
| 130 |
+
clear_btn: gr.update(disabled=False),
|
| 131 |
+
sender: gr.update(loading=False),
|
| 132 |
+
chatbot: gr.update(value=chatbot_value),
|
| 133 |
+
}
|
| 134 |
+
except Exception as e:
|
| 135 |
+
chatbot_value[-1]["loading"] = False
|
| 136 |
+
chatbot_value[-1]["status"] = "done"
|
| 137 |
+
chatbot_value[-1]["content"] = "Failed to respond, please try again."
|
| 138 |
+
yield {
|
| 139 |
+
clear_btn: gr.update(disabled=False),
|
| 140 |
+
sender: gr.update(loading=False),
|
| 141 |
+
chatbot: gr.update(value=chatbot_value),
|
| 142 |
+
}
|
| 143 |
+
raise e
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
with gr.Blocks() as demo, ms.Application(), antdx.XProvider():
|
| 147 |
+
with antd.Flex(vertical=True, gap="middle"):
|
| 148 |
+
chatbot = pro.Chatbot(
|
| 149 |
+
height=800,
|
| 150 |
+
welcome_config=ChatbotWelcomeConfig(
|
| 151 |
+
variant="borderless",
|
| 152 |
+
icon=
|
| 153 |
+
"./xinyuan.png",
|
| 154 |
+
title=f"Hello, I'm Xinyuan👋",
|
| 155 |
+
description="You can input text to get started.",
|
| 156 |
+
prompts=ChatbotPromptsConfig(
|
| 157 |
+
title="How can I help you today?",
|
| 158 |
+
styles={
|
| 159 |
+
"list": {
|
| 160 |
+
"width": '100%',
|
| 161 |
+
},
|
| 162 |
+
"item": {
|
| 163 |
+
"flex": 1,
|
| 164 |
+
},
|
| 165 |
+
},
|
| 166 |
+
items=[{
|
| 167 |
+
"label":
|
| 168 |
+
"💝 心理学与实际应用",
|
| 169 |
+
"children": [{
|
| 170 |
+
"description":
|
| 171 |
+
"课题分离是什么意思?"
|
| 172 |
+
}, {
|
| 173 |
+
"description":
|
| 174 |
+
"回避型依恋和焦虑型依恋有什么区别?还有其他依恋类型吗?"
|
| 175 |
+
}, {
|
| 176 |
+
"description":
|
| 177 |
+
"为什么我背单词的时候总是只记得开头和结尾,中间全忘了?"
|
| 178 |
+
}]
|
| 179 |
+
}, {
|
| 180 |
+
"label":
|
| 181 |
+
"👪 儿童教育与发展",
|
| 182 |
+
"children": [{
|
| 183 |
+
"description":
|
| 184 |
+
"什么是正念养育?"
|
| 185 |
+
}, {
|
| 186 |
+
"description":
|
| 187 |
+
"2岁孩子分离焦虑严重,送托育中心天天哭闹怎么办?"
|
| 188 |
+
}, {
|
| 189 |
+
"description":
|
| 190 |
+
"4岁娃说话不清还爱打人,是心理问题还是欠管教?"
|
| 191 |
+
}]
|
| 192 |
+
}])),
|
| 193 |
+
user_config=ChatbotUserConfig(
|
| 194 |
+
avatar="https://api.dicebear.com/7.x/miniavs/svg?seed=3",
|
| 195 |
+
variant="shadow"),
|
| 196 |
+
bot_config=ChatbotBotConfig(
|
| 197 |
+
header=model,
|
| 198 |
+
avatar=
|
| 199 |
+
"./xinyuan.png",
|
| 200 |
+
actions=["copy", "retry"],
|
| 201 |
+
variant="shadow"),
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
with antdx.Sender() as sender:
|
| 205 |
+
with ms.Slot("prefix"):
|
| 206 |
+
with antd.Button(value=None, color="default",
|
| 207 |
+
variant="text") as clear_btn:
|
| 208 |
+
with ms.Slot("icon"):
|
| 209 |
+
antd.Icon("ClearOutlined")
|
| 210 |
+
clear_btn.click(fn=clear, outputs=[chatbot])
|
| 211 |
+
submit_event = sender.submit(fn=submit,
|
| 212 |
+
inputs=[sender, chatbot],
|
| 213 |
+
outputs=[sender, chatbot, clear_btn])
|
| 214 |
+
sender.cancel(fn=cancel,
|
| 215 |
+
inputs=[chatbot],
|
| 216 |
+
outputs=[chatbot, sender, clear_btn],
|
| 217 |
+
cancels=[submit_event],
|
| 218 |
+
queue=False)
|
| 219 |
+
chatbot.retry(fn=retry,
|
| 220 |
+
inputs=[chatbot],
|
| 221 |
+
outputs=[sender, chatbot, clear_btn])
|
| 222 |
+
chatbot.welcome_prompt_select(fn=prompt_select, outputs=[sender])
|
| 223 |
|
| 224 |
if __name__ == "__main__":
|
| 225 |
+
demo.queue().launch()
|