Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
|
| 2 |
-
import
|
| 3 |
import spaces
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from pydantic import BaseModel
|
| 8 |
from typing import Optional
|
| 9 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 10 |
|
| 11 |
MODEL_ID = "Qwen/Qwen2.5-3B-Instruct"
|
| 12 |
|
|
@@ -14,46 +17,24 @@ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
|
| 14 |
|
| 15 |
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
MODEL_ID,
|
| 17 |
-
|
| 18 |
device_map="auto"
|
| 19 |
)
|
| 20 |
|
| 21 |
-
app = FastAPI(title="Qwen OpenAI-Compatible API")
|
| 22 |
-
|
| 23 |
-
app.add_middleware(
|
| 24 |
-
CORSMiddleware,
|
| 25 |
-
allow_origins=["*"],
|
| 26 |
-
allow_methods=["*"],
|
| 27 |
-
allow_headers=["*"]
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
class Message(BaseModel):
|
| 31 |
-
role: str
|
| 32 |
-
content: str
|
| 33 |
-
|
| 34 |
-
class ChatRequest(BaseModel):
|
| 35 |
-
model: Optional[str] = MODEL_ID
|
| 36 |
-
messages: list[Message]
|
| 37 |
-
temperature: Optional[float] = 0.7
|
| 38 |
-
max_tokens: Optional[int] = 512
|
| 39 |
-
stream: Optional[bool] = False
|
| 40 |
-
|
| 41 |
@spaces.GPU
|
| 42 |
-
def
|
| 43 |
-
chat_messages = [
|
| 44 |
-
{"role": m.role, "content": m.content}
|
| 45 |
-
for m in messages
|
| 46 |
-
]
|
| 47 |
-
|
| 48 |
prompt = tokenizer.apply_chat_template(
|
| 49 |
-
|
| 50 |
tokenize=False,
|
| 51 |
add_generation_prompt=True
|
| 52 |
)
|
| 53 |
|
| 54 |
-
inputs = tokenizer(
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
with torch.
|
| 57 |
output = model.generate(
|
| 58 |
**inputs,
|
| 59 |
max_new_tokens=max_tokens,
|
|
@@ -62,54 +43,112 @@ def generate(messages, temperature, max_tokens):
|
|
| 62 |
pad_token_id=tokenizer.eos_token_id
|
| 63 |
)
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
return tokenizer.decode(generated, skip_special_tokens=True)
|
| 68 |
|
| 69 |
@app.get("/")
|
| 70 |
def home():
|
| 71 |
return {
|
| 72 |
"status": "online",
|
| 73 |
-
"model": MODEL_ID
|
| 74 |
-
"api": "/v1/chat/completions"
|
| 75 |
}
|
| 76 |
|
|
|
|
| 77 |
@app.get("/v1/models")
|
| 78 |
def models():
|
| 79 |
return {
|
| 80 |
"object": "list",
|
| 81 |
-
"data": [
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
}
|
| 87 |
-
]
|
| 88 |
}
|
| 89 |
|
|
|
|
| 90 |
@app.post("/v1/chat/completions")
|
| 91 |
-
def
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
return {
|
| 99 |
-
"id": "chatcmpl-
|
| 100 |
"object": "chat.completion",
|
| 101 |
-
"created":
|
| 102 |
"model": MODEL_ID,
|
| 103 |
-
"choices": [
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
"
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
}
|
| 112 |
-
],
|
| 113 |
"usage": {
|
| 114 |
"prompt_tokens": 0,
|
| 115 |
"completion_tokens": 0,
|
|
@@ -117,5 +156,11 @@ def chat_completions(request: ChatRequest):
|
|
| 117 |
}
|
| 118 |
}
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
if __name__ == "__main__":
|
|
|
|
| 121 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
|
| 2 |
+
import gradio as gr
|
| 3 |
import spaces
|
| 4 |
+
import torch
|
| 5 |
+
import json
|
| 6 |
+
import time
|
| 7 |
+
import uuid
|
| 8 |
+
|
| 9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 10 |
+
from fastapi import FastAPI, HTTPException
|
| 11 |
from pydantic import BaseModel
|
| 12 |
from typing import Optional
|
|
|
|
| 13 |
|
| 14 |
MODEL_ID = "Qwen/Qwen2.5-3B-Instruct"
|
| 15 |
|
|
|
|
| 17 |
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
MODEL_ID,
|
| 20 |
+
dtype=torch.bfloat16,
|
| 21 |
device_map="auto"
|
| 22 |
)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
@spaces.GPU
|
| 25 |
+
def inference(messages, max_tokens=512, temperature=0.7):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
prompt = tokenizer.apply_chat_template(
|
| 27 |
+
messages,
|
| 28 |
tokenize=False,
|
| 29 |
add_generation_prompt=True
|
| 30 |
)
|
| 31 |
|
| 32 |
+
inputs = tokenizer(
|
| 33 |
+
prompt,
|
| 34 |
+
return_tensors="pt"
|
| 35 |
+
).to(model.device)
|
| 36 |
|
| 37 |
+
with torch.inference_mode():
|
| 38 |
output = model.generate(
|
| 39 |
**inputs,
|
| 40 |
max_new_tokens=max_tokens,
|
|
|
|
| 43 |
pad_token_id=tokenizer.eos_token_id
|
| 44 |
)
|
| 45 |
|
| 46 |
+
result = output[0][inputs.input_ids.shape[-1]:]
|
| 47 |
+
|
| 48 |
+
return tokenizer.decode(
|
| 49 |
+
result,
|
| 50 |
+
skip_special_tokens=True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Gradio GPU endpoint
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
|
| 57 |
+
gr.api(
|
| 58 |
+
inference,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.JSON(label="Messages"),
|
| 61 |
+
gr.Slider(1, 4096, value=512),
|
| 62 |
+
gr.Slider(0, 2, value=0.7)
|
| 63 |
+
],
|
| 64 |
+
outputs=gr.Textbox(),
|
| 65 |
+
api_name="inference"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# FastAPI application
|
| 70 |
+
app = FastAPI(title="Qwen OpenAI API")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class ChatRequest(BaseModel):
|
| 74 |
+
model: Optional[str] = MODEL_ID
|
| 75 |
+
messages: list[dict]
|
| 76 |
+
temperature: Optional[float] = 0.7
|
| 77 |
+
max_tokens: Optional[int] = 512
|
| 78 |
+
stream: Optional[bool] = False
|
| 79 |
|
|
|
|
| 80 |
|
| 81 |
@app.get("/")
|
| 82 |
def home():
|
| 83 |
return {
|
| 84 |
"status": "online",
|
| 85 |
+
"model": MODEL_ID
|
|
|
|
| 86 |
}
|
| 87 |
|
| 88 |
+
|
| 89 |
@app.get("/v1/models")
|
| 90 |
def models():
|
| 91 |
return {
|
| 92 |
"object": "list",
|
| 93 |
+
"data": [{
|
| 94 |
+
"id": MODEL_ID,
|
| 95 |
+
"object": "model",
|
| 96 |
+
"owned_by": "local"
|
| 97 |
+
}]
|
|
|
|
|
|
|
| 98 |
}
|
| 99 |
|
| 100 |
+
|
| 101 |
@app.post("/v1/chat/completions")
|
| 102 |
+
async def chat(request: ChatRequest):
|
| 103 |
+
|
| 104 |
+
if request.stream:
|
| 105 |
+
raise HTTPException(
|
| 106 |
+
status_code=400,
|
| 107 |
+
detail="Streaming is not supported yet."
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
if not request.messages:
|
| 111 |
+
raise HTTPException(
|
| 112 |
+
status_code=400,
|
| 113 |
+
detail="Messages cannot be empty."
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
messages = [
|
| 117 |
+
{
|
| 118 |
+
"role": m["role"],
|
| 119 |
+
"content": m["content"]
|
| 120 |
+
}
|
| 121 |
+
for m in request.messages
|
| 122 |
+
]
|
| 123 |
+
|
| 124 |
+
try:
|
| 125 |
+
result = await demo.call_api(
|
| 126 |
+
"inference",
|
| 127 |
+
api_input=[
|
| 128 |
+
messages,
|
| 129 |
+
min(max(request.max_tokens or 512, 1), 4096),
|
| 130 |
+
min(max(request.temperature or 0.7, 0), 2)
|
| 131 |
+
]
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
content = result.result
|
| 135 |
+
|
| 136 |
+
except Exception as e:
|
| 137 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 138 |
|
| 139 |
return {
|
| 140 |
+
"id": "chatcmpl-" + uuid.uuid4().hex,
|
| 141 |
"object": "chat.completion",
|
| 142 |
+
"created": int(time.time()),
|
| 143 |
"model": MODEL_ID,
|
| 144 |
+
"choices": [{
|
| 145 |
+
"index": 0,
|
| 146 |
+
"message": {
|
| 147 |
+
"role": "assistant",
|
| 148 |
+
"content": content
|
| 149 |
+
},
|
| 150 |
+
"finish_reason": "stop"
|
| 151 |
+
}],
|
|
|
|
|
|
|
| 152 |
"usage": {
|
| 153 |
"prompt_tokens": 0,
|
| 154 |
"completion_tokens": 0,
|
|
|
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
+
|
| 160 |
+
# Mount Gradio onto FastAPI
|
| 161 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 162 |
+
|
| 163 |
+
|
| 164 |
if __name__ == "__main__":
|
| 165 |
+
import uvicorn
|
| 166 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|