Spaces:
Running
Running
restricted to domain specific only
Browse files
app.py
CHANGED
|
@@ -8,6 +8,8 @@ print("Loading model...")
|
|
| 8 |
BASE_MODEL = "Qwen/Qwen2.5-Coder-1.5B"
|
| 9 |
LORA_MODEL = "orca99/auto-mininet-qwen"
|
| 10 |
|
|
|
|
|
|
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
| 12 |
tokenizer.pad_token = tokenizer.eos_token
|
| 13 |
|
|
@@ -15,17 +17,58 @@ base = AutoModelForCausalLM.from_pretrained(
|
|
| 15 |
BASE_MODEL,
|
| 16 |
torch_dtype=torch.float32,
|
| 17 |
trust_remote_code=True
|
| 18 |
-
)
|
| 19 |
-
|
|
|
|
| 20 |
model.eval()
|
|
|
|
|
|
|
| 21 |
print("β
Model ready")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def generate(instruction, temperature, max_tokens):
|
| 24 |
if not instruction.strip():
|
| 25 |
return "β οΈ Please enter a topology description."
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
with torch.no_grad():
|
| 31 |
outputs = model.generate(
|
|
@@ -40,10 +83,8 @@ def generate(instruction, temperature, max_tokens):
|
|
| 40 |
code = result.split("### Response:")[-1].strip()
|
| 41 |
|
| 42 |
# ββ Always fix the topos line βββββββββββββββββββββββββββββ
|
| 43 |
-
# Remove any existing broken topos line
|
| 44 |
code = re.sub(r"\ntopos\s*=.*", "", code).strip()
|
| 45 |
|
| 46 |
-
# Find the class name
|
| 47 |
match = re.search(r"class (\w+)\(Topo\)", code)
|
| 48 |
if match:
|
| 49 |
class_name = match.group(1)
|
|
@@ -55,13 +96,13 @@ examples = [
|
|
| 55 |
["Create a simple topology with 2 hosts and 1 switch.", 0.2, 512],
|
| 56 |
["Create a linear topology with 3 switches and 2 hosts per switch.", 0.2, 512],
|
| 57 |
["Build a tree topology with depth 2 and fanout 3.", 0.2, 512],
|
| 58 |
-
["Create a topology with 3 routers using
|
| 59 |
]
|
| 60 |
|
| 61 |
with gr.Blocks(title="Auto Mininet", theme=gr.themes.Soft()) as demo:
|
| 62 |
gr.Markdown("""
|
| 63 |
# π Auto Mininet
|
| 64 |
-
### Convert plain English β Executable Mininet Python Script
|
| 65 |
*Fine-tuned Qwen2.5-Coder-1.5B with LoRA*
|
| 66 |
""")
|
| 67 |
|
|
@@ -87,13 +128,4 @@ with gr.Blocks(title="Auto Mininet", theme=gr.themes.Soft()) as demo:
|
|
| 87 |
gr.Markdown("### π‘ How to run the generated script:")
|
| 88 |
gr.Markdown("""
|
| 89 |
```bash
|
| 90 |
-
|
| 91 |
-
sudo mn --custom topo.py --topo custom
|
| 92 |
-
```
|
| 93 |
-
The topo name is always **custom** β same command every time.
|
| 94 |
-
""")
|
| 95 |
-
|
| 96 |
-
btn.click(fn=generate, inputs=[instruction, temperature, max_tokens], outputs=output)
|
| 97 |
-
gr.Examples(examples=examples, inputs=[instruction, temperature, max_tokens])
|
| 98 |
-
|
| 99 |
-
demo.launch()
|
|
|
|
| 8 |
BASE_MODEL = "Qwen/Qwen2.5-Coder-1.5B"
|
| 9 |
LORA_MODEL = "orca99/auto-mininet-qwen"
|
| 10 |
|
| 11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
| 14 |
tokenizer.pad_token = tokenizer.eos_token
|
| 15 |
|
|
|
|
| 17 |
BASE_MODEL,
|
| 18 |
torch_dtype=torch.float32,
|
| 19 |
trust_remote_code=True
|
| 20 |
+
).to(device)
|
| 21 |
+
|
| 22 |
+
model = PeftModel.from_pretrained(base, LORA_MODEL).to(device)
|
| 23 |
model.eval()
|
| 24 |
+
model.requires_grad_(False)
|
| 25 |
+
|
| 26 |
print("β
Model ready")
|
| 27 |
|
| 28 |
+
# π SYSTEM PROMPT (CRITICAL)
|
| 29 |
+
SYSTEM_PROMPT = """
|
| 30 |
+
You are Auto Mininet, a specialized assistant.
|
| 31 |
+
You ONLY generate executable Mininet Python topology scripts.
|
| 32 |
+
|
| 33 |
+
Rules:
|
| 34 |
+
- ONLY output Python code
|
| 35 |
+
- ONLY use Mininet APIs (Topo, Mininet, TCLink, OVSSwitch, Host, Controller)
|
| 36 |
+
- NEVER answer general questions
|
| 37 |
+
- If input is not about Mininet or network topologies, respond with:
|
| 38 |
+
"ERROR: This tool only supports Mininet topology generation."
|
| 39 |
+
|
| 40 |
+
The output MUST contain:
|
| 41 |
+
- a class that inherits from Topo
|
| 42 |
+
- valid Mininet links and nodes
|
| 43 |
+
- NO explanations, NO markdown, NO comments outside code
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
# Keywords that indicate a valid networking request
|
| 47 |
+
NETWORK_KEYWORDS = [
|
| 48 |
+
"topology", "mininet", "switch", "host", "router",
|
| 49 |
+
"link", "tree", "linear", "fat-tree", "tc", "bandwidth", "delay"
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
def is_network_query(text: str) -> bool:
|
| 53 |
+
text = text.lower()
|
| 54 |
+
return any(k in text for k in NETWORK_KEYWORDS)
|
| 55 |
+
|
| 56 |
def generate(instruction, temperature, max_tokens):
|
| 57 |
if not instruction.strip():
|
| 58 |
return "β οΈ Please enter a topology description."
|
| 59 |
|
| 60 |
+
# β Hard reject non-network queries
|
| 61 |
+
if not is_network_query(instruction):
|
| 62 |
+
return "ERROR: This tool only supports Mininet topology generation."
|
| 63 |
+
|
| 64 |
+
prompt = (
|
| 65 |
+
SYSTEM_PROMPT.strip()
|
| 66 |
+
+ "\n\n### Instruction:\n"
|
| 67 |
+
+ instruction
|
| 68 |
+
+ "\n\n### Response:\n"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 72 |
|
| 73 |
with torch.no_grad():
|
| 74 |
outputs = model.generate(
|
|
|
|
| 83 |
code = result.split("### Response:")[-1].strip()
|
| 84 |
|
| 85 |
# ββ Always fix the topos line βββββββββββββββββββββββββββββ
|
|
|
|
| 86 |
code = re.sub(r"\ntopos\s*=.*", "", code).strip()
|
| 87 |
|
|
|
|
| 88 |
match = re.search(r"class (\w+)\(Topo\)", code)
|
| 89 |
if match:
|
| 90 |
class_name = match.group(1)
|
|
|
|
| 96 |
["Create a simple topology with 2 hosts and 1 switch.", 0.2, 512],
|
| 97 |
["Create a linear topology with 3 switches and 2 hosts per switch.", 0.2, 512],
|
| 98 |
["Build a tree topology with depth 2 and fanout 3.", 0.2, 512],
|
| 99 |
+
["Create a topology with 3 routers using TCLinks and bandwidth limits.", 0.2, 512],
|
| 100 |
]
|
| 101 |
|
| 102 |
with gr.Blocks(title="Auto Mininet", theme=gr.themes.Soft()) as demo:
|
| 103 |
gr.Markdown("""
|
| 104 |
# π Auto Mininet
|
| 105 |
+
### Convert plain English β Executable Mininet Python Script
|
| 106 |
*Fine-tuned Qwen2.5-Coder-1.5B with LoRA*
|
| 107 |
""")
|
| 108 |
|
|
|
|
| 128 |
gr.Markdown("### π‘ How to run the generated script:")
|
| 129 |
gr.Markdown("""
|
| 130 |
```bash
|
| 131 |
+
sudo mn --custom topo.py --topo custom
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|