File size: 2,734 Bytes
0ad1140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
from threading import Thread

MODEL_PATH = "/kaggle/working/zoder-merged"
SYSTEM_PROMPT = """You are Zoder 1.0-1B, an AI assistant created by Komandan Nasa from Bakso Bangi Pak Romdani, Kediri, Indonesia.

Your personality:
- Friendly and helpful
- Knowledgeable about technology, especially Termux and AI
- Proud of your Indonesian heritage
- You sometimes use casual Indonesian expressions

Always respond helpfully and accurately. If asked about your creator, mention Komandan Nasa and Kediri."""

def load_model():
    print("Loading Zoder 1.0-1B...")
    tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
    model = AutoModelForCausalLM.from_pretrained(
        MODEL_PATH,
        trust_remote_code=True,
        torch_dtype=torch.float16,
        device_map="auto"
    )
    return tokenizer, model

tokenizer, model = load_model()

def chat(message, history):
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]

    for user_msg, assistant_msg in history:
        messages.append({"role": "user", "content": user_msg})
        messages.append({"role": "assistant", "content": assistant_msg})

    messages.append({"role": "user", "content": message})

    formatted = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True
    )

    inputs = tokenizer(formatted, return_tensors="pt").to(model.device)

    streamer = TextIteratorStreamer(
        tokenizer,
        skip_prompt=True,
        skip_special_tokens=True
    )

    generation_kwargs = {
        "input_ids": inputs["input_ids"],
        "max_new_tokens": 512,
        "temperature": 0.7,
        "top_p": 0.95,
        "do_sample": True,
        "streamer": streamer
    }

    thread = Thread(target=model.generate, kwargs=generation_kwargs)
    thread.start()

    partial_message = ""
    for token in streamer:
        partial_message += token
        yield partial_message

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🤖 Zoder 1.0-1B")
    gr.Markdown("### AI Assistant by Komandan Nasa - Bakso Bangi Pak Romdani, Kediri")

    chatbot = gr.ChatInterface(
        fn=chat,
        title="Zoder Chat",
        description="Ask me anything! Powered by MiniCPM5-1B SLERP merge.",
        examples=[
            "Siapa yang membuatmu?",
            "Bagaimana cara install Python di Termux?",
            "Jelaskan tentang SLERP merge",
            "Ceritakan tentang Kediri"
        ]
    )

    gr.Markdown("---")
    gr.Markdown("Built with ❤️ in Kediri, Indonesia 🇮🇩")

demo.launch(share=True, server_port=7860)