Zoder1.0-1B / zoder_gradio.py
Nasaawakening's picture
Upload Gradio Web UI
0ad1140 verified
Raw
History Blame Contribute Delete
2.73 kB
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)