Arifzyn19
commited on
Commit
·
d819961
1
Parent(s):
3c86e31
Add application file
Browse files- Dockerfile +20 -0
- app.py +35 -0
- requirements.txt +4 -0
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
# Tambahkan user non-root
|
4 |
+
RUN useradd -m -u 1000 user
|
5 |
+
USER user
|
6 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
7 |
+
|
8 |
+
# Bekerja di folder app
|
9 |
+
WORKDIR /app
|
10 |
+
|
11 |
+
# Install dependensi
|
12 |
+
COPY --chown=user requirements.txt .
|
13 |
+
RUN pip install --no-cache-dir --upgrade pip \
|
14 |
+
&& pip install --no-cache-dir -r requirements.txt
|
15 |
+
|
16 |
+
# Salin seluruh project
|
17 |
+
COPY --chown=user . .
|
18 |
+
|
19 |
+
# Port default Gradio (7860) atau bisa ganti jadi 8000 kalau pakai FastAPI
|
20 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from fastapi import FastAPI
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
model_id = "mistralai/Mistral-7B-Instruct-v0.1" # example model
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
11 |
+
|
12 |
+
class ChatRequest(BaseModel):
|
13 |
+
messages: list
|
14 |
+
|
15 |
+
@app.post("/chat")
|
16 |
+
async def chat(req: ChatRequest):
|
17 |
+
prompt = ""
|
18 |
+
for msg in req.messages:
|
19 |
+
role = msg['role']
|
20 |
+
content = msg['content']
|
21 |
+
prompt += f"[{role.capitalize()}]: {content}\n"
|
22 |
+
prompt += "[Assistant]:"
|
23 |
+
|
24 |
+
# Encode the prompt
|
25 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
26 |
+
inputs = {key: value.to(model.device) for key, value in inputs.items()}
|
27 |
+
|
28 |
+
# Generate a response
|
29 |
+
output = model.generate(inputs['input_ids'], max_new_tokens=100)
|
30 |
+
|
31 |
+
# Decode the output
|
32 |
+
result = tokenizer.decode(output[0], skip_special_tokens=True)
|
33 |
+
|
34 |
+
# Return the response, removing the prompt part
|
35 |
+
return {"response": result.replace(prompt, "").strip()}
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
transformers
|
4 |
+
torch
|