Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_id = "andreagalasso99/Qwen3.5-4B-NL2SH"
4tokenizer = AutoTokenizer.from_pretrained(model_id)
5model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
6
7SYSTEM_PROMPT = """You are a Linux System Automator. \
8Your goal is to convert natural language instructions into precise, \
9safe Bash commands. Given the following instruction, \
10output a JSON object with two keys: 'command' (the string to execute) \
11and 'safety_warning' (a string describing eventual risks, or 'none')."""
12
13def generate_command(user_instruction):
14 messages = [
15 {"role": "system", "content": SYSTEM_PROMPT},
16 {"role": "user", "content": user_instruction},
17 ]
18
19 # Apply ChatML template
20 prompt = tokenizer.apply_chat_template(
21 messages,
22 tokenize=False,
23 add_generation_prompt=True
24 )
25
26 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
27
28 outputs = model.generate(
29 **inputs,
30 max_new_tokens=128,
31 temperature=0.7,
32 do_sample=True,
33 pad_token_id=tokenizer.pad_token_id,
34 eos_token_id=tokenizer.eos_token_id
35 )
36
37 # Decode and extract only the new tokens
38 decoded_output = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
39 return decoded_output.strip()
40
41# Example Usage
42instruction = "Find all files in /home/user/ that end with .txt and count them."
43response = generate_command(instruction)
44print(response)