Erinaldorodrigues commited on
Commit
0eda058
·
verified ·
1 Parent(s): ccb7d01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -6,55 +6,55 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
6
 
7
  app = FastAPI()
8
 
9
- modelo_id = "Qwen/Qwen2.5-Coder-7B-Instruct"
10
 
11
- tokenizer = AutoTokenizer.from_pretrained(modelo_id)
12
 
13
- modelo = None
14
 
15
 
16
- class Pedido(BaseModel):
17
  model: str
18
  messages: list
19
 
20
 
21
  @spaces.GPU
22
- def gerar(prompt):
23
 
24
- global modelo
25
 
26
- if modelo is None:
27
- modelo = AutoModelForCausalLM.from_pretrained(
28
- modelo_id,
29
  torch_dtype=torch.float16,
30
  device_map="auto"
31
  )
32
 
33
- entrada = tokenizer(
34
  prompt,
35
  return_tensors="pt"
36
- ).to(modelo.device)
37
 
38
- saida = modelo.generate(
39
- **entrada,
40
  max_new_tokens=1024
41
  )
42
 
43
  return tokenizer.decode(
44
- saida[0],
45
  skip_special_tokens=True
46
  )
47
 
48
 
49
  @app.post("/v1/chat/completions")
50
- def chat(req: Pedido):
51
 
52
- texto = req.messages[-1]["content"]
53
 
54
- resposta = gerar(texto)
55
 
56
  return {
57
- "id": "qwen-local",
58
  "object": "chat.completion",
59
  "choices": [
60
  {
 
6
 
7
  app = FastAPI()
8
 
9
+ MODEL = "Qwen/Qwen2.5-Coder-7B-Instruct"
10
 
11
+ tokenizer = AutoTokenizer.from_pretrained(MODEL)
12
 
13
+ model = None
14
 
15
 
16
+ class Chat(BaseModel):
17
  model: str
18
  messages: list
19
 
20
 
21
  @spaces.GPU
22
+ def run_ai(prompt):
23
 
24
+ global model
25
 
26
+ if model is None:
27
+ model = AutoModelForCausalLM.from_pretrained(
28
+ MODEL,
29
  torch_dtype=torch.float16,
30
  device_map="auto"
31
  )
32
 
33
+ inputs = tokenizer(
34
  prompt,
35
  return_tensors="pt"
36
+ ).to(model.device)
37
 
38
+ output = model.generate(
39
+ **inputs,
40
  max_new_tokens=1024
41
  )
42
 
43
  return tokenizer.decode(
44
+ output[0],
45
  skip_special_tokens=True
46
  )
47
 
48
 
49
  @app.post("/v1/chat/completions")
50
+ def completions(req: Chat):
51
 
52
+ prompt = req.messages[-1]["content"]
53
 
54
+ resposta = run_ai(prompt)
55
 
56
  return {
57
+ "id": "qwen-coder",
58
  "object": "chat.completion",
59
  "choices": [
60
  {