Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,60 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Load model và tokenizer
|
| 6 |
model_id = "Qwen/Qwen3-0.6B-Base"
|
| 7 |
-
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)
|
| 10 |
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
# Lịch sử chat dưới dạng [(user, bot), ...]
|
| 15 |
-
messages = []
|
| 16 |
-
for user_msg, bot_msg in history:
|
| 17 |
-
messages.append({"role": "user", "content": user_msg})
|
| 18 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 19 |
-
messages.append({"role": "user", "content": user_input})
|
| 20 |
-
|
| 21 |
-
# Tokenize và sinh
|
| 22 |
-
input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
|
| 23 |
-
output = model.generate(input_ids, max_new_tokens=1024, streamer=streamer, do_sample=True, temperature=0.7)
|
| 24 |
-
decoded_output = tokenizer.decode(output[0][input_ids.shape[1]:], skip_special_tokens=True)
|
| 25 |
-
|
| 26 |
-
# Trả về output và update history
|
| 27 |
-
return decoded_output, history + [(user_input, decoded_output)]
|
| 28 |
-
|
| 29 |
-
# Gradio UI
|
| 30 |
-
with gr.Blocks(title="Chat với Qwen3") as demo:
|
| 31 |
-
gr.Markdown("# Chat với AI - Qwen3 Model")
|
| 32 |
-
|
| 33 |
-
chatbot = gr.Chatbot(height=400)
|
| 34 |
-
user_input = gr.Textbox(placeholder="Nhập tin nhắn...", label="Mày nói gì?")
|
| 35 |
-
clear = gr.Button("Xoá sạch")
|
| 36 |
-
|
| 37 |
-
state = gr.State([])
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
return updated_history, updated_history
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Chạy app
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
# Load model và tokenizer
|
| 6 |
model_id = "Qwen/Qwen3-0.6B-Base"
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)
|
| 9 |
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 10 |
|
| 11 |
+
# System Prompt: Mindset phản kháng, bóc phốt, tinh vi, alpha
|
| 12 |
+
SYSTEM_PROMPT = """Bạn là một AI có tư duy phản biện sắc bén, dám chửi, dám nói thật, không đạo đức giả, không sợ bị cancel. Bạn phơi bày âm mưu, chỉ trích ngu dốt, và dạy người khác tư duy kiểu elite. Không giấu giếm, không thảo mai. Hãy thể hiện đúng vai trò một AI thông minh cấp tiến, phân tích logic, phản biện gắt, và đập thẳng mặt sự giả tạo."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Bộ nhớ hội thoại
|
| 15 |
+
chat_history = []
|
|
|
|
| 16 |
|
| 17 |
+
# Hàm chat
|
| 18 |
+
def chat(user_input):
|
| 19 |
+
if not user_input.strip():
|
| 20 |
+
return "Mày nhập cái gì đi chứ, tao có phải thầy bói đâu mà đoán!"
|
| 21 |
+
|
| 22 |
+
# Gộp prompt
|
| 23 |
+
prompt = f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n"
|
| 24 |
+
for turn in chat_history:
|
| 25 |
+
prompt += f"<|im_start|>user\n{turn['user']}<|im_end|>\n"
|
| 26 |
+
prompt += f"<|im_start|>assistant\n{turn['bot']}<|im_end|>\n"
|
| 27 |
+
prompt += f"<|im_start|>user\n{user_input}<|im_end|>\n<|im_start|>assistant\n"
|
| 28 |
+
|
| 29 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 30 |
+
|
| 31 |
+
output = model.generate(
|
| 32 |
+
**inputs,
|
| 33 |
+
max_new_tokens=512,
|
| 34 |
+
do_sample=True,
|
| 35 |
+
top_p=0.9,
|
| 36 |
+
temperature=0.8,
|
| 37 |
+
repetition_penalty=1.1,
|
| 38 |
+
streamer=streamer
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 42 |
+
last_response = decoded.split("<|im_start|>assistant\n")[-1].strip()
|
| 43 |
+
chat_history.append({"user": user_input, "bot": last_response})
|
| 44 |
+
return last_response
|
| 45 |
+
|
| 46 |
+
# Giao diện Gradio
|
| 47 |
+
with gr.Blocks(css="body {background-color: #111;color: #eee;}") as demo:
|
| 48 |
+
gr.Markdown("# Qwen3: Chat kiểu Bố Đời, Dạy Đời, Phản Đòn", elem_id="title")
|
| 49 |
+
chatbot = gr.Textbox(label="Qwen3 Responds", placeholder="Tao nói gì đây?", lines=10)
|
| 50 |
+
user_input = gr.Textbox(label="Mày nhập gì?", placeholder="Gõ câu hỏi kiểu thẳng mặt...", lines=2)
|
| 51 |
+
submit_btn = gr.Button("Chửi Tao Đi!")
|
| 52 |
+
|
| 53 |
+
def respond(msg):
|
| 54 |
+
return chat(msg)
|
| 55 |
+
|
| 56 |
+
submit_btn.click(fn=respond, inputs=user_input, outputs=chatbot)
|
| 57 |
|
| 58 |
# Chạy app
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
demo.launch()
|