Download app.py from MrFozan/fozan-mini: direct link, hf CLI and curl.
- Browser
- Download file 5.26 kB
-
https://huggingface.co/MrFozan/fozan-mini/resolve/main/app.py
- Command line
-
hf download hf://MrFozan/fozan-mini/app.py
-
curl -L -o app.py https://huggingface.co/MrFozan/fozan-mini/resolve/main/app.py
5.26 kB
| """ | |
| 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""" | |
| <div class="fozan-hero"> | |
| # ๐ค Fozan Mini | |
| Mini-LLM **{PARAMS} parameter** yang dilatih **dari nol di CPU laptop** | |
| (Intel i3-1215U, 8 GB RAM, tanpa GPU) dengan dataset bahasa Indonesia. | |
| **Arsitektur:** RoPE + GQA + SwiGLU + RMSNorm + QK-norm ยท | |
| **Tokenizer:** char-level 70 token ยท **Konteks:** 384 karakter | |
| <span class="fozan-muted"> | |
| Model ini dilatih dari 225 contoh percakapan. Pertanyaan di luar pola | |
| dataset kemungkinan besar dijawab ngawur โ itu batas data, bukan bug. | |
| </span> | |
| </div> | |
| """ | |
| ) | |
| gr.ChatInterface( | |
| respond, | |
| type="messages", | |
| examples=EXAMPLES, | |
| cache_examples=False, | |
| run_examples_on_click=True, | |
| additional_inputs=[ | |
| gr.Slider(0.05, 1.0, value=DEFAULT_TEMP, step=0.05, | |
| label="Temperature (kecil = konsisten, besar = kreatif)"), | |
| gr.Slider(40, 300, value=DEFAULT_TOKENS, step=20, | |
| label="Panjang jawaban (maks token)"), | |
| gr.Slider(1, 60, value=DEFAULT_TOP_K, step=1, label="Top-k"), | |
| gr.Slider(0.3, 1.0, value=DEFAULT_TOP_P, step=0.05, label="Top-p"), | |
| gr.Slider(1.0, 1.6, value=DEFAULT_REP, step=0.05, | |
| label="Repetition penalty"), | |
| ], | |
| additional_inputs_accordion=gr.Accordion("โ๏ธ Pengaturan sampling", open=False), | |
| title=None, | |
| description=None, | |
| ) | |
| gr.Markdown( | |
| """ | |
| --- | |
| ### ๐ Tautan | |
| [๐ค Model](https://huggingface.co/MrFozan/fozan-mini) ยท | |
| [๐ Dataset](https://huggingface.co/datasets/MrFozan/fozan-mini-dataset) ยท | |
| [๐ป GitHub](https://github.com/mahakammoonlightstudio-beep/llm-fozan) | |
| ### ๐ค Dukung perkembangan Fozan | |
| Proyek ini dikembangkan sendirian di laptop biasa โ open source | |
| sepenuhnya di bawah lisensi MIT. | |
| ๐ฎ๐ฉ [Saweria](https://saweria.co/MahakamMoonStudio) | | |
| ๐ [PayPal](https://www.paypal.com/paypalme/mrfozan) | | |
| ๐ [Sociabuzz](https://sociabuzz.com/mahakam_moonlight_studio/tribe) | |
| """ | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |