""" app.py โ demo web Fozan Mini (Gradio) untuk Hugging Face Spaces. Untuk HF Spaces: file ini HARUS di root repo Space, bersama app.py, requirements.txt, fozan/, checkpoints/fozan_mini.pt (lihat README bagian "Hugging Face"). Jalankan lokal: pip install "gradio>=5.0" python app.py """ from __future__ import annotations import warnings import gradio as gr from fozan.chat import ChatEngine from fozan.config import MODEL_CKPT from fozan.console import enable_utf8 enable_utf8() warnings.filterwarnings("ignore", message=".*torch.ao.quantization.*") ERROR_TEXT = ("Model belum bisa dimuat: {err}\n\n" "Latih dulu di mesinmu:\n" " python scripts/prepare.py\n" " python scripts/pretrain.py\n" " python scripts/finetune.py") try: ENGINE = ChatEngine(MODEL_CKPT) LOAD_ERROR = None except SystemExit as exc: # checkpoint belum ada / format lama ENGINE, LOAD_ERROR = None, str(exc) PARAMS = f"{ENGINE.params:,}" if ENGINE is not None else "?" DEFAULT_TEMP = 0.3 DEFAULT_TOKENS = 160 DEFAULT_TOP_K = 20 DEFAULT_TOP_P = 0.85 DEFAULT_REP = 1.25 def respond(message, history, temperature, max_tokens, top_k, top_p, rep): """Terima pesan + pengaturan sampling, kembalikan jawaban teks.""" if ENGINE is None: return ERROR_TEXT.format(err=LOAD_ERROR) text = (message or "").strip() if not text: return "Tulis pertanyaan dulu ya." answer = ENGINE.answer( text, temperature=float(temperature), max_new_tokens=int(max_tokens), top_k=int(top_k), top_p=float(top_p), repetition_penalty=float(rep), ) return answer or "(Fozan bingung, coba tanya lain...)" # Dengan additional_inputs, Gradio 5 meminta examples berupa list of lists: # [pesan, temperature, max_tokens, top_k, top_p, repetition_penalty] EXAMPLES = [ ["Halo, Fozan!", DEFAULT_TEMP, DEFAULT_TOKENS, DEFAULT_TOP_K, DEFAULT_TOP_P, DEFAULT_REP], ["Siapa kamu?", DEFAULT_TEMP, DEFAULT_TOKENS, DEFAULT_TOP_K, DEFAULT_TOP_P, DEFAULT_REP], ["Apa saja yang bisa kamu lakukan?", DEFAULT_TEMP, DEFAULT_TOKENS, DEFAULT_TOP_K, DEFAULT_TOP_P, DEFAULT_REP], ["Aku capek banget hari ini.", DEFAULT_TEMP, DEFAULT_TOKENS, DEFAULT_TOP_K, DEFAULT_TOP_P, DEFAULT_REP], ["Terjemahkan selamat pagi.", DEFAULT_TEMP, DEFAULT_TOKENS, DEFAULT_TOP_K, DEFAULT_TOP_P, DEFAULT_REP], ["Ringkas: Karena hujan deras, acara di lapangan dipindah ke aula.", DEFAULT_TEMP, DEFAULT_TOKENS, DEFAULT_TOP_K, DEFAULT_TOP_P, DEFAULT_REP], ] CSS = """ .gradio-container { max-width: 960px !important; } .fozan-hero { border: 1px solid var(--border-color-primary); border-radius: 16px; padding: 18px 22px; background: linear-gradient(135deg, rgba(99,102,241,.12), rgba(16,185,129,.10)); } .fozan-hero h1 { margin: 0 0 6px 0; } .fozan-muted { opacity: .8; font-size: .95rem; } """ with gr.Blocks(title="Fozan Mini LLM", theme=gr.themes.Soft(), css=CSS) as demo: gr.Markdown( f"""