Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,121 @@
|
|
| 1 |
-
|
| 2 |
import torch
|
| 3 |
import spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
-
|
| 12 |
-
torch_dtype=torch.bfloat16,
|
| 13 |
device_map="auto"
|
| 14 |
)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
@spaces.GPU
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"role": "system",
|
| 22 |
-
"content": "You are a JSON generator. You MUST respond strictly with valid JSON. Do not include markdown code block backticks (like ```json), commentary, or extra text."
|
| 23 |
-
},
|
| 24 |
-
{"role": "user", "content": user_prompt}
|
| 25 |
]
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
**model_inputs,
|
| 32 |
-
max_new_tokens=max_new_tokens,
|
| 33 |
-
temperature=temperature # Lower temperature (0.1–0.3) keeps JSON syntax consistent
|
| 34 |
)
|
| 35 |
-
|
| 36 |
-
generated_ids = [
|
| 37 |
-
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
| 38 |
-
]
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
-
|
|
|
|
| 1 |
+
|
| 2 |
import torch
|
| 3 |
import spaces
|
| 4 |
+
import uvicorn
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 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 |
+
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 14 |
|
|
|
|
| 15 |
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
+
MODEL_ID,
|
| 17 |
+
torch_dtype=torch.bfloat16,
|
| 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 generate(messages, temperature, max_tokens):
|
| 43 |
+
chat_messages = [
|
| 44 |
+
{"role": m.role, "content": m.content}
|
| 45 |
+
for m in messages
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
]
|
| 47 |
+
|
| 48 |
+
prompt = tokenizer.apply_chat_template(
|
| 49 |
+
chat_messages,
|
| 50 |
+
tokenize=False,
|
| 51 |
+
add_generation_prompt=True
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 55 |
+
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
output = model.generate(
|
| 58 |
+
**inputs,
|
| 59 |
+
max_new_tokens=max_tokens,
|
| 60 |
+
do_sample=temperature > 0,
|
| 61 |
+
temperature=max(temperature, 0.01),
|
| 62 |
+
pad_token_id=tokenizer.eos_token_id
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
generated = output[0][inputs.input_ids.shape[-1]:]
|
| 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 |
+
"id": MODEL_ID,
|
| 84 |
+
"object": "model",
|
| 85 |
+
"owned_by": "local"
|
| 86 |
+
}
|
| 87 |
+
]
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
@app.post("/v1/chat/completions")
|
| 91 |
+
def chat_completions(request: ChatRequest):
|
| 92 |
+
response = generate(
|
| 93 |
+
request.messages,
|
| 94 |
+
request.temperature,
|
| 95 |
+
request.max_tokens
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
return {
|
| 99 |
+
"id": "chatcmpl-qwen",
|
| 100 |
+
"object": "chat.completion",
|
| 101 |
+
"created": 0,
|
| 102 |
+
"model": MODEL_ID,
|
| 103 |
+
"choices": [
|
| 104 |
+
{
|
| 105 |
+
"index": 0,
|
| 106 |
+
"message": {
|
| 107 |
+
"role": "assistant",
|
| 108 |
+
"content": response
|
| 109 |
+
},
|
| 110 |
+
"finish_reason": "stop"
|
| 111 |
+
}
|
| 112 |
+
],
|
| 113 |
+
"usage": {
|
| 114 |
+
"prompt_tokens": 0,
|
| 115 |
+
"completion_tokens": 0,
|
| 116 |
+
"total_tokens": 0
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|