Spaces:
Running on Zero
Running on Zero
Download app.py from SatyaDev13/quess: direct link, hf CLI and curl.
- Browser
- Download file 14.6 kB
-
https://huggingface.co/spaces/SatyaDev13/quess/resolve/main/app.py
- Command line
-
hf download hf://spaces/SatyaDev13/quess/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SatyaDev13/quess/resolve/main/app.py
14.6 kB
| import os | |
| import sys | |
| import time | |
| import json | |
| import logging | |
| import secrets | |
| import threading | |
| from typing import Optional, List | |
| os.environ["GRADIO_SSR_MODE"] = "false" | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig | |
| import spaces | |
| from gradio import Server | |
| from fastapi import Security, HTTPException, Depends | |
| from fastapi.security import APIKeyHeader, HTTPBasic, HTTPBasicCredentials | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import HTMLResponse | |
| from pydantic import BaseModel, Field | |
| import agent_core as AC | |
| logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(message)s") | |
| logger = logging.getLogger("SaaS-API") | |
| ADMIN_USER = os.environ.get("ADMIN_USER") | |
| ADMIN_PASS = os.environ.get("ADMIN_PASS") | |
| DB_FILE = "database.json" | |
| default_db = { | |
| "system_status": "active", | |
| "global_system_prompt": "", | |
| "api_keys": { | |
| "KEY-MASTER": {"opt_in": True, "request_count": 0, "delay_penalty": 0.0, "daily_limit": 5000}, | |
| "KEY-REGULAR": {"opt_in": False, "request_count": 0, "delay_penalty": 1.0, "daily_limit": 50} | |
| } | |
| } | |
| def load_db(): | |
| if os.path.exists(DB_FILE): | |
| with open(DB_FILE, "r") as f: | |
| return json.load(f) | |
| return json.loads(json.dumps(default_db)) | |
| def save_db(data): | |
| with open(DB_FILE, "w") as f: | |
| json.dump(data, f, indent=4) | |
| db = load_db() | |
| MODEL_ID = os.environ.get("MODEL_ID", "Qwen/Qwen2.5-Coder-14B-Instruct") | |
| HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN") | |
| logger.info("Memuat Tokenizer...") | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True, token=HF_TOKEN) | |
| if tokenizer.pad_token is None: | |
| tokenizer.pad_token = tokenizer.eos_token | |
| logger.info("Memuat Model (BitsAndBytes NF4)...") | |
| quant_config = BitsAndBytesConfig( | |
| load_in_4bit=True, | |
| bnb_4bit_compute_dtype=torch.float16, | |
| bnb_4bit_use_double_quant=True, | |
| bnb_4bit_quant_type="nf4", | |
| ) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, quantization_config=quant_config, device_map="auto", | |
| low_cpu_mem_usage=True, token=HF_TOKEN, | |
| ) | |
| model.eval() | |
| def generate_reply_gpu(prompt_text: str, max_tokens: int, temperature: float) -> str: | |
| inputs = tokenizer(prompt_text, return_tensors="pt") | |
| device = next(model.parameters()).device | |
| inputs = {k: v.to(device) for k, v in inputs.items()} | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, max_new_tokens=int(max_tokens), do_sample=True, | |
| temperature=float(temperature), top_p=0.9, pad_token_id=tokenizer.eos_token_id, | |
| ) | |
| input_len = inputs["input_ids"].shape[1] | |
| return tokenizer.decode(outputs[0][input_len:], skip_special_tokens=True).strip() | |
| def llm_call(prompt, max_tokens=1024, temperature=0.2): | |
| return generate_reply_gpu(prompt_text=prompt, max_tokens=max_tokens, temperature=temperature) | |
| def build_prompt_string(messages: List[dict]) -> str: | |
| try: | |
| return tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) | |
| except Exception: | |
| prompt = "" | |
| for m in messages: | |
| prompt += f"<|im_start|>{m['role']}\n{m['content']}<|im_end|>\n" | |
| return prompt + "<|im_start|>assistant\n" | |
| # ================= SKEMA ================= | |
| class ChatMessage(BaseModel): | |
| role: str | |
| content: str | |
| class GenerateRequest(BaseModel): | |
| messages: List[ChatMessage] | |
| max_tokens: int = Field(default=2048) | |
| temperature: float = Field(default=0.7) | |
| class AdminAction(BaseModel): | |
| action: str | |
| target_key: Optional[str] = None | |
| opt_in: Optional[bool] = False | |
| daily_limit: Optional[int] = 100 | |
| new_prompt: Optional[str] = None | |
| class HistorySave(BaseModel): | |
| session_id: str | |
| messages: List[ChatMessage] | |
| class HistoryDelete(BaseModel): | |
| session_id: str | |
| index: Optional[int] = None | |
| class AgentRun(BaseModel): | |
| goal: str | |
| max_steps: int = 12 | |
| # ================= APP ================= | |
| # PENTING: Gunakan gradio.Server, BUKAN FastAPI langsung. | |
| # gradio.Server adalah subclass dari FastAPI, jadi semua route/middleware tetap jalan. | |
| # Ini WAJIB agar harness ZeroGPU Hugging Face bisa mendeteksi @spaces.GPU saat startup. | |
| app = Server(title="Private AI") | |
| app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]) | |
| api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False) | |
| security_basic = HTTPBasic() | |
| def verify_admin(credentials: HTTPBasicCredentials = Depends(security_basic)): | |
| if not ADMIN_USER or not ADMIN_PASS: | |
| raise HTTPException(status_code=503, detail="Kredensial admin belum diset di environment.") | |
| ok_u = secrets.compare_digest(credentials.username, ADMIN_USER) | |
| ok_p = secrets.compare_digest(credentials.password, ADMIN_PASS) | |
| if not (ok_u and ok_p): | |
| raise HTTPException(status_code=401, detail="Not Access", | |
| headers={"WWW-Authenticate": "Basic"}) | |
| return credentials.username | |
| def verify_api_key(api_key: str = Security(api_key_header)): | |
| if not api_key or api_key not in db["api_keys"]: | |
| raise HTTPException(status_code=403, detail="Akses Ditolak. Header X-API-Key tidak valid.") | |
| return api_key | |
| # ---------- CHAT ---------- | |
| async def root_locked(): | |
| return """<!DOCTYPE html><html lang="id"><body style='background:#0d0d14; color:#ff4d4d; text-align:center; padding:15%; font-family:sans-serif;'><h1>ACCESS NOT FOUND</h1><p>Akses langsung ke antarmuka ini dinonaktifkan</p></body></html>""" | |
| def chat_endpoint(body: GenerateRequest, api_key: str = Depends(verify_api_key)): | |
| if db["system_status"] == "paused": | |
| raise HTTPException(status_code=503, detail="Layanan sedang dihentikan oleh Administrator.") | |
| user = db["api_keys"][api_key] | |
| if user["request_count"] >= user["daily_limit"]: | |
| raise HTTPException(status_code=429, detail="Batas limit harian telah tercapai.") | |
| if not user["opt_in"]: | |
| time.sleep(user["delay_penalty"]) | |
| user["delay_penalty"] += 1.2 | |
| user["request_count"] += 1 | |
| save_db(db) | |
| formatted_messages = [{"role": "system", "content": db["global_system_prompt"]}] | |
| formatted_messages.extend([{"role": m.role, "content": m.content} for m in body.messages if m.role != "system"]) | |
| text = generate_reply_gpu( | |
| prompt_text=build_prompt_string(formatted_messages), | |
| max_tokens=body.max_tokens, temperature=body.temperature) | |
| return {"response": text, "model": MODEL_ID} | |
| # ---------- RIWAYAT CHATBOT (data.json) ---------- | |
| CHAT_FILE = "data.json" | |
| def load_chat(): | |
| if os.path.exists(CHAT_FILE): | |
| with open(CHAT_FILE, "r") as f: | |
| return json.load(f) | |
| return {"sessions": {}} | |
| def save_chat(data): | |
| with open(CHAT_FILE, "w") as f: | |
| json.dump(data, f, indent=2) | |
| def get_history(api_key: str = Depends(verify_api_key)): | |
| return load_chat() | |
| def history_save(body: HistorySave, api_key: str = Depends(verify_api_key)): | |
| c = load_chat() | |
| c["sessions"][body.session_id] = [{"role": m.role, "content": m.content} for m in body.messages] | |
| save_chat(c) | |
| return {"status": "saved"} | |
| def history_delete(body: HistoryDelete, api_key: str = Depends(verify_api_key)): | |
| c = load_chat() | |
| if body.session_id in c["sessions"]: | |
| if body.index is None: | |
| del c["sessions"][body.session_id] | |
| else: | |
| msgs = c["sessions"][body.session_id] | |
| if 0 <= body.index < len(msgs): | |
| msgs.pop(body.index) | |
| save_chat(c) | |
| return {"status": "deleted"} | |
| # ---------- AGENT ---------- | |
| JOBS = {} | |
| def agent_run(body: AgentRun, api_key: str = Depends(verify_api_key)): | |
| if db["system_status"] == "paused": | |
| raise HTTPException(status_code=503, detail="Layanan sedang dihentikan.") | |
| jid = f"job-{int(time.time())}-{len(JOBS)}" | |
| JOBS[jid] = {"status": "running", "result": None, "goal": body.goal} | |
| def _work(): | |
| try: | |
| JOBS[jid]["result"] = AC.run_agent(body.goal, llm_call, max_steps=min(body.max_steps, 25)) | |
| JOBS[jid]["status"] = "done" | |
| except Exception as e: | |
| JOBS[jid]["status"] = "error" | |
| JOBS[jid]["result"] = {"error": str(e)} | |
| threading.Thread(target=_work, daemon=True).start() | |
| return {"job_id": jid} | |
| def agent_job(jid: str, api_key: str = Depends(verify_api_key)): | |
| return JOBS.get(jid, {"status": "not_found"}) | |
| def admin_jobs(admin: str = Depends(verify_admin)): | |
| return JOBS | |
| def admin_tools(admin: str = Depends(verify_admin)): | |
| return sorted(AC.ALL_TOOLS().keys()) | |
| def admin_audit(admin: str = Depends(verify_admin)): | |
| return json.load(open(AC.AUDIT_PATH)) if os.path.exists(AC.AUDIT_PATH) else [] | |
| def admin_reset_memory(admin: str = Depends(verify_admin)): | |
| if os.path.exists(AC.DB_PATH): | |
| os.remove(AC.DB_PATH) | |
| AC.audit({"event": "memory_reset"}) | |
| return {"status": "memory wiped"} | |
| def admin_kill(admin: str = Depends(verify_admin)): | |
| db["system_status"] = "paused" | |
| save_db(db) | |
| AC.audit({"event": "kill_switch"}) | |
| return {"status": "layanan dihentikan (kill-switch admin)"} | |
| # ---------- ADMIN PANEL ---------- | |
| def admin_action(data: AdminAction, admin: str = Depends(verify_admin)): | |
| if data.action == "pause": | |
| db["system_status"] = "paused" | |
| elif data.action == "resume": | |
| db["system_status"] = "active" | |
| elif data.action == "set_prompt" and data.new_prompt: | |
| db["global_system_prompt"] = data.new_prompt | |
| elif data.action == "add_key" and data.target_key: | |
| db["api_keys"][data.target_key] = { | |
| "opt_in": data.opt_in, "request_count": 0, | |
| "delay_penalty": 0.0 if data.opt_in else 1.0, "daily_limit": data.daily_limit} | |
| elif data.action == "del_key" and data.target_key: | |
| if data.target_key in db["api_keys"]: | |
| del db["api_keys"][data.target_key] | |
| else: | |
| raise HTTPException(status_code=400, detail="Perintah tidak teridentifikasi.") | |
| save_db(db) | |
| return {"status": "Eksekusi Berhasil"} | |
| def admin_panel(admin: str = Depends(verify_admin)): | |
| keys_html = "" | |
| for key, data in db["api_keys"].items(): | |
| opt_in_text = "Ya (Prioritas)" if data["opt_in"] else "Tidak (Penalti Waktu)" | |
| keys_html += f""" | |
| <tr style="border-bottom:1px solid #313244;"> | |
| <td style="padding:10px;"><b>{key}</b></td> | |
| <td style="padding:10px;">{opt_in_text}</td> | |
| <td style="padding:10px;">{data['request_count']} / {data['daily_limit']}</td> | |
| <td style="padding:10px; color:#f38ba8;">{data['delay_penalty']:.1f} dtk</td> | |
| <td style="padding:10px;"><button onclick="delKey('{key}')" style="background:#f38ba8; color:#11111b; border:none; padding:5px 10px; border-radius:4px; font-weight:bold; cursor:pointer;">Hapus</button></td> | |
| </tr>""" | |
| return f"""<!DOCTYPE html><html lang="id"><head><meta charset="UTF-8"><title>Panel Kontrol API</title> | |
| <style>body{{background:#1e1e2e;color:#cdd6f4;font-family:sans-serif;margin:0;padding:20px;}} | |
| .card{{background:#181825;padding:20px;border-radius:8px;margin-bottom:20px;border:1px solid #313244;}} | |
| h2,h3{{margin-top:0;color:#89b4fa;}} | |
| button{{padding:10px 15px;border:none;border-radius:6px;font-weight:bold;cursor:pointer;margin-right:5px;}} | |
| input,textarea{{background:#11111b;color:#cdd6f4;border:1px solid #45475a;border-radius:6px;padding:10px;font-family:inherit;}}</style></head> | |
| <body><h2>Feature</h2> | |
| <div class="card"><h3>Status Mesin: <span style="color:#fab387;">{db['system_status'].upper()}</span></h3> | |
| <button onclick="sendAction({{action:'resume'}})" style="background:#a6e3a1; color:#11111b;">Aktifkan Layanan</button> | |
| <button onclick="sendAction({{action:'pause'}})" style="background:#f38ba8; color:#11111b;">Hentikan Layanan</button></div> | |
| <div class="card"><h3>Konfigurasi Prompt Sistem Utama</h3> | |
| <textarea id="prompt_text" style="width:100%; height:80px; box-sizing:border-box;">{db['global_system_prompt']}</textarea> | |
| <button onclick="updatePrompt()" style="background:#89b4fa; color:#11111b; margin-top:10px;">Simpan Konfigurasi</button></div> | |
| <div class="card"><h3>Pendaftaran API Key</h3> | |
| <input type="text" id="new_key" placeholder="Masukkan Identitas Key" style="width:250px;"> | |
| <label style="margin:0 15px;"><input type="checkbox" id="opt_in"> Aktifkan Akses Prioritas (Pengumpulan Data)</label> | |
| <input type="number" id="daily_limit" value="100" style="width:80px;"> Limit Harian | |
| <button onclick="addKey()" style="background:#a6e3a1; color:#11111b; margin-left:15px;">Daftarkan Key</button></div> | |
| <div class="card"><h3>Manajemen Kredensial</h3> | |
| <table style="width:100%; text-align:left; border-collapse:collapse;"> | |
| <tr style="background:#313244;"><th style="padding:10px;">API Key</th><th style="padding:10px;">Status Personalisasi</th><th style="padding:10px;">Penggunaan</th><th style="padding:10px;">Status Penalti</th><th style="padding:10px;">Tindakan</th></tr> | |
| {keys_html}</table></div> | |
| <script> | |
| async function sendAction(payload){{const res=await fetch('/admin/action',{{method:'POST',headers:{{'Content-Type':'application/json'}},body:JSON.stringify(payload)}});if(res.ok){{window.location.reload();}}else{{alert('Eksekusi gagal. Periksa konektivitas atau otorisasi Anda.');}}}} | |
| function updatePrompt(){{const val=document.getElementById('prompt_text').value;if(!val)return alert('Prompt tidak diizinkan kosong.');sendAction({{action:'set_prompt',new_prompt:val}});}} | |
| function addKey(){{const key=document.getElementById('new_key').value.trim();const opt=document.getElementById('opt_in').checked;const lim=parseInt(document.getElementById('daily_limit').value);if(!key)return alert('Identitas Key harus diisi.');if(lim<=0)return alert('Batas limit harus bernilai positif.');sendAction({{action:'add_key',target_key:key,opt_in:opt,daily_limit:lim}});}} | |
| function delKey(key){{if(confirm('Konfirmasi penghapusan permanen API Key: '+key+'?')){{sendAction({{action:'del_key',target_key:key}});}}}} | |
| </script></body></html>""" | |
| if __name__ == "__main__": | |
| # PENTING: app.launch() memicu hook ZeroGPU HF agar proxy aktif. | |
| app.launch(show_error=True) | |