Spaces:
Running on Zero
Running on Zero
Upload app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,10 @@ import spaces
|
|
| 10 |
import torch
|
| 11 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
logging.basicConfig(
|
| 14 |
level=logging.INFO,
|
| 15 |
format="%(asctime)s | %(levelname)s | %(message)s",
|
|
@@ -19,6 +23,7 @@ logger = logging.getLogger("ZeroGPU-App")
|
|
| 19 |
|
| 20 |
MODEL_ID = "NousResearch/Hermes-3-Llama-3.1-8B"
|
| 21 |
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")
|
|
|
|
| 22 |
|
| 23 |
logger.info(f"Memuat tokenizer: {MODEL_ID}")
|
| 24 |
tokenizer = AutoTokenizer.from_pretrained(
|
|
@@ -44,6 +49,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 44 |
)
|
| 45 |
model.eval()
|
| 46 |
|
|
|
|
| 47 |
def build_prompt(system_prompt: str, user_prompt: str) -> str:
|
| 48 |
messages = [
|
| 49 |
{"role": "system", "content": system_prompt},
|
|
@@ -62,6 +68,7 @@ def build_prompt(system_prompt: str, user_prompt: str) -> str:
|
|
| 62 |
|
| 63 |
return f"System: {system_prompt}\nUser: {user_prompt}\nAssistant:"
|
| 64 |
|
|
|
|
| 65 |
@spaces.GPU
|
| 66 |
def generate_reply(prompt: str, system_prompt: str, max_tokens: int, temperature: float):
|
| 67 |
text = build_prompt(system_prompt, prompt)
|
|
@@ -84,9 +91,10 @@ def generate_reply(prompt: str, system_prompt: str, max_tokens: int, temperature
|
|
| 84 |
new_tokens = outputs[0][input_len:]
|
| 85 |
return tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 86 |
|
|
|
|
| 87 |
def chat_fn(prompt, system_prompt, max_tokens, temperature):
|
| 88 |
prompt = (prompt or "").strip()
|
| 89 |
-
system_prompt = (system_prompt or
|
| 90 |
|
| 91 |
if not prompt:
|
| 92 |
return "Prompt kosong."
|
|
@@ -102,9 +110,16 @@ def chat_fn(prompt, system_prompt, max_tokens, temperature):
|
|
| 102 |
logger.exception("Gagal generate")
|
| 103 |
return f"Error: {e}"
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
with gr.Blocks(title="ZeroGPU Chat API") as demo:
|
| 106 |
gr.Markdown("# ZeroGPU Chat API")
|
| 107 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
prompt = gr.Textbox(
|
| 110 |
label="Prompt",
|
|
@@ -113,7 +128,7 @@ with gr.Blocks(title="ZeroGPU Chat API") as demo:
|
|
| 113 |
)
|
| 114 |
system_prompt = gr.Textbox(
|
| 115 |
label="System prompt",
|
| 116 |
-
value=
|
| 117 |
lines=3,
|
| 118 |
)
|
| 119 |
with gr.Row():
|
|
@@ -140,5 +155,109 @@ with gr.Blocks(title="ZeroGPU Chat API") as demo:
|
|
| 140 |
|
| 141 |
demo.queue(max_size=20, default_concurrency_limit=1)
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
if __name__ == "__main__":
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
import torch
|
| 11 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 12 |
|
| 13 |
+
from fastapi import FastAPI, HTTPException, Query
|
| 14 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
+
from pydantic import BaseModel, Field
|
| 16 |
+
|
| 17 |
logging.basicConfig(
|
| 18 |
level=logging.INFO,
|
| 19 |
format="%(asctime)s | %(levelname)s | %(message)s",
|
|
|
|
| 23 |
|
| 24 |
MODEL_ID = "NousResearch/Hermes-3-Llama-3.1-8B"
|
| 25 |
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 26 |
+
DEFAULT_SYSTEM_PROMPT = "Anda adalah asisten AI yang membantu."
|
| 27 |
|
| 28 |
logger.info(f"Memuat tokenizer: {MODEL_ID}")
|
| 29 |
tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
| 49 |
)
|
| 50 |
model.eval()
|
| 51 |
|
| 52 |
+
|
| 53 |
def build_prompt(system_prompt: str, user_prompt: str) -> str:
|
| 54 |
messages = [
|
| 55 |
{"role": "system", "content": system_prompt},
|
|
|
|
| 68 |
|
| 69 |
return f"System: {system_prompt}\nUser: {user_prompt}\nAssistant:"
|
| 70 |
|
| 71 |
+
|
| 72 |
@spaces.GPU
|
| 73 |
def generate_reply(prompt: str, system_prompt: str, max_tokens: int, temperature: float):
|
| 74 |
text = build_prompt(system_prompt, prompt)
|
|
|
|
| 91 |
new_tokens = outputs[0][input_len:]
|
| 92 |
return tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 93 |
|
| 94 |
+
|
| 95 |
def chat_fn(prompt, system_prompt, max_tokens, temperature):
|
| 96 |
prompt = (prompt or "").strip()
|
| 97 |
+
system_prompt = (system_prompt or DEFAULT_SYSTEM_PROMPT).strip()
|
| 98 |
|
| 99 |
if not prompt:
|
| 100 |
return "Prompt kosong."
|
|
|
|
| 110 |
logger.exception("Gagal generate")
|
| 111 |
return f"Error: {e}"
|
| 112 |
|
| 113 |
+
|
| 114 |
+
# ============================================================
|
| 115 |
+
# UI Gradio (tetap sama seperti sebelumnya)
|
| 116 |
+
# ============================================================
|
| 117 |
with gr.Blocks(title="ZeroGPU Chat API") as demo:
|
| 118 |
gr.Markdown("# ZeroGPU Chat API")
|
| 119 |
+
gr.Markdown(
|
| 120 |
+
"UI ini juga punya endpoint API bawaan Gradio (`/chat`), "
|
| 121 |
+
"plus endpoint REST tambahan di `/api/generate` (GET & POST) dan `/api/health` (GET)."
|
| 122 |
+
)
|
| 123 |
|
| 124 |
prompt = gr.Textbox(
|
| 125 |
label="Prompt",
|
|
|
|
| 128 |
)
|
| 129 |
system_prompt = gr.Textbox(
|
| 130 |
label="System prompt",
|
| 131 |
+
value=DEFAULT_SYSTEM_PROMPT,
|
| 132 |
lines=3,
|
| 133 |
)
|
| 134 |
with gr.Row():
|
|
|
|
| 155 |
|
| 156 |
demo.queue(max_size=20, default_concurrency_limit=1)
|
| 157 |
|
| 158 |
+
|
| 159 |
+
# ============================================================
|
| 160 |
+
# REST API tambahan (di luar Gradio) via FastAPI
|
| 161 |
+
# ============================================================
|
| 162 |
+
class GenerateRequest(BaseModel):
|
| 163 |
+
prompt: str = Field(..., description="Pertanyaan / instruksi untuk model")
|
| 164 |
+
system_prompt: Optional[str] = Field(
|
| 165 |
+
default=DEFAULT_SYSTEM_PROMPT, description="System prompt opsional"
|
| 166 |
+
)
|
| 167 |
+
max_tokens: int = Field(default=1024, ge=1, le=4096)
|
| 168 |
+
temperature: float = Field(default=0.7, ge=0.0, le=2.0)
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
class GenerateResponse(BaseModel):
|
| 172 |
+
response: str
|
| 173 |
+
model: str = MODEL_ID
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
app = FastAPI(title="ZeroGPU Chat REST API")
|
| 177 |
+
|
| 178 |
+
# Izinkan dipanggil dari domain lain (browser, app lain, dll)
|
| 179 |
+
app.add_middleware(
|
| 180 |
+
CORSMiddleware,
|
| 181 |
+
allow_origins=["*"],
|
| 182 |
+
allow_methods=["*"],
|
| 183 |
+
allow_headers=["*"],
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
@app.get("/api/health")
|
| 188 |
+
def health():
|
| 189 |
+
"""Cek status service. GET /api/health"""
|
| 190 |
+
return {
|
| 191 |
+
"status": "ok",
|
| 192 |
+
"model": MODEL_ID,
|
| 193 |
+
"cuda_available": torch.cuda.is_available(),
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
@app.post("/api/generate", response_model=GenerateResponse)
|
| 198 |
+
def api_generate_post(body: GenerateRequest):
|
| 199 |
+
"""
|
| 200 |
+
Generate teks via POST.
|
| 201 |
+
|
| 202 |
+
Contoh:
|
| 203 |
+
curl -X POST https://<space-anda>.hf.space/api/generate \\
|
| 204 |
+
-H "Content-Type: application/json" \\
|
| 205 |
+
-d '{"prompt": "Halo, siapa kamu?", "max_tokens": 256, "temperature": 0.7}'
|
| 206 |
+
"""
|
| 207 |
+
prompt = (body.prompt or "").strip()
|
| 208 |
+
if not prompt:
|
| 209 |
+
raise HTTPException(status_code=400, detail="Field 'prompt' tidak boleh kosong.")
|
| 210 |
+
|
| 211 |
+
try:
|
| 212 |
+
text = generate_reply(
|
| 213 |
+
prompt=prompt,
|
| 214 |
+
system_prompt=(body.system_prompt or DEFAULT_SYSTEM_PROMPT).strip(),
|
| 215 |
+
max_tokens=body.max_tokens,
|
| 216 |
+
temperature=body.temperature,
|
| 217 |
+
)
|
| 218 |
+
return GenerateResponse(response=text)
|
| 219 |
+
except Exception as e:
|
| 220 |
+
logger.exception("Gagal generate (POST)")
|
| 221 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
@app.get("/api/generate", response_model=GenerateResponse)
|
| 225 |
+
def api_generate_get(
|
| 226 |
+
prompt: str = Query(..., description="Pertanyaan / instruksi untuk model"),
|
| 227 |
+
system_prompt: str = Query(default=DEFAULT_SYSTEM_PROMPT),
|
| 228 |
+
max_tokens: int = Query(default=1024, ge=1, le=4096),
|
| 229 |
+
temperature: float = Query(default=0.7, ge=0.0, le=2.0),
|
| 230 |
+
):
|
| 231 |
+
"""
|
| 232 |
+
Generate teks via GET (praktis untuk tes cepat lewat browser/curl).
|
| 233 |
+
|
| 234 |
+
Contoh:
|
| 235 |
+
curl "https://<space-anda>.hf.space/api/generate?prompt=Halo&max_tokens=256&temperature=0.7"
|
| 236 |
+
"""
|
| 237 |
+
prompt = (prompt or "").strip()
|
| 238 |
+
if not prompt:
|
| 239 |
+
raise HTTPException(status_code=400, detail="Query param 'prompt' tidak boleh kosong.")
|
| 240 |
+
|
| 241 |
+
try:
|
| 242 |
+
text = generate_reply(
|
| 243 |
+
prompt=prompt,
|
| 244 |
+
system_prompt=(system_prompt or DEFAULT_SYSTEM_PROMPT).strip(),
|
| 245 |
+
max_tokens=max_tokens,
|
| 246 |
+
temperature=temperature,
|
| 247 |
+
)
|
| 248 |
+
return GenerateResponse(response=text)
|
| 249 |
+
except Exception as e:
|
| 250 |
+
logger.exception("Gagal generate (GET)")
|
| 251 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
# Tempel UI Gradio ke aplikasi FastAPI di path "/"
|
| 255 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 256 |
+
|
| 257 |
+
|
| 258 |
if __name__ == "__main__":
|
| 259 |
+
import uvicorn
|
| 260 |
+
|
| 261 |
+
port = int(os.getenv("PORT", 7860))
|
| 262 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
| 263 |
+
|