Spaces:
Running on Zero
Running on Zero
Upload app.py
Browse files
app.py
CHANGED
|
@@ -22,7 +22,7 @@ logger = logging.getLogger("ZeroGPU-App")
|
|
| 22 |
|
| 23 |
MODEL_ID = "NousResearch/Hermes-3-Llama-3.1-8B"
|
| 24 |
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 25 |
-
DEFAULT_SYSTEM_PROMPT = "Anda adalah asisten AI yang membantu."
|
| 26 |
|
| 27 |
logger.info(f"Memuat tokenizer: {MODEL_ID}")
|
| 28 |
tokenizer = AutoTokenizer.from_pretrained(
|
|
@@ -49,21 +49,33 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 49 |
model.eval()
|
| 50 |
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
if hasattr(tokenizer, "apply_chat_template"):
|
| 56 |
try:
|
| 57 |
-
|
| 58 |
messages, tokenize=False, add_generation_prompt=True
|
| 59 |
)
|
| 60 |
except Exception as e:
|
| 61 |
-
logger.error(f"Gagal
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
device = next(model.parameters()).device
|
| 68 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 69 |
|
|
@@ -82,25 +94,19 @@ def generate_reply(messages: List[dict], max_tokens: int, temperature: float) ->
|
|
| 82 |
return tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 83 |
|
| 84 |
|
| 85 |
-
# ============================================================
|
| 86 |
-
# Skema Data Baru untuk Mendukung Struktur Percakapan
|
| 87 |
-
# ============================================================
|
| 88 |
class ChatMessage(BaseModel):
|
| 89 |
-
role: str
|
| 90 |
content: str
|
| 91 |
|
| 92 |
-
|
| 93 |
class GenerateRequest(BaseModel):
|
| 94 |
-
messages: List[ChatMessage] = Field(...
|
| 95 |
max_tokens: int = Field(default=1024, ge=1, le=4096)
|
| 96 |
temperature: float = Field(default=0.7, ge=0.0, le=2.0)
|
| 97 |
|
| 98 |
-
|
| 99 |
class GenerateResponse(BaseModel):
|
| 100 |
response: str
|
| 101 |
model: str = MODEL_ID
|
| 102 |
|
| 103 |
-
|
| 104 |
app = Server(title="ZeroGPU Chat API")
|
| 105 |
|
| 106 |
app.add_middleware(
|
|
@@ -110,58 +116,26 @@ app.add_middleware(
|
|
| 110 |
allow_headers=["*"],
|
| 111 |
)
|
| 112 |
|
| 113 |
-
|
| 114 |
-
@app.get("/api/health")
|
| 115 |
-
def health():
|
| 116 |
-
return {
|
| 117 |
-
"status": "ok",
|
| 118 |
-
"model": MODEL_ID,
|
| 119 |
-
"cuda_available": torch.cuda.is_available(),
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
|
| 123 |
@app.post("/api/generate", response_model=GenerateResponse)
|
| 124 |
def api_generate_post(body: GenerateRequest):
|
| 125 |
-
"""
|
| 126 |
-
POST /api/generate — Menerima array pesan lengkap untuk mempertahankan konteks obrolan.
|
| 127 |
-
|
| 128 |
-
Format JSON Request:
|
| 129 |
-
{
|
| 130 |
-
"messages": [
|
| 131 |
-
{"role": "system", "content": "Kamu adalah robot pintar."},
|
| 132 |
-
{"role": "user", "content": "Halo!"},
|
| 133 |
-
{"role": "assistant", "content": "Hai! Ada yang bisa kubantu?"},
|
| 134 |
-
{"role": "user", "content": "Siapa nama kamu tadi?"}
|
| 135 |
-
],
|
| 136 |
-
"max_tokens": 512,
|
| 137 |
-
"temperature": 0.7
|
| 138 |
-
}
|
| 139 |
-
"""
|
| 140 |
if not body.messages:
|
| 141 |
-
return {"response": "Error:
|
| 142 |
|
| 143 |
-
# Konversi data Pydantic menjadi format dict standar Python
|
| 144 |
formatted_messages = [{"role": m.role, "content": m.content} for m in body.messages]
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 148 |
max_tokens=body.max_tokens,
|
| 149 |
temperature=body.temperature,
|
| 150 |
)
|
| 151 |
return GenerateResponse(response=text)
|
| 152 |
|
| 153 |
-
|
| 154 |
@app.get("/api/generate", response_model=GenerateResponse)
|
| 155 |
-
def api_generate_get(
|
| 156 |
-
prompt: str,
|
| 157 |
-
system_prompt: str = DEFAULT_SYSTEM_PROMPT,
|
| 158 |
-
max_tokens: int = 1024,
|
| 159 |
-
temperature: float = 0.7,
|
| 160 |
-
):
|
| 161 |
-
"""
|
| 162 |
-
GET /api/generate — Mode sekali jawab (Single-turn fallback) agar tetap
|
| 163 |
-
bisa digunakan untuk pengetesan instan via URL browser/curl.
|
| 164 |
-
"""
|
| 165 |
prompt = (prompt or "").strip()
|
| 166 |
if not prompt:
|
| 167 |
return {"response": "Error: prompt tidak boleh kosong.", "model": MODEL_ID}
|
|
@@ -170,18 +144,11 @@ def api_generate_get(
|
|
| 170 |
{"role": "system", "content": system_prompt},
|
| 171 |
{"role": "user", "content": prompt}
|
| 172 |
]
|
| 173 |
-
|
| 174 |
-
text =
|
| 175 |
-
messages=fallback_messages,
|
| 176 |
-
max_tokens=max_tokens,
|
| 177 |
-
temperature=temperature,
|
| 178 |
-
)
|
| 179 |
return GenerateResponse(response=text)
|
| 180 |
|
| 181 |
-
|
| 182 |
-
# ============================================================
|
| 183 |
-
# UI Baru Menggunakan Tampilan Chat Box Interaktif
|
| 184 |
-
# ============================================================
|
| 185 |
@app.get("/", response_class=HTMLResponse)
|
| 186 |
async def homepage():
|
| 187 |
return """
|
|
@@ -207,34 +174,23 @@ async def homepage():
|
|
| 207 |
</head>
|
| 208 |
<body>
|
| 209 |
<h1>ZeroGPU Chat API (Multi-Turn)</h1>
|
| 210 |
-
<p>UI Web ini otomatis menyimpan memori riwayat obrolan lokal Anda di browser.</p>
|
| 211 |
-
|
| 212 |
<div id="chat-box"></div>
|
| 213 |
-
|
| 214 |
<label>Pesan Anda</label>
|
| 215 |
<textarea id="prompt" rows="3" placeholder="Ketik pesan lalu tekan Kirim..."></textarea>
|
| 216 |
-
|
| 217 |
<label>System Prompt (Mengubah ini akan mereset obrolan)</label>
|
| 218 |
<input id="system_prompt" value="Anda adalah asisten AI yang membantu." onchange="resetChat()">
|
| 219 |
-
|
| 220 |
<div class="actions">
|
| 221 |
<button id="btn" onclick="kirim()">Kirim</button>
|
| 222 |
<button style="background:#ff4d4d;" onclick="resetChat()">Reset Obrolan</button>
|
| 223 |
</div>
|
| 224 |
-
|
| 225 |
<script>
|
| 226 |
let chatHistory = [];
|
| 227 |
-
|
| 228 |
function initChat() {
|
| 229 |
const sysPrompt = document.getElementById('system_prompt').value;
|
| 230 |
chatHistory = [{ role: "system", content: sysPrompt }];
|
| 231 |
renderChat();
|
| 232 |
}
|
| 233 |
-
|
| 234 |
-
function resetChat() {
|
| 235 |
-
initChat();
|
| 236 |
-
}
|
| 237 |
-
|
| 238 |
function renderChat() {
|
| 239 |
const box = document.getElementById('chat-box');
|
| 240 |
box.innerHTML = '';
|
|
@@ -246,32 +202,25 @@ function renderChat() {
|
|
| 246 |
});
|
| 247 |
box.scrollTop = box.scrollHeight;
|
| 248 |
}
|
| 249 |
-
|
| 250 |
function escapeHTML(str) {
|
| 251 |
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
| 252 |
}
|
| 253 |
-
|
| 254 |
async function kirim() {
|
| 255 |
const promptInput = document.getElementById('prompt');
|
| 256 |
const btn = document.getElementById('btn');
|
| 257 |
const userText = promptInput.value.trim();
|
| 258 |
-
|
| 259 |
if (!userText) return;
|
| 260 |
if (chatHistory.length === 0) initChat();
|
| 261 |
-
|
| 262 |
chatHistory.push({ role: "user", content: userText });
|
| 263 |
renderChat();
|
| 264 |
-
|
| 265 |
promptInput.value = "";
|
| 266 |
btn.disabled = true;
|
| 267 |
-
|
| 268 |
const box = document.getElementById('chat-box');
|
| 269 |
const loadingDiv = document.createElement('div');
|
| 270 |
loadingDiv.className = 'msg assistant';
|
| 271 |
loadingDiv.textContent = 'Sedang mengetik jawaban...';
|
| 272 |
box.appendChild(loadingDiv);
|
| 273 |
box.scrollTop = box.scrollHeight;
|
| 274 |
-
|
| 275 |
try {
|
| 276 |
const res = await fetch('/api/generate', {
|
| 277 |
method: 'POST',
|
|
@@ -282,10 +231,8 @@ async function kirim() {
|
|
| 282 |
temperature: 0.7
|
| 283 |
})
|
| 284 |
});
|
| 285 |
-
|
| 286 |
const data = await res.json();
|
| 287 |
box.removeChild(loadingDiv);
|
| 288 |
-
|
| 289 |
chatHistory.push({ role: "assistant", content: data.response });
|
| 290 |
renderChat();
|
| 291 |
} catch (e) {
|
|
@@ -300,15 +247,12 @@ async function kirim() {
|
|
| 300 |
btn.disabled = false;
|
| 301 |
}
|
| 302 |
}
|
| 303 |
-
|
| 304 |
-
// Inisialisasi awal saat halaman dimuat
|
| 305 |
initChat();
|
| 306 |
</script>
|
| 307 |
</body>
|
| 308 |
</html>
|
| 309 |
"""
|
| 310 |
|
| 311 |
-
|
| 312 |
if __name__ == "__main__":
|
| 313 |
app.launch(show_error=True)
|
| 314 |
|
|
|
|
| 22 |
|
| 23 |
MODEL_ID = "NousResearch/Hermes-3-Llama-3.1-8B"
|
| 24 |
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 25 |
+
DEFAULT_SYSTEM_PROMPT = "Anda adalah asisten AI yang cerdas, praktis, dan membantu."
|
| 26 |
|
| 27 |
logger.info(f"Memuat tokenizer: {MODEL_ID}")
|
| 28 |
tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
| 49 |
model.eval()
|
| 50 |
|
| 51 |
|
| 52 |
+
# ============================================================
|
| 53 |
+
# PEMBARUAN: Format teks dikerjakan di CPU, BUKAN di GPU
|
| 54 |
+
# ============================================================
|
| 55 |
+
def build_prompt_string(messages: List[dict]) -> str:
|
| 56 |
+
"""Mengubah array obrolan menjadi string rapi dengan format ChatML/Llama-3"""
|
| 57 |
if hasattr(tokenizer, "apply_chat_template"):
|
| 58 |
try:
|
| 59 |
+
return tokenizer.apply_chat_template(
|
| 60 |
messages, tokenize=False, add_generation_prompt=True
|
| 61 |
)
|
| 62 |
except Exception as e:
|
| 63 |
+
logger.error(f"Peringatan: Gagal memuat template asli, beralih ke ChatML manual. Error: {e}")
|
| 64 |
+
|
| 65 |
+
# Fallback manual menggunakan format ChatML (Standar untuk Hermes 3)
|
| 66 |
+
prompt = ""
|
| 67 |
+
for m in messages:
|
| 68 |
+
prompt += f"<|im_start|>{m['role']}\n{m['content']}<|im_end|>\n"
|
| 69 |
+
prompt += "<|im_start|>assistant\n"
|
| 70 |
+
return prompt
|
| 71 |
|
| 72 |
+
|
| 73 |
+
# ============================================================
|
| 74 |
+
# FUNGSI GPU HANYA MENERIMA STRING MENTAH
|
| 75 |
+
# ============================================================
|
| 76 |
+
@spaces.GPU
|
| 77 |
+
def generate_reply_gpu(prompt_text: str, max_tokens: int, temperature: float) -> str:
|
| 78 |
+
inputs = tokenizer(prompt_text, return_tensors="pt")
|
| 79 |
device = next(model.parameters()).device
|
| 80 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 81 |
|
|
|
|
| 94 |
return tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 95 |
|
| 96 |
|
|
|
|
|
|
|
|
|
|
| 97 |
class ChatMessage(BaseModel):
|
| 98 |
+
role: str
|
| 99 |
content: str
|
| 100 |
|
|
|
|
| 101 |
class GenerateRequest(BaseModel):
|
| 102 |
+
messages: List[ChatMessage] = Field(...)
|
| 103 |
max_tokens: int = Field(default=1024, ge=1, le=4096)
|
| 104 |
temperature: float = Field(default=0.7, ge=0.0, le=2.0)
|
| 105 |
|
|
|
|
| 106 |
class GenerateResponse(BaseModel):
|
| 107 |
response: str
|
| 108 |
model: str = MODEL_ID
|
| 109 |
|
|
|
|
| 110 |
app = Server(title="ZeroGPU Chat API")
|
| 111 |
|
| 112 |
app.add_middleware(
|
|
|
|
| 116 |
allow_headers=["*"],
|
| 117 |
)
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
@app.post("/api/generate", response_model=GenerateResponse)
|
| 120 |
def api_generate_post(body: GenerateRequest):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
if not body.messages:
|
| 122 |
+
return {"response": "Error: messages kosong.", "model": MODEL_ID}
|
| 123 |
|
|
|
|
| 124 |
formatted_messages = [{"role": m.role, "content": m.content} for m in body.messages]
|
| 125 |
+
|
| 126 |
+
# 1. Bangun format teks di CPU agar aman dari isolasi GPU
|
| 127 |
+
prompt_text = build_prompt_string(formatted_messages)
|
| 128 |
|
| 129 |
+
# 2. Kirim teks utuh ke GPU
|
| 130 |
+
text = generate_reply_gpu(
|
| 131 |
+
prompt_text=prompt_text,
|
| 132 |
max_tokens=body.max_tokens,
|
| 133 |
temperature=body.temperature,
|
| 134 |
)
|
| 135 |
return GenerateResponse(response=text)
|
| 136 |
|
|
|
|
| 137 |
@app.get("/api/generate", response_model=GenerateResponse)
|
| 138 |
+
def api_generate_get(prompt: str, system_prompt: str = DEFAULT_SYSTEM_PROMPT, max_tokens: int = 1024, temperature: float = 0.7):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
prompt = (prompt or "").strip()
|
| 140 |
if not prompt:
|
| 141 |
return {"response": "Error: prompt tidak boleh kosong.", "model": MODEL_ID}
|
|
|
|
| 144 |
{"role": "system", "content": system_prompt},
|
| 145 |
{"role": "user", "content": prompt}
|
| 146 |
]
|
| 147 |
+
prompt_text = build_prompt_string(fallback_messages)
|
| 148 |
+
text = generate_reply_gpu(prompt_text=prompt_text, max_tokens=max_tokens, temperature=temperature)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
return GenerateResponse(response=text)
|
| 150 |
|
| 151 |
+
# UI Bawaan Tetap Dipertahankan
|
|
|
|
|
|
|
|
|
|
| 152 |
@app.get("/", response_class=HTMLResponse)
|
| 153 |
async def homepage():
|
| 154 |
return """
|
|
|
|
| 174 |
</head>
|
| 175 |
<body>
|
| 176 |
<h1>ZeroGPU Chat API (Multi-Turn)</h1>
|
|
|
|
|
|
|
| 177 |
<div id="chat-box"></div>
|
|
|
|
| 178 |
<label>Pesan Anda</label>
|
| 179 |
<textarea id="prompt" rows="3" placeholder="Ketik pesan lalu tekan Kirim..."></textarea>
|
|
|
|
| 180 |
<label>System Prompt (Mengubah ini akan mereset obrolan)</label>
|
| 181 |
<input id="system_prompt" value="Anda adalah asisten AI yang membantu." onchange="resetChat()">
|
|
|
|
| 182 |
<div class="actions">
|
| 183 |
<button id="btn" onclick="kirim()">Kirim</button>
|
| 184 |
<button style="background:#ff4d4d;" onclick="resetChat()">Reset Obrolan</button>
|
| 185 |
</div>
|
|
|
|
| 186 |
<script>
|
| 187 |
let chatHistory = [];
|
|
|
|
| 188 |
function initChat() {
|
| 189 |
const sysPrompt = document.getElementById('system_prompt').value;
|
| 190 |
chatHistory = [{ role: "system", content: sysPrompt }];
|
| 191 |
renderChat();
|
| 192 |
}
|
| 193 |
+
function resetChat() { initChat(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
function renderChat() {
|
| 195 |
const box = document.getElementById('chat-box');
|
| 196 |
box.innerHTML = '';
|
|
|
|
| 202 |
});
|
| 203 |
box.scrollTop = box.scrollHeight;
|
| 204 |
}
|
|
|
|
| 205 |
function escapeHTML(str) {
|
| 206 |
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
| 207 |
}
|
|
|
|
| 208 |
async function kirim() {
|
| 209 |
const promptInput = document.getElementById('prompt');
|
| 210 |
const btn = document.getElementById('btn');
|
| 211 |
const userText = promptInput.value.trim();
|
|
|
|
| 212 |
if (!userText) return;
|
| 213 |
if (chatHistory.length === 0) initChat();
|
|
|
|
| 214 |
chatHistory.push({ role: "user", content: userText });
|
| 215 |
renderChat();
|
|
|
|
| 216 |
promptInput.value = "";
|
| 217 |
btn.disabled = true;
|
|
|
|
| 218 |
const box = document.getElementById('chat-box');
|
| 219 |
const loadingDiv = document.createElement('div');
|
| 220 |
loadingDiv.className = 'msg assistant';
|
| 221 |
loadingDiv.textContent = 'Sedang mengetik jawaban...';
|
| 222 |
box.appendChild(loadingDiv);
|
| 223 |
box.scrollTop = box.scrollHeight;
|
|
|
|
| 224 |
try {
|
| 225 |
const res = await fetch('/api/generate', {
|
| 226 |
method: 'POST',
|
|
|
|
| 231 |
temperature: 0.7
|
| 232 |
})
|
| 233 |
});
|
|
|
|
| 234 |
const data = await res.json();
|
| 235 |
box.removeChild(loadingDiv);
|
|
|
|
| 236 |
chatHistory.push({ role: "assistant", content: data.response });
|
| 237 |
renderChat();
|
| 238 |
} catch (e) {
|
|
|
|
| 247 |
btn.disabled = false;
|
| 248 |
}
|
| 249 |
}
|
|
|
|
|
|
|
| 250 |
initChat();
|
| 251 |
</script>
|
| 252 |
</body>
|
| 253 |
</html>
|
| 254 |
"""
|
| 255 |
|
|
|
|
| 256 |
if __name__ == "__main__":
|
| 257 |
app.launch(show_error=True)
|
| 258 |
|