Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,136 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
return response.json().get("text", "Không có phản hồi từ server.")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
+
# Cấu hình
|
| 8 |
+
LLAMA_SERVER_URL = "http://localhost:3000/completion" # llama-server chạy trên cổng 3000
|
|
|
|
| 9 |
|
| 10 |
+
# Hàm gọi API llama-server
|
| 11 |
+
def call_llama_server(messages, max_length=50):
|
| 12 |
+
payload = {
|
| 13 |
+
"prompt": messages[-1]["content"], # Chỉ gửi prompt cuối
|
| 14 |
+
"n_predict": max_length
|
| 15 |
+
}
|
| 16 |
+
try:
|
| 17 |
+
response = requests.post(LLAMA_SERVER_URL, json=payload, timeout=10)
|
| 18 |
+
response.raise_for_status()
|
| 19 |
+
return response.json().get("content", "No response")
|
| 20 |
+
except requests.RequestException as e:
|
| 21 |
+
return f"Error: {str(e)}"
|
| 22 |
|
| 23 |
+
# Định dạng lịch sử cho giao diện
|
| 24 |
+
def format_history(history):
|
| 25 |
+
return [{"role": "user", "content": item["content"]} if item["role"] == "user"
|
| 26 |
+
else {"role": "assistant", "content": item["content"]}
|
| 27 |
+
for item in history if item["role"] != "placeholder"]
|
| 28 |
+
|
| 29 |
+
# Giao diện chính
|
| 30 |
+
with gr.Blocks(
|
| 31 |
+
title="LLaMA Chat",
|
| 32 |
+
theme=gr.themes.Soft(primary_hue="purple", secondary_hue="gray"),
|
| 33 |
+
css="""
|
| 34 |
+
.chatbot { height: calc(100vh - 100px); overflow-y: auto; }
|
| 35 |
+
.message-user { background-color: #e6f3ff; padding: 10px; border-radius: 5px; margin: 5px 0; }
|
| 36 |
+
.message-assistant { background-color: #f0f0f0; padding: 10px; border-radius: 5px; margin: 5px 0; }
|
| 37 |
+
"""
|
| 38 |
+
) as demo:
|
| 39 |
+
# Trạng thái lưu lịch sử
|
| 40 |
+
state = gr.State({
|
| 41 |
+
"conversations_history": {},
|
| 42 |
+
"conversations": [],
|
| 43 |
+
"conversation_id": ""
|
| 44 |
+
})
|
| 45 |
+
|
| 46 |
+
gr.Markdown("# LLaMA Chat\nChat với mô hình Qwen2.5-0.5B - Powered by llama.cpp")
|
| 47 |
+
|
| 48 |
+
with gr.Row():
|
| 49 |
+
# Cột trái: Danh sách hội thoại
|
| 50 |
+
with gr.Column(scale=1, min_width=200):
|
| 51 |
+
gr.Markdown("### Hội thoại")
|
| 52 |
+
conversations = gr.Dropdown(label="Chọn hội thoại", choices=[], value="")
|
| 53 |
+
new_chat_btn = gr.Button("Tạo hội thoại mới", variant="primary")
|
| 54 |
+
clear_btn = gr.Button("Xóa lịch sử", variant="secondary")
|
| 55 |
+
|
| 56 |
+
# Cột phải: Chatbot
|
| 57 |
+
with gr.Column(scale=3):
|
| 58 |
+
chatbot = gr.Chatbot(label="Cuộc trò chuyện", elem_classes="chatbot")
|
| 59 |
+
with gr.Row():
|
| 60 |
+
prompt_input = gr.Textbox(
|
| 61 |
+
label="Nhập tin nhắn",
|
| 62 |
+
placeholder="Gõ tin nhắn hoặc '/' để xem gợi ý...",
|
| 63 |
+
show_label=False,
|
| 64 |
+
container=False
|
| 65 |
+
)
|
| 66 |
+
submit_btn = gr.Button("Gửi", variant="primary")
|
| 67 |
+
|
| 68 |
+
# Hàm xử lý sự kiện
|
| 69 |
+
def submit_prompt(prompt, state):
|
| 70 |
+
if not prompt.strip():
|
| 71 |
+
return state, [], ""
|
| 72 |
+
|
| 73 |
+
# Tạo hội thoại mới nếu chưa có
|
| 74 |
+
if not state["conversation_id"]:
|
| 75 |
+
convo_id = str(uuid.uuid4())
|
| 76 |
+
state["conversation_id"] = convo_id
|
| 77 |
+
state["conversations_history"][convo_id] = []
|
| 78 |
+
state["conversations"].append({"label": prompt[:20] + "...", "value": convo_id})
|
| 79 |
+
|
| 80 |
+
history = state["conversations_history"][state["conversation_id"]]
|
| 81 |
+
history.append({"role": "user", "content": prompt, "key": str(uuid.uuid4())})
|
| 82 |
+
|
| 83 |
+
# Gọi llama-server
|
| 84 |
+
response = call_llama_server(format_history(history))
|
| 85 |
+
history.append({"role": "assistant", "content": response, "key": str(uuid.uuid4())})
|
| 86 |
+
|
| 87 |
+
return (
|
| 88 |
+
state,
|
| 89 |
+
[(item["content"], None) if item["role"] == "user" else (None, item["content"])
|
| 90 |
+
for item in history],
|
| 91 |
+
""
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
def new_chat(state):
|
| 95 |
+
state["conversation_id"] = ""
|
| 96 |
+
return state, [], gr.update(choices=[(c["label"], c["value"]) for c in state["conversations"]])
|
| 97 |
+
|
| 98 |
+
def select_conversation(state, convo_id):
|
| 99 |
+
if convo_id and convo_id in state["conversations_history"]:
|
| 100 |
+
state["conversation_id"] = convo_id
|
| 101 |
+
history = state["conversations_history"][convo_id]
|
| 102 |
+
return (
|
| 103 |
+
state,
|
| 104 |
+
[(item["content"], None) if item["role"] == "user" else (None, item["content"])
|
| 105 |
+
for item in history]
|
| 106 |
+
)
|
| 107 |
+
return state, []
|
| 108 |
+
|
| 109 |
+
def clear_history(state):
|
| 110 |
+
if state["conversation_id"]:
|
| 111 |
+
state["conversations_history"][state["conversation_id"]] = []
|
| 112 |
+
return state, []
|
| 113 |
+
|
| 114 |
+
# Sự kiện
|
| 115 |
+
submit_btn.click(
|
| 116 |
+
fn=submit_prompt,
|
| 117 |
+
inputs=[prompt_input, state],
|
| 118 |
+
outputs=[state, chatbot, prompt_input]
|
| 119 |
+
)
|
| 120 |
+
new_chat_btn.click(
|
| 121 |
+
fn=new_chat,
|
| 122 |
+
inputs=[state],
|
| 123 |
+
outputs=[state, chatbot, conversations]
|
| 124 |
+
)
|
| 125 |
+
conversations.change(
|
| 126 |
+
fn=select_conversation,
|
| 127 |
+
inputs=[state, conversations],
|
| 128 |
+
outputs=[state, chatbot]
|
| 129 |
+
)
|
| 130 |
+
clear_btn.click(
|
| 131 |
+
fn=clear_history,
|
| 132 |
+
inputs=[state],
|
| 133 |
+
outputs=[state, chatbot]
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
demo.launch(server_name="0.0.0.0", server_port=3000)
|