AnatoliiG commited on
Commit ·
c581315
1
Parent(s): 11d7c2d
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,7 +22,8 @@ try:
|
|
| 22 |
llm = Llama(
|
| 23 |
model_path=model_path,
|
| 24 |
n_ctx=CONTEXT_SIZE,
|
| 25 |
-
n_threads=
|
|
|
|
| 26 |
n_batch=512,
|
| 27 |
verbose=True,
|
| 28 |
)
|
|
@@ -78,30 +79,25 @@ async def chat_completions(request: Request):
|
|
| 78 |
|
| 79 |
|
| 80 |
def user_input(user_message, history):
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
def bot_response(history, system_prompt, temperature, max_tokens):
|
| 85 |
if not llm:
|
| 86 |
-
history
|
| 87 |
yield history
|
| 88 |
return
|
| 89 |
|
| 90 |
-
# Конвертируем историю Gradio (списки) в формат Llama (словари)
|
| 91 |
messages = [{"role": "system", "content": system_prompt}]
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
if user_msg:
|
| 98 |
-
messages.append({"role": "user", "content": user_msg})
|
| 99 |
-
if assistant_msg:
|
| 100 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
| 101 |
-
|
| 102 |
-
# Добавляем последнее сообщение пользователя
|
| 103 |
-
last_user_msg = history[-1][0]
|
| 104 |
-
messages.append({"role": "user", "content": last_user_msg})
|
| 105 |
|
| 106 |
partial_text = ""
|
| 107 |
try:
|
|
@@ -116,13 +112,12 @@ def bot_response(history, system_prompt, temperature, max_tokens):
|
|
| 116 |
delta = chunk["choices"][0]["delta"]
|
| 117 |
if "content" in delta:
|
| 118 |
partial_text += delta["content"]
|
| 119 |
-
|
| 120 |
-
history[-1][1] = partial_text
|
| 121 |
yield history
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
traceback.print_exc()
|
| 125 |
-
history[-1][
|
| 126 |
yield history
|
| 127 |
|
| 128 |
|
|
@@ -155,13 +150,10 @@ with gr.Blocks(theme=theme, css=custom_css, title="Qwen Coder Pro") as demo:
|
|
| 155 |
|
| 156 |
# Чат
|
| 157 |
with gr.Column(scale=4):
|
| 158 |
-
# ВАЖНО: Убрали type="messages", используем стандартный формат
|
| 159 |
chatbot = gr.Chatbot(
|
| 160 |
label="Conversation",
|
| 161 |
elem_id="chatbot",
|
| 162 |
-
# show_copy_button=True,
|
| 163 |
avatar_images=(None, "https://api.iconify.design/noto:robot.svg"),
|
| 164 |
-
# type="tuples",
|
| 165 |
)
|
| 166 |
|
| 167 |
msg = gr.Textbox(
|
|
@@ -176,7 +168,7 @@ with gr.Blocks(theme=theme, css=custom_css, title="Qwen Coder Pro") as demo:
|
|
| 176 |
submit_btn.click(user_input, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 177 |
bot_response, [chatbot, system_prompt, temperature, max_tokens], chatbot
|
| 178 |
)
|
| 179 |
-
clear_btn.click(lambda:
|
| 180 |
|
| 181 |
app = mount_gradio_app(app, demo, path="/")
|
| 182 |
|
|
|
|
| 22 |
llm = Llama(
|
| 23 |
model_path=model_path,
|
| 24 |
n_ctx=CONTEXT_SIZE,
|
| 25 |
+
n_threads=4, # Оптимизация для CPU Spaces
|
| 26 |
+
n_gpu_layers=0, # Явно указываем 0 для CPU
|
| 27 |
n_batch=512,
|
| 28 |
verbose=True,
|
| 29 |
)
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
def user_input(user_message, history):
|
| 82 |
+
# Gradio 5: История - это список словарей. Добавляем сообщение пользователя.
|
| 83 |
+
if history is None:
|
| 84 |
+
history = []
|
| 85 |
+
return "", history + [{"role": "user", "content": user_message}]
|
| 86 |
|
| 87 |
|
| 88 |
def bot_response(history, system_prompt, temperature, max_tokens):
|
| 89 |
if not llm:
|
| 90 |
+
history.append({"role": "assistant", "content": "Error: Model failed to load."})
|
| 91 |
yield history
|
| 92 |
return
|
| 93 |
|
|
|
|
| 94 |
messages = [{"role": "system", "content": system_prompt}]
|
| 95 |
|
| 96 |
+
relevant_history = history[-10:] if len(history) > 10 else history
|
| 97 |
+
for msg in relevant_history:
|
| 98 |
+
messages.append({"role": msg["role"], "content": msg["content"]})
|
| 99 |
|
| 100 |
+
history.append({"role": "assistant", "content": ""})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
partial_text = ""
|
| 103 |
try:
|
|
|
|
| 112 |
delta = chunk["choices"][0]["delta"]
|
| 113 |
if "content" in delta:
|
| 114 |
partial_text += delta["content"]
|
| 115 |
+
history[-1]["content"] = partial_text
|
|
|
|
| 116 |
yield history
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
traceback.print_exc()
|
| 120 |
+
history[-1]["content"] = partial_text + f"\n\n❌ **Error:** {str(e)}"
|
| 121 |
yield history
|
| 122 |
|
| 123 |
|
|
|
|
| 150 |
|
| 151 |
# Чат
|
| 152 |
with gr.Column(scale=4):
|
|
|
|
| 153 |
chatbot = gr.Chatbot(
|
| 154 |
label="Conversation",
|
| 155 |
elem_id="chatbot",
|
|
|
|
| 156 |
avatar_images=(None, "https://api.iconify.design/noto:robot.svg"),
|
|
|
|
| 157 |
)
|
| 158 |
|
| 159 |
msg = gr.Textbox(
|
|
|
|
| 168 |
submit_btn.click(user_input, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 169 |
bot_response, [chatbot, system_prompt, temperature, max_tokens], chatbot
|
| 170 |
)
|
| 171 |
+
clear_btn.click(lambda: [], None, chatbot, queue=False)
|
| 172 |
|
| 173 |
app = mount_gradio_app(app, demo, path="/")
|
| 174 |
|