File size: 1,821 Bytes
181364d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import spaces
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "sahellx/kitty-bash-llm"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.float16,
    device_map="auto"
)

@spaces.GPU
def generate_bash(instruction):
    if not instruction.strip():
        return "Please enter a valid command instruction."
    
    messages = [
        {"role": "system", "content": "You are a helpful assistant that generates bash and shell commands."},
        {"role": "user", "content": instruction}
    ]
    prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=128,
            temperature=0.2,
            do_sample=False,
            pad_token_id=tokenizer.eos_token_id
        )
    
    response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
    return response.strip()

demo = gr.Interface(
    fn=generate_bash,
    inputs=gr.Textbox(lines=2, label="Task instruction", placeholder="e.g. find all mp4 files larger than 100MB and delete them"),
    outputs=gr.Code(language="shell", label="Generated Bash Command"),
    title="Kitty Bash LLM Demo",
    description="Natural language to bash commands powered by sahellx/kitty-bash-llm on ZeroGPU.",
    examples=[
        ["list all active listening ports on this machine"],
        ["find all files with extension .log older than 7 days and delete them"],
        ["create a tar.gz archive of the folder named project"]
    ]
)

if __name__ == "__main__":
    demo.launch()