Spaces:
Runtime error
Runtime error
File size: 1,882 Bytes
ecb6cb3 06ae3d3 6959a84 71d5daa efbef9f c4ad912 0eda058 c4ad912 71d5daa 166b635 0eda058 c4ad912 71d5daa c4ad912 0eda058 166b635 0eda058 ecb6cb3 1649d6c f6cbdb3 ecb6cb3 f6cbdb3 1649d6c efbef9f 71d5daa f6cbdb3 166b635 efbef9f 22c8583 166b635 71d5daa efbef9f 1649d6c 166b635 f6cbdb3 6959a84 71d5daa 6959a84 71d5daa f6cbdb3 71d5daa 6959a84 166b635 71d5daa 166b635 6959a84 71d5daa 6959a84 71d5daa 6959a84 f6cbdb3 1649d6c 06ae3d3 71d5daa 166b635 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import spaces
import torch
import gradio as gr
from fastapi import FastAPI
from gradio.routes import mount_gradio_app
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForCausalLM
MODEL = "Qwen/Qwen2.5-Coder-7B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(MODEL)
model = None
@spaces.GPU
def gerar(prompt):
global model
if model is None:
print("Carregando modelo...")
model = AutoModelForCausalLM.from_pretrained(
MODEL,
torch_dtype=torch.float16,
device_map="auto"
)
model.eval()
entrada = tokenizer(
prompt,
return_tensors="pt"
).to(model.device)
with torch.no_grad():
saida = model.generate(
**entrada,
max_new_tokens=1024,
temperature=0.2
)
texto = tokenizer.decode(
saida[0],
skip_special_tokens=True
)
return texto
class Chat(BaseModel):
model: str
messages: list
api = FastAPI()
@api.get("/status")
def status():
return {
"status": "online",
"model": MODEL
}
@api.post("/v1/chat/completions")
def completions(req: Chat):
prompt = ""
for msg in req.messages:
prompt += msg["role"] + ": "
prompt += msg["content"] + "\n"
resposta = gerar(prompt)
return {
"id": "qwen",
"object": "chat.completion",
"model": MODEL,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": resposta
},
"finish_reason": "stop"
}
]
}
demo = gr.Interface(
fn=gerar,
inputs="text",
outputs="text",
title="Qwen2.5 Coder Bridge"
)
app = mount_gradio_app(
api,
demo,
path="/"
) |