Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,9 +36,9 @@ def load_model(model_key: str):
|
|
| 36 |
model_cache = {}
|
| 37 |
|
| 38 |
def respond(message: str,
|
| 39 |
-
history: List[
|
| 40 |
model_key: str,
|
| 41 |
-
system_prompt: str) -> Tuple[List[
|
| 42 |
"""Локальный чат с pipeline."""
|
| 43 |
try:
|
| 44 |
if model_key not in model_cache:
|
|
@@ -47,16 +47,11 @@ def respond(message: str,
|
|
| 47 |
|
| 48 |
print(f"🚀 Генерация: {model_key}, Msg='{message[:30]}...'")
|
| 49 |
|
| 50 |
-
#
|
| 51 |
messages = []
|
| 52 |
if system_prompt.strip():
|
| 53 |
messages.append({"role": "system", "content": system_prompt})
|
| 54 |
-
|
| 55 |
-
# Конвертируем историю из формата [["user_msg", "bot_msg"], ...]
|
| 56 |
-
for user_msg, bot_msg in history:
|
| 57 |
-
messages.append({"role": "user", "content": user_msg})
|
| 58 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 59 |
-
|
| 60 |
messages.append({"role": "user", "content": message})
|
| 61 |
|
| 62 |
# Apply chat template (для instruct)
|
|
@@ -69,26 +64,31 @@ def respond(message: str,
|
|
| 69 |
|
| 70 |
print(f"✅ Ответ: {bot_reply[:50]}...")
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
return new_history, "", gr.update(value="")
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
error_msg = f"❌ {model_key}: {str(e)}"
|
| 78 |
print(f"💥 {error_msg}")
|
| 79 |
-
new_history = history + [
|
|
|
|
|
|
|
|
|
|
| 80 |
return new_history, error_msg, gr.update(value="")
|
| 81 |
|
| 82 |
# UI
|
| 83 |
-
with gr.Blocks(title="🚀 Локальный HF Чат (на слабом CPU!)") as demo:
|
| 84 |
gr.Markdown("# Локальный Inference (без API!)\n**Маленькие модели** — 1-3 сек CPU. Большие думают ооочень долго. Нет limits/token. В качестве примера.")
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
model_dropdown = gr.Dropdown(choices=list(MODELS.keys()), value="Qwen2.5-0.5B", label="🧠 Модель")
|
| 88 |
system_prompt = gr.Textbox(label="📝 System", placeholder="Ты весёлый ИИ.", lines=2)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
chatbot = gr.Chatbot(height=500)
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
msg_input = gr.Textbox(placeholder="Привет! (Enter)", show_label=False, lines=1)
|
|
@@ -108,9 +108,9 @@ with gr.Blocks(title="🚀 Локальный HF Чат (на слабом CPU!)
|
|
| 108 |
return [], "", gr.update(value="")
|
| 109 |
clear_btn.click(clear, outputs=[chatbot, status, msg_input])
|
| 110 |
|
| 111 |
-
def retry(history):
|
| 112 |
-
if len(history) >=
|
| 113 |
-
return history[-
|
| 114 |
return ""
|
| 115 |
retry_btn.click(retry, inputs=[chatbot], outputs=[msg_input])
|
| 116 |
|
|
|
|
| 36 |
model_cache = {}
|
| 37 |
|
| 38 |
def respond(message: str,
|
| 39 |
+
history: List[Dict[str, str]],
|
| 40 |
model_key: str,
|
| 41 |
+
system_prompt: str) -> Tuple[List[Dict[str, str]], str, Dict[str, Any]]:
|
| 42 |
"""Локальный чат с pipeline."""
|
| 43 |
try:
|
| 44 |
if model_key not in model_cache:
|
|
|
|
| 47 |
|
| 48 |
print(f"🚀 Генерация: {model_key}, Msg='{message[:30]}...'")
|
| 49 |
|
| 50 |
+
# Chat format (system + history + user)
|
| 51 |
messages = []
|
| 52 |
if system_prompt.strip():
|
| 53 |
messages.append({"role": "system", "content": system_prompt})
|
| 54 |
+
messages.extend(history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
messages.append({"role": "user", "content": message})
|
| 56 |
|
| 57 |
# Apply chat template (для instruct)
|
|
|
|
| 64 |
|
| 65 |
print(f"✅ Ответ: {bot_reply[:50]}...")
|
| 66 |
|
| 67 |
+
new_history = history + [
|
| 68 |
+
{"role": "user", "content": message},
|
| 69 |
+
{"role": "assistant", "content": bot_reply}
|
| 70 |
+
]
|
| 71 |
return new_history, "", gr.update(value="")
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
error_msg = f"❌ {model_key}: {str(e)}"
|
| 75 |
print(f"💥 {error_msg}")
|
| 76 |
+
new_history = history + [
|
| 77 |
+
{"role": "user", "content": message},
|
| 78 |
+
{"role": "assistant", "content": error_msg}
|
| 79 |
+
]
|
| 80 |
return new_history, error_msg, gr.update(value="")
|
| 81 |
|
| 82 |
# UI
|
| 83 |
+
with gr.Blocks(title="🚀 Локальный HF Чат (на слабом CPU!)", theme=gr.themes.Soft()) as demo:
|
| 84 |
gr.Markdown("# Локальный Inference (без API!)\n**Маленькие модели** — 1-3 сек CPU. Большие думают ооочень долго. Нет limits/token. В качестве примера.")
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
model_dropdown = gr.Dropdown(choices=list(MODELS.keys()), value="Qwen2.5-0.5B", label="🧠 Модель")
|
| 88 |
system_prompt = gr.Textbox(label="📝 System", placeholder="Ты весёлый ИИ.", lines=2)
|
| 89 |
|
| 90 |
+
# ВЕРНУЛИ type="messages" для правильного формата
|
| 91 |
+
chatbot = gr.Chatbot(type="messages", height=500, label="Чат")
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
msg_input = gr.Textbox(placeholder="Привет! (Enter)", show_label=False, lines=1)
|
|
|
|
| 108 |
return [], "", gr.update(value="")
|
| 109 |
clear_btn.click(clear, outputs=[chatbot, status, msg_input])
|
| 110 |
|
| 111 |
+
def retry(history: List[Dict[str, str]]):
|
| 112 |
+
if len(history) >= 2 and history[-2]["role"] == "user":
|
| 113 |
+
return history[-2]["content"]
|
| 114 |
return ""
|
| 115 |
retry_btn.click(retry, inputs=[chatbot], outputs=[msg_input])
|
| 116 |
|