Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
modelo_id = "Qwen/Qwen2.5-Coder-7B-Instruct"
|
| 7 |
|
|
@@ -10,8 +13,14 @@ tokenizer = AutoTokenizer.from_pretrained(modelo_id)
|
|
| 10 |
modelo = None
|
| 11 |
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
@spaces.GPU
|
| 14 |
-
def
|
|
|
|
| 15 |
global modelo
|
| 16 |
|
| 17 |
if modelo is None:
|
|
@@ -21,37 +30,38 @@ def gerar_codigo(prompt):
|
|
| 21 |
device_map="auto"
|
| 22 |
)
|
| 23 |
|
| 24 |
-
|
| 25 |
prompt,
|
| 26 |
return_tensors="pt"
|
| 27 |
).to(modelo.device)
|
| 28 |
|
| 29 |
saida = modelo.generate(
|
| 30 |
-
**
|
| 31 |
-
max_new_tokens=
|
| 32 |
)
|
| 33 |
|
| 34 |
-
|
| 35 |
saida[0],
|
| 36 |
skip_special_tokens=True
|
| 37 |
)
|
| 38 |
|
| 39 |
-
return texto
|
| 40 |
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
fn=gerar_codigo,
|
| 44 |
-
inputs=gr.Textbox(
|
| 45 |
-
label="Pedido"
|
| 46 |
-
),
|
| 47 |
-
outputs=gr.Textbox(
|
| 48 |
-
label="Resposta"
|
| 49 |
-
),
|
| 50 |
-
title="Qwen2.5 Coder Agente"
|
| 51 |
-
)
|
| 52 |
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import spaces
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
|
| 9 |
modelo_id = "Qwen/Qwen2.5-Coder-7B-Instruct"
|
| 10 |
|
|
|
|
| 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:
|
|
|
|
| 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 |
+
{
|
| 61 |
+
"message": {
|
| 62 |
+
"role": "assistant",
|
| 63 |
+
"content": resposta
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
]
|
| 67 |
+
}
|