File size: 2,000 Bytes
e1d1986
4e6d10c
 
 
 
e1d1986
 
5c9e4f8
e1d1986
 
 
 
 
 
5c9e4f8
e1d1986
 
 
 
 
 
 
5c9e4f8
e1d1986
5c9e4f8
e1d1986
4e6d10c
 
 
 
 
e1d1986
 
4e6d10c
 
 
 
 
 
 
 
 
 
 
 
 
 
e1d1986
4e6d10c
e1d1986
4e6d10c
e1d1986
4e6d10c
e1d1986
 
 
 
4e6d10c
 
 
e1d1986
 
4e6d10c
 
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
import os
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
import torch

# Automatically load token from secret
hf_token = os.environ.get("HF_TOKEN")

# Load model
tokenizer = AutoTokenizer.from_pretrained(
    "moonshotai/Kimi-K2-Instruct",
    use_auth_token=hf_token,
    trust_remote_code=True
)

model = AutoModelForCausalLM.from_pretrained(
    "moonshotai/Kimi-K2-Instruct",
    trust_remote_code=True,
    torch_dtype=torch.float16,
    low_cpu_mem_usage=True,
    use_auth_token=hf_token
).eval()

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

# Format and chat
def format_prompt(history, user_input):
    system_prompt = "You are Kimi, a helpful and conversational AI assistant."
    history_text = "\n".join([f"User: {u}\nAI: {a}" for u, a in history])
    return f"{system_prompt}\n{history_text}\nUser: {user_input}\nAI:"

def chat(user_input, history):
    history = history or []
    prompt = format_prompt(history, user_input)
    inputs = tokenizer(prompt, return_tensors="pt").to("cpu")

    with torch.no_grad():
        output = model.generate(
            **inputs,
            max_new_tokens=512,
            do_sample=True,
            temperature=0.7,
            top_p=0.9,
            pad_token_id=tokenizer.eos_token_id,
        )
    response = tokenizer.decode(output[0], skip_special_tokens=True).split("AI:")[-1].strip()
    history.append((user_input, response))
    return history, history

# UI
with gr.Blocks(css="footer {visibility: hidden}") as demo:
    gr.Markdown("# 🤖 Kimi-K2 AI Assistant\nChat naturally with Kimi!")

    chatbot = gr.Chatbot(height=400)
    with gr.Row():
        user_input = gr.Textbox(placeholder="Type your message...", scale=10)
        submit_btn = gr.Button("Send", scale=2)

    state = gr.State([])

    submit_btn.click(chat, [user_input, state], [chatbot, state])
    user_input.submit(chat, [user_input, state], [chatbot, state])

demo.launch()