Nhut0902 commited on
Commit
9b936dd
·
verified ·
1 Parent(s): 3b4a546

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -26
app.py CHANGED
@@ -1,12 +1,12 @@
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
 
@@ -21,9 +21,9 @@ print("Creating quantization config...")
21
 
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...")
@@ -32,21 +32,17 @@ model = AutoModelForCausalLM.from_pretrained(
32
  MODEL_NAME,
33
  quantization_config=bnb_config,
34
  device_map="auto",
35
- torch_dtype=torch.float16,
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 {
@@ -54,7 +50,6 @@ def home():
54
  "model": MODEL_NAME
55
  }
56
 
57
-
58
  @app.post("/chat")
59
  async def chat(req: ChatRequest):
60
 
@@ -81,31 +76,21 @@ async def chat(req: ChatRequest):
81
  ).to(model.device)
82
 
83
  with torch.no_grad():
84
-
85
  outputs = model.generate(
86
  **inputs,
87
  max_new_tokens=256,
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
- )
 
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
+
4
+ import torch
5
  from transformers import (
6
  AutoTokenizer,
7
  AutoModelForCausalLM,
8
  BitsAndBytesConfig
9
  )
 
 
10
 
11
  MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
12
 
 
21
 
22
  bnb_config = BitsAndBytesConfig(
23
  load_in_4bit=True,
24
+ bnb_4bit_compute_dtype=torch.float16,
25
  bnb_4bit_quant_type="nf4",
26
+ bnb_4bit_use_double_quant=True
 
27
  )
28
 
29
  print("Loading model...")
 
32
  MODEL_NAME,
33
  quantization_config=bnb_config,
34
  device_map="auto",
35
+ trust_remote_code=True,
36
+ torch_dtype=torch.float16
37
  )
38
 
 
 
39
  print("Model loaded!")
40
 
41
  app = FastAPI()
42
 
 
43
  class ChatRequest(BaseModel):
44
  message: str
45
 
 
46
  @app.get("/")
47
  def home():
48
  return {
 
50
  "model": MODEL_NAME
51
  }
52
 
 
53
  @app.post("/chat")
54
  async def chat(req: ChatRequest):
55
 
 
76
  ).to(model.device)
77
 
78
  with torch.no_grad():
 
79
  outputs = model.generate(
80
  **inputs,
81
  max_new_tokens=256,
82
  temperature=0.7,
 
83
  do_sample=True,
84
+ top_p=0.9,
85
+ repetition_penalty=1.1,
86
+ pad_token_id=tokenizer.eos_token_id
87
  )
88
 
 
 
89
  response = tokenizer.decode(
90
+ outputs[0][inputs["input_ids"].shape[1]:],
91
  skip_special_tokens=True
92
  )
93
 
94
  return {
95
  "response": response
96
+ }