Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
from transformers import (
|
| 5 |
AutoTokenizer,
|
| 6 |
AutoModelForCausalLM,
|
| 7 |
BitsAndBytesConfig
|
| 8 |
)
|
|
|
|
|
|
|
| 9 |
|
| 10 |
MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 11 |
|
|
@@ -21,8 +22,8 @@ print("Creating quantization config...")
|
|
| 21 |
bnb_config = BitsAndBytesConfig(
|
| 22 |
load_in_4bit=True,
|
| 23 |
bnb_4bit_quant_type="nf4",
|
| 24 |
-
|
| 25 |
-
|
| 26 |
)
|
| 27 |
|
| 28 |
print("Loading model...")
|
|
@@ -35,30 +36,39 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 35 |
trust_remote_code=True
|
| 36 |
)
|
| 37 |
|
|
|
|
|
|
|
| 38 |
print("Model loaded!")
|
| 39 |
|
| 40 |
-
|
| 41 |
-
You are a helpful AI assistant.
|
| 42 |
-
Answer clearly and shortly.
|
| 43 |
-
"""
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
messages = [
|
| 48 |
{
|
| 49 |
"role": "system",
|
| 50 |
-
"content":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
}
|
| 52 |
]
|
| 53 |
|
| 54 |
-
if history:
|
| 55 |
-
messages.extend(history)
|
| 56 |
-
|
| 57 |
-
messages.append({
|
| 58 |
-
"role": "user",
|
| 59 |
-
"content": message
|
| 60 |
-
})
|
| 61 |
-
|
| 62 |
text = tokenizer.apply_chat_template(
|
| 63 |
messages,
|
| 64 |
tokenize=False,
|
|
@@ -68,12 +78,7 @@ def generate_response(message, history):
|
|
| 68 |
inputs = tokenizer(
|
| 69 |
text,
|
| 70 |
return_tensors="pt"
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
inputs = {
|
| 74 |
-
k: v.to(model.device)
|
| 75 |
-
for k, v in inputs.items()
|
| 76 |
-
}
|
| 77 |
|
| 78 |
with torch.no_grad():
|
| 79 |
|
|
@@ -83,54 +88,24 @@ def generate_response(message, history):
|
|
| 83 |
temperature=0.7,
|
| 84 |
top_p=0.9,
|
| 85 |
do_sample=True,
|
| 86 |
-
repetition_penalty=1.1
|
| 87 |
-
pad_token_id=tokenizer.eos_token_id
|
| 88 |
)
|
| 89 |
|
|
|
|
|
|
|
| 90 |
response = tokenizer.decode(
|
| 91 |
-
|
| 92 |
skip_special_tokens=True
|
| 93 |
)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
"
|
| 97 |
-
|
| 98 |
-
})
|
| 99 |
-
|
| 100 |
-
history.append({
|
| 101 |
-
"role": "assistant",
|
| 102 |
-
"content": response
|
| 103 |
-
})
|
| 104 |
-
|
| 105 |
-
return history, history
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
with gr.Blocks(
|
| 109 |
-
theme=gr.themes.Soft()
|
| 110 |
-
) as demo:
|
| 111 |
-
|
| 112 |
-
gr.Markdown("# 🤖 Nhut AI")
|
| 113 |
-
gr.Markdown("Qwen2.5-1.5B-Instruct 4bit")
|
| 114 |
-
|
| 115 |
-
chatbot = gr.Chatbot(
|
| 116 |
-
type="messages",
|
| 117 |
-
height=500
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
msg = gr.Textbox(
|
| 121 |
-
placeholder="Nhập tin nhắn...",
|
| 122 |
-
container=True
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
state = gr.State([])
|
| 126 |
|
| 127 |
-
msg.submit(
|
| 128 |
-
generate_response,
|
| 129 |
-
[msg, state],
|
| 130 |
-
[chatbot, state]
|
| 131 |
-
)
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
|
|
|
| 3 |
from transformers import (
|
| 4 |
AutoTokenizer,
|
| 5 |
AutoModelForCausalLM,
|
| 6 |
BitsAndBytesConfig
|
| 7 |
)
|
| 8 |
+
import torch
|
| 9 |
+
import uvicorn
|
| 10 |
|
| 11 |
MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 12 |
|
|
|
|
| 22 |
bnb_config = BitsAndBytesConfig(
|
| 23 |
load_in_4bit=True,
|
| 24 |
bnb_4bit_quant_type="nf4",
|
| 25 |
+
bnb_4bit_use_double_quant=True,
|
| 26 |
+
bnb_4bit_compute_dtype=torch.float16
|
| 27 |
)
|
| 28 |
|
| 29 |
print("Loading model...")
|
|
|
|
| 36 |
trust_remote_code=True
|
| 37 |
)
|
| 38 |
|
| 39 |
+
model.eval()
|
| 40 |
+
|
| 41 |
print("Model loaded!")
|
| 42 |
|
| 43 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
|
| 46 |
+
class ChatRequest(BaseModel):
|
| 47 |
+
message: str
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
@app.get("/")
|
| 51 |
+
def home():
|
| 52 |
+
return {
|
| 53 |
+
"status": "running",
|
| 54 |
+
"model": MODEL_NAME
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
@app.post("/chat")
|
| 59 |
+
async def chat(req: ChatRequest):
|
| 60 |
|
| 61 |
messages = [
|
| 62 |
{
|
| 63 |
"role": "system",
|
| 64 |
+
"content": "Bạn là AI assistant hữu ích."
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"role": "user",
|
| 68 |
+
"content": req.message
|
| 69 |
}
|
| 70 |
]
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
text = tokenizer.apply_chat_template(
|
| 73 |
messages,
|
| 74 |
tokenize=False,
|
|
|
|
| 78 |
inputs = tokenizer(
|
| 79 |
text,
|
| 80 |
return_tensors="pt"
|
| 81 |
+
).to(model.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
with torch.no_grad():
|
| 84 |
|
|
|
|
| 88 |
temperature=0.7,
|
| 89 |
top_p=0.9,
|
| 90 |
do_sample=True,
|
| 91 |
+
repetition_penalty=1.1
|
|
|
|
| 92 |
)
|
| 93 |
|
| 94 |
+
generated_ids = outputs[0][inputs["input_ids"].shape[1]:]
|
| 95 |
+
|
| 96 |
response = tokenizer.decode(
|
| 97 |
+
generated_ids,
|
| 98 |
skip_special_tokens=True
|
| 99 |
)
|
| 100 |
|
| 101 |
+
return {
|
| 102 |
+
"response": response
|
| 103 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
uvicorn.run(
|
| 108 |
+
"app:app",
|
| 109 |
+
host="0.0.0.0",
|
| 110 |
+
port=7860
|
| 111 |
+
)
|