Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,55 +6,55 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
|
| 10 |
|
| 11 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
|
| 16 |
-
class
|
| 17 |
model: str
|
| 18 |
messages: list
|
| 19 |
|
| 20 |
|
| 21 |
@spaces.GPU
|
| 22 |
-
def
|
| 23 |
|
| 24 |
-
global
|
| 25 |
|
| 26 |
-
if
|
| 27 |
-
|
| 28 |
-
|
| 29 |
torch_dtype=torch.float16,
|
| 30 |
device_map="auto"
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
| 34 |
prompt,
|
| 35 |
return_tensors="pt"
|
| 36 |
-
).to(
|
| 37 |
|
| 38 |
-
|
| 39 |
-
**
|
| 40 |
max_new_tokens=1024
|
| 41 |
)
|
| 42 |
|
| 43 |
return tokenizer.decode(
|
| 44 |
-
|
| 45 |
skip_special_tokens=True
|
| 46 |
)
|
| 47 |
|
| 48 |
|
| 49 |
@app.post("/v1/chat/completions")
|
| 50 |
-
def
|
| 51 |
|
| 52 |
-
|
| 53 |
|
| 54 |
-
resposta =
|
| 55 |
|
| 56 |
return {
|
| 57 |
-
"id": "qwen-
|
| 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 |
{
|