|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
|
|
import torch
|
|
|
|
|
|
|
|
|
model_path = r"C:\app\dataset\new_cmd_model"
|
|
|
|
|
|
|
|
|
if not os.path.exists(model_path):
|
|
|
raise FileNotFoundError(f"Model directory '{model_path}' not found. Please verify the path.")
|
|
|
|
|
|
|
|
|
try:
|
|
|
model = T5ForConditionalGeneration.from_pretrained(model_path)
|
|
|
tokenizer = T5Tokenizer.from_pretrained(model_path)
|
|
|
print("Model and tokenizer loaded successfully.")
|
|
|
except Exception as e:
|
|
|
raise Exception(f"Error loading model or tokenizer: {str(e)}")
|
|
|
|
|
|
|
|
|
def generate_command_description(command_name, source, max_length=150):
|
|
|
|
|
|
prompt = f"Describe the command: {command_name} in {source}"
|
|
|
print(f"Input prompt: {prompt}")
|
|
|
|
|
|
|
|
|
inputs = tokenizer(prompt, return_tensors="pt", max_length=128, truncation=True)
|
|
|
|
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
model.to(device)
|
|
|
inputs = {key: value.to(device) for key, value in inputs.items()}
|
|
|
print(f"Using device: {device}")
|
|
|
|
|
|
|
|
|
try:
|
|
|
outputs = model.generate(
|
|
|
inputs["input_ids"],
|
|
|
max_length=max_length,
|
|
|
num_beams=5,
|
|
|
length_penalty=1.2,
|
|
|
early_stopping=True,
|
|
|
no_repeat_ngram_size=2,
|
|
|
do_sample=False
|
|
|
)
|
|
|
|
|
|
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
if not generated_text.strip():
|
|
|
return "Warning: Empty description generated. Check model training or prompt format."
|
|
|
return generated_text
|
|
|
except Exception as e:
|
|
|
return f"Error generating description: {str(e)}"
|
|
|
|
|
|
|
|
|
test_commands = [
|
|
|
("ls", "linux"),
|
|
|
("dir", "cmd"),
|
|
|
("chmod", "macos"),
|
|
|
("MsgBox", "vbscript")
|
|
|
]
|
|
|
|
|
|
print("\nGenerated Command Descriptions:")
|
|
|
print("-" * 50)
|
|
|
for command_name, source in test_commands:
|
|
|
description = generate_command_description(command_name, source)
|
|
|
print(f"Command: {command_name} ({source})")
|
|
|
print(f"Description: {description}")
|
|
|
print("-" * 50)
|
|
|
|
|
|
|
|
|
print("\nInteractive Mode: Enter a command and source to get its description.")
|
|
|
print("Valid sources: cmd, linux, macos, vbscript")
|
|
|
print("Type 'exit' to quit.\n")
|
|
|
while True:
|
|
|
command_name = input("Enter command name (or 'exit' to quit): ").strip()
|
|
|
if command_name.lower() == "exit":
|
|
|
break
|
|
|
source = input("Enter source (e.g., cmd, linux, macos, vbscript): ").strip().lower()
|
|
|
|
|
|
valid_sources = ["cmd", "linux", "macos", "vbscript"]
|
|
|
if source not in valid_sources:
|
|
|
print(f"Invalid source. Please use one of: {', '.join(valid_sources)}")
|
|
|
continue
|
|
|
description = generate_command_description(command_name, source)
|
|
|
print(f"\nCommand: {command_name} ({source})")
|
|
|
print(f"Description: {description}")
|
|
|
print("-" * 50)
|
|
|
|
|
|
print("Exiting interactive mode.") |