File size: 1,310 Bytes
6f78bf3
f8184cb
6f78bf3
53ee96a
6f78bf3
53ee96a
6f78bf3
53ee96a
c5b37af
f8184cb
 
 
 
 
 
6f78bf3
f8184cb
 
 
6f78bf3
 
f8184cb
53ee96a
 
 
 
 
 
 
 
 
f8184cb
 
53ee96a
f8184cb
 
 
53ee96a
f8184cb
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
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch, os, uvicorn

app = FastAPI()

model_name = "Qwen/Qwen-1_8B-Chat"  # ganti sesuai ukuran
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    trust_remote_code=True,
    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
    device_map="auto" if torch.cuda.is_available() else "cpu"
)

class ChatRequest(BaseModel):
    prompt: str
    max_new_tokens: int = 128

@app.post("/chat")
def chat(req: ChatRequest):
    # Format percakapan sesuai template Qwen
    messages = [
        {"role": "system", "content": "You are a helpful AI assistant."},
        {"role": "user", "content": req.prompt},
    ]

    text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)

    inputs = tokenizer(text, return_tensors="pt").to(model.device)
    outputs = model.generate(**inputs, max_new_tokens=req.max_new_tokens)
    reply = tokenizer.decode(outputs[0], skip_special_tokens=True)

    return {"reply": reply}

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 7860))
    uvicorn.run("app:app", host="0.0.0.0", port=port)