Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import spaces
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
from fastapi import FastAPI
|
| 6 |
from gradio.routes import mount_gradio_app
|
| 7 |
from pydantic import BaseModel
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
@@ -11,6 +11,7 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 11 |
MODEL = "Qwen/Qwen2.5-Coder-7B-Instruct"
|
| 12 |
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
|
|
|
| 14 |
model = None
|
| 15 |
|
| 16 |
|
|
@@ -19,7 +20,7 @@ def gerar(prompt):
|
|
| 19 |
global model
|
| 20 |
|
| 21 |
if model is None:
|
| 22 |
-
print("Carregando
|
| 23 |
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
MODEL,
|
| 25 |
torch_dtype=torch.float16,
|
|
@@ -35,21 +36,28 @@ def gerar(prompt):
|
|
| 35 |
with torch.no_grad():
|
| 36 |
saida = model.generate(
|
| 37 |
**entrada,
|
| 38 |
-
max_new_tokens=1024
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
-
|
| 42 |
saida[0],
|
| 43 |
skip_special_tokens=True
|
| 44 |
)
|
| 45 |
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
class
|
| 48 |
model: str
|
| 49 |
messages: list
|
|
|
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
-
api = FastAPI(
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
@api.get("/status")
|
|
@@ -60,15 +68,38 @@ def status():
|
|
| 60 |
}
|
| 61 |
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
@api.post("/v1/chat/completions")
|
| 64 |
-
def
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
resposta = gerar(prompt)
|
| 69 |
|
| 70 |
return {
|
| 71 |
-
"id": "qwen",
|
| 72 |
"object": "chat.completion",
|
| 73 |
"model": MODEL,
|
| 74 |
"choices": [
|
|
@@ -84,13 +115,13 @@ def completions(req: Chat):
|
|
| 84 |
}
|
| 85 |
|
| 86 |
|
| 87 |
-
def
|
| 88 |
return gerar(msg)
|
| 89 |
|
| 90 |
|
| 91 |
demo = gr.ChatInterface(
|
| 92 |
-
fn=
|
| 93 |
-
title="Qwen2.5 Coder
|
| 94 |
)
|
| 95 |
|
| 96 |
|
|
@@ -98,13 +129,4 @@ app = mount_gradio_app(
|
|
| 98 |
api,
|
| 99 |
demo,
|
| 100 |
path="/"
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
if __name__ == "__main__":
|
| 105 |
-
import uvicorn
|
| 106 |
-
uvicorn.run(
|
| 107 |
-
app,
|
| 108 |
-
host="0.0.0.0",
|
| 109 |
-
port=7860
|
| 110 |
-
)
|
|
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
from fastapi import FastAPI, Request
|
| 6 |
from gradio.routes import mount_gradio_app
|
| 7 |
from pydantic import BaseModel
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 11 |
MODEL = "Qwen/Qwen2.5-Coder-7B-Instruct"
|
| 12 |
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 14 |
+
|
| 15 |
model = None
|
| 16 |
|
| 17 |
|
|
|
|
| 20 |
global model
|
| 21 |
|
| 22 |
if model is None:
|
| 23 |
+
print("Carregando modelo...")
|
| 24 |
model = AutoModelForCausalLM.from_pretrained(
|
| 25 |
MODEL,
|
| 26 |
torch_dtype=torch.float16,
|
|
|
|
| 36 |
with torch.no_grad():
|
| 37 |
saida = model.generate(
|
| 38 |
**entrada,
|
| 39 |
+
max_new_tokens=1024,
|
| 40 |
+
temperature=0.2
|
| 41 |
)
|
| 42 |
|
| 43 |
+
texto = tokenizer.decode(
|
| 44 |
saida[0],
|
| 45 |
skip_special_tokens=True
|
| 46 |
)
|
| 47 |
|
| 48 |
+
return texto
|
| 49 |
+
|
| 50 |
|
| 51 |
+
class ChatRequest(BaseModel):
|
| 52 |
model: str
|
| 53 |
messages: list
|
| 54 |
+
temperature: float | None = 0.2
|
| 55 |
+
max_tokens: int | None = 1024
|
| 56 |
|
| 57 |
|
| 58 |
+
api = FastAPI(
|
| 59 |
+
title="Qwen OpenAI Compatible API"
|
| 60 |
+
)
|
| 61 |
|
| 62 |
|
| 63 |
@api.get("/status")
|
|
|
|
| 68 |
}
|
| 69 |
|
| 70 |
|
| 71 |
+
@api.get("/v1/models")
|
| 72 |
+
def models():
|
| 73 |
+
|
| 74 |
+
return {
|
| 75 |
+
"object": "list",
|
| 76 |
+
"data": [
|
| 77 |
+
{
|
| 78 |
+
"id": MODEL,
|
| 79 |
+
"object": "model",
|
| 80 |
+
"owned_by": "local"
|
| 81 |
+
}
|
| 82 |
+
]
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
@api.post("/v1/chat/completions")
|
| 87 |
+
def chat(req: ChatRequest):
|
| 88 |
+
|
| 89 |
+
prompt = ""
|
| 90 |
|
| 91 |
+
for msg in req.messages:
|
| 92 |
+
prompt += (
|
| 93 |
+
msg["role"]
|
| 94 |
+
+ ": "
|
| 95 |
+
+ msg["content"]
|
| 96 |
+
+ "\n"
|
| 97 |
+
)
|
| 98 |
|
| 99 |
resposta = gerar(prompt)
|
| 100 |
|
| 101 |
return {
|
| 102 |
+
"id": "chatcmpl-qwen",
|
| 103 |
"object": "chat.completion",
|
| 104 |
"model": MODEL,
|
| 105 |
"choices": [
|
|
|
|
| 115 |
}
|
| 116 |
|
| 117 |
|
| 118 |
+
def interface(msg, history):
|
| 119 |
return gerar(msg)
|
| 120 |
|
| 121 |
|
| 122 |
demo = gr.ChatInterface(
|
| 123 |
+
fn=interface,
|
| 124 |
+
title="Qwen2.5 Coder Remote AI"
|
| 125 |
)
|
| 126 |
|
| 127 |
|
|
|
|
| 129 |
api,
|
| 130 |
demo,
|
| 131 |
path="/"
|
| 132 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|