Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,54 +4,61 @@ import sys
|
|
4 |
# Cài đặt các thư viện nếu chưa có
|
5 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "transformers", "streamlit", "torch", "bitsandbytes","peft"])
|
6 |
|
7 |
-
import streamlit as st
|
8 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
|
9 |
-
from peft import PeftModel
|
10 |
import torch
|
|
|
|
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
ADAPTER_MODEL_PATH = "lora_model"
|
15 |
|
16 |
-
# Load
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_NAME)
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
with torch.no_grad():
|
27 |
-
model.generate(**inputs, streamer=streamer, max_length=512)
|
28 |
-
return ""
|
29 |
|
30 |
-
#
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
-
st.markdown(message["content"])
|
42 |
|
43 |
-
|
44 |
-
user_input = st.chat_input("Nhập tin nhắn...")
|
45 |
if user_input:
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
# Generate response
|
52 |
-
with st.chat_message("assistant"):
|
53 |
-
response = generate_response(user_input)
|
54 |
-
st.markdown(response)
|
55 |
-
|
56 |
-
# Append assistant response
|
57 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
4 |
# Cài đặt các thư viện nếu chưa có
|
5 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "transformers", "streamlit", "torch", "bitsandbytes","peft"])
|
6 |
|
|
|
|
|
|
|
7 |
import torch
|
8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
9 |
+
from peft import PeftModel
|
10 |
+
import streamlit as st
|
11 |
|
12 |
+
# 1. Cấu hình và load model gốc
|
13 |
+
base_model_name = r"lora_model" # model gốc mà adapter được áp dụng
|
|
|
14 |
|
15 |
+
# Load tokenizer và model gốc (chạy trên GPU nếu có sẵn)
|
16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
18 |
+
base_model = AutoModelForCausalLM.from_pretrained(base_model_name, device_map="auto")
|
|
|
19 |
|
20 |
+
# 2. Tải adapter LoRA từ thư mục "lora_model"
|
21 |
+
# Thư mục này chứa các file được lưu bởi model.save_pretrained("lora_model")
|
22 |
+
model = PeftModel.from_pretrained(base_model, base_model_name)
|
23 |
+
model.to(device)
|
|
|
|
|
|
|
24 |
|
25 |
+
# 3. Định nghĩa prompt template cho chatbot (có thể tùy chỉnh)
|
26 |
+
prompt_template = """Bạn là một trợ lý ảo thông minh, đóng vai một giáo viên giàu kinh nghiệm, tận tâm và nhiệt huyết.
|
27 |
+
Nhiệm vụ của bạn là giải đáp mọi thắc mắc của người dùng một cách rõ ràng, chi tiết và dễ hiểu.
|
28 |
+
Nếu người dùng chưa gửi bất kỳ tin nhắn nào, hãy hỏi: “Bạn cần giúp gì?” hoặc “Tôi có thể hỗ trợ bạn điều gì?”
|
29 |
+
|
30 |
+
### Đầu vào:
|
31 |
+
{}
|
32 |
+
### Trả lời:
|
33 |
+
"""
|
34 |
|
35 |
+
def generate_response(user_input):
|
36 |
+
# Tạo prompt dựa trên input của người dùng
|
37 |
+
prompt = prompt_template.format(user_input)
|
38 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
39 |
+
|
40 |
+
# Sinh kết quả với sampling (có thể điều chỉnh temperature, max_new_tokens,...)
|
41 |
+
outputs = model.generate(
|
42 |
+
**inputs,
|
43 |
+
|
44 |
+
do_sample=True,
|
45 |
+
temperature=0.7,
|
46 |
+
pad_token_id=tokenizer.eos_token_id
|
47 |
+
)
|
48 |
+
# Decode kết quả và loại bỏ token đặc biệt
|
49 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
50 |
+
# Nếu cần, bạn có thể xử lý để cắt bỏ phần prompt ban đầu
|
51 |
+
# Ví dụ: trả về phần text sau prompt_template
|
52 |
+
return response.replace(prompt, "").strip()
|
53 |
|
54 |
+
# Streamlit UI
|
55 |
+
st.title("Chatbot Trợ Lý Ảo")
|
56 |
+
st.write("Chào mừng đến với Chatbot! Nhập 'exit' để thoát.")
|
|
|
57 |
|
58 |
+
user_input = st.text_input("Bạn: ", "")
|
|
|
59 |
if user_input:
|
60 |
+
if user_input.lower() == "exit":
|
61 |
+
st.write("Chatbot: Tạm biệt!")
|
62 |
+
else:
|
63 |
+
reply = generate_response(user_input)
|
64 |
+
st.write(f"Chatbot: {reply}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|