Spaces:
Running
Running
Error Lover commited on
Commit ·
badc803
1
Parent(s): e62be1a
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 4 |
+
from threading import Thread
|
| 5 |
+
|
| 6 |
+
MODEL = "yava-code/kimi-coder-135m"
|
| 7 |
+
|
| 8 |
+
tok = AutoTokenizer.from_pretrained(MODEL)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
MODEL,
|
| 11 |
+
torch_dtype=torch.bfloat16,
|
| 12 |
+
device_map="auto",
|
| 13 |
+
)
|
| 14 |
+
model.eval()
|
| 15 |
+
|
| 16 |
+
def respond(msg, history, max_new, temp):
|
| 17 |
+
chat = []
|
| 18 |
+
for u, a in history:
|
| 19 |
+
chat += [{"role": "user", "content": u}, {"role": "assistant", "content": a}]
|
| 20 |
+
chat.append({"role": "user", "content": msg})
|
| 21 |
+
|
| 22 |
+
input_ids = tok.apply_chat_template(chat, return_tensors="pt", add_generation_prompt=True).to(model.device)
|
| 23 |
+
|
| 24 |
+
streamer = TextIteratorStreamer(tok, skip_prompt=True, skip_special_tokens=True)
|
| 25 |
+
gen_kwargs = dict(
|
| 26 |
+
input_ids=input_ids,
|
| 27 |
+
streamer=streamer,
|
| 28 |
+
max_new_tokens=max_new,
|
| 29 |
+
temperature=temp,
|
| 30 |
+
do_sample=temp > 0,
|
| 31 |
+
)
|
| 32 |
+
Thread(target=model.generate, kwargs=gen_kwargs).start()
|
| 33 |
+
|
| 34 |
+
out = ""
|
| 35 |
+
for token in streamer:
|
| 36 |
+
out += token
|
| 37 |
+
yield out
|
| 38 |
+
|
| 39 |
+
EXAMPLES = [
|
| 40 |
+
["Write a Python function to find all prime numbers up to n using the Sieve of Eratosthenes."],
|
| 41 |
+
["Implement a binary search tree with insert and search methods in Python."],
|
| 42 |
+
["Write a decorator that caches function results (memoization)."],
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
with gr.Blocks(title="kimi-coder-135m") as demo:
|
| 46 |
+
gr.Markdown(
|
| 47 |
+
"""
|
| 48 |
+
## 🤖 kimi-coder-135m
|
| 49 |
+
SmolLM2-135M fine-tuned on 15k coding samples distilled from KIMI-K2.5.
|
| 50 |
+
Model: [yava-code/kimi-coder-135m](https://huggingface.co/yava-code/kimi-coder-135m)
|
| 51 |
+
"""
|
| 52 |
+
)
|
| 53 |
+
chatbot = gr.ChatInterface(
|
| 54 |
+
respond,
|
| 55 |
+
additional_inputs=[
|
| 56 |
+
gr.Slider(64, 1024, value=512, label="Max new tokens"),
|
| 57 |
+
gr.Slider(0, 1, value=0.3, step=0.05, label="Temperature"),
|
| 58 |
+
],
|
| 59 |
+
examples=EXAMPLES,
|
| 60 |
+
title="",
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
demo.launch()
|