kitty-bash-demo / app.py
sahellx's picture
Create app.py
181364d verified
Raw History Blame Contribute Delete
1.82 kB
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()