update app
Browse files
app.py
CHANGED
|
@@ -1,28 +1,35 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
-
import torch
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
-
model_name = "Qwen/Qwen-1_8B"
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 10 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
@app.post("/chat")
|
| 17 |
-
|
| 18 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 28 |
-
return {"response": text}
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import torch, os
|
| 5 |
+
import uvicorn
|
| 6 |
|
| 7 |
+
# --- Inisialisasi FastAPI ---
|
| 8 |
+
app = FastAPI(title="Qwen Chat API")
|
| 9 |
|
| 10 |
+
# --- Load model & tokenizer ---
|
| 11 |
+
model_name = "Qwen/Qwen-1_8B-Chat" # ganti sesuai model yang muat di RAM
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
model_name,
|
| 15 |
+
trust_remote_code=True,
|
| 16 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 17 |
+
device_map="auto" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
)
|
| 19 |
|
| 20 |
+
# --- Request & Response schema ---
|
| 21 |
+
class ChatRequest(BaseModel):
|
| 22 |
+
prompt: str
|
| 23 |
+
max_new_tokens: int = 128
|
| 24 |
|
| 25 |
@app.post("/chat")
|
| 26 |
+
def chat(req: ChatRequest):
|
| 27 |
+
inputs = tokenizer(req.prompt, return_tensors="pt").to(model.device)
|
| 28 |
+
outputs = model.generate(**inputs, max_new_tokens=req.max_new_tokens)
|
| 29 |
+
reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 30 |
+
return {"reply": reply}
|
| 31 |
+
|
| 32 |
+
# --- Entrypoint ---
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
port = int(os.environ.get("PORT", 7860)) # HF Spaces default port
|
| 35 |
+
uvicorn.run("app:app", host="0.0.0.0", port=port)
|
|
|
|
|
|