File size: 21,692 Bytes
6640b02 c57dc09 6640b02 10104ce c57dc09 10104ce c57dc09 6640b02 c57dc09 6640b02 91a333d c57dc09 91a333d c57dc09 91a333d 6640b02 c57dc09 6640b02 cf4d218 6640b02 cf4d218 6640b02 0f83a0b 6640b02 8a622dc 3ef49c3 8a622dc 3ef49c3 6640b02 0f83a0b 3ef49c3 0f83a0b 6640b02 3ef49c3 0f83a0b 6640b02 3ef49c3 6640b02 3ef49c3 6640b02 0f83a0b 3ef49c3 0f83a0b 3ef49c3 6640b02 3ef49c3 6640b02 3ef49c3 6640b02 3ef49c3 6640b02 3ef49c3 6640b02 91a333d e45a2cc 6640b02 e45a2cc 6640b02 e45a2cc 6640b02 e45a2cc 6640b02 91a333d 6640b02 e45a2cc 6640b02 0f83a0b 6640b02 0f83a0b 6640b02 c57dc09 |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 |
"""
n8n Workflow Generator - Gradio Web Interface
Deploy this to Hugging Face Spaces
"""
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
import torch
import json
import re
# ==============================================================================
# CONFIGURATION
# ==============================================================================
MODEL_REPO = "Nishan30/n8n-workflow-generator" # Update with your HF repo
BASE_MODEL = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
# Memory optimization: Set to True for 8-bit quantization (uses less memory but slower)
USE_8BIT = False # Change to True if you get out-of-memory errors
# ==============================================================================
# MODEL LOADING
# ==============================================================================
def load_model():
"""Load model once and cache it"""
print("Loading model...")
# Prepare model loading kwargs with disk offloading for limited memory
model_kwargs = {
"device_map": "auto",
"trust_remote_code": True,
"low_cpu_mem_usage": True,
"offload_folder": "offload", # Enable disk offloading for HF Space
}
# Use 8-bit quantization if enabled (saves memory)
if USE_8BIT:
print("Using 8-bit quantization for memory efficiency...")
model_kwargs["load_in_8bit"] = True
else:
model_kwargs["torch_dtype"] = torch.float16
# Load base model with memory optimization
base_model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
**model_kwargs
)
# Load LoRA adapter with error handling for unsupported parameters
try:
model = PeftModel.from_pretrained(
base_model,
MODEL_REPO,
)
except TypeError as e:
if "unexpected keyword argument" in str(e):
print(f"β οΈ Warning: {e}")
print("Attempting to load with filtered config...")
# Download and modify config
from huggingface_hub import hf_hub_download
import tempfile
import shutil
config_path = hf_hub_download(repo_id=MODEL_REPO, filename="adapter_config.json")
with open(config_path, 'r') as f:
config = json.load(f)
# Remove unsupported parameters
unsupported_params = ['alora_invocation_tokens', 'alora_invocation_token_ids']
for param in unsupported_params:
if param in config:
print(f"Removing unsupported parameter: {param}")
del config[param]
# Save modified config to temp directory
temp_dir = tempfile.mkdtemp()
temp_config_path = f"{temp_dir}/adapter_config.json"
with open(temp_config_path, 'w') as f:
json.dump(config, f, indent=2)
# Copy other adapter files
for filename in ['adapter_model.safetensors', 'adapter_model.bin']:
try:
src = hf_hub_download(repo_id=MODEL_REPO, filename=filename)
shutil.copy(src, f"{temp_dir}/{filename}")
break
except:
continue
# Load from temp directory
model = PeftModel.from_pretrained(
base_model,
temp_dir,
)
# Cleanup
shutil.rmtree(temp_dir)
else:
raise
tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO)
# Set pad token if not present
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
print("Model loaded successfully!")
return model, tokenizer
# Load model at startup (global variable for caching)
print("π Loading model at startup...")
model, tokenizer = load_model()
print("β
Model loaded and ready!")
# ==============================================================================
# CODE GENERATION
# ==============================================================================
def generate_workflow(prompt, temperature=0.5, max_tokens=1024):
"""Generate n8n workflow code from prompt"""
if not prompt.strip():
return "Please enter a workflow description.", None, None
# EXACT TRAINING PROMPT - No modifications!
formatted_prompt = f"""### System:
You are an expert n8n workflow generator. n8n is a powerful workflow automation tool that connects various services and APIs.
Your task is to generate TypeScript DSL code for n8n workflows based on user requests.
## Available n8n Nodes:
### TRIGGERS (Start workflows):
- n8n-nodes-base.webhook - Receives HTTP requests
- n8n-nodes-base.scheduleTrigger - Runs workflows on schedule (cron)
- n8n-nodes-base.manualTrigger - Manually triggered workflows
- n8n-nodes-base.formTrigger - Creates forms to collect data
- n8n-nodes-base.emailTrigger - Triggered by incoming emails
### ACTIONS (Send data/notifications):
- n8n-nodes-base.slack - Send messages to Slack channels
- n8n-nodes-base.gmail - Send emails via Gmail
- n8n-nodes-base.email - Send emails via SMTP
- n8n-nodes-base.discord - Send messages to Discord
- n8n-nodes-base.telegram - Send messages via Telegram
- n8n-nodes-base.httpRequest - Make HTTP API calls
- n8n-nodes-base.googleSheets - Read/write Google Sheets
- n8n-nodes-base.airtable - Interact with Airtable
- n8n-nodes-base.notion - Create/update Notion pages
### DATA PROCESSING:
- n8n-nodes-base.if - Conditional routing (if/else logic)
- n8n-nodes-base.switch - Multi-way branching
- n8n-nodes-base.set - Transform/set data fields
- n8n-nodes-base.filter - Filter items based on conditions
- n8n-nodes-base.merge - Merge data from multiple sources
- n8n-nodes-base.split - Split data into multiple items
- n8n-nodes-base.aggregate - Aggregate/group data
- n8n-nodes-base.sort - Sort items
### UTILITIES:
- n8n-nodes-base.code - Execute custom JavaScript/Python
- n8n-nodes-base.function - Run custom functions
- n8n-nodes-base.wait - Add delays to workflows
- n8n-nodes-base.noOp - No operation (placeholder)
- n8n-nodes-base.stopAndError - Stop workflow with error
## DSL Syntax:
```typescript
const workflow = new Workflow('Workflow Name');
// Add nodes
const triggerNode = workflow.add('n8n-nodes-base.webhook', {{
path: '/webhook-path',
method: 'POST'
}});
const actionNode = workflow.add('n8n-nodes-base.slack', {{
channel: '#general',
text: 'Message text'
}});
// Connect nodes
triggerNode.to(actionNode);
```
## Guidelines:
1. Always start with a trigger node
2. Use descriptive workflow names
3. Connect nodes logically
4. Include proper parameters for each node
5. Only use nodes from the list above
6. Keep workflows clean and maintainable
Generate ONLY the TypeScript DSL code, wrapped in ```typescript code blocks.
### Instruction:
{prompt}
### Response:
"""
# Debug: Print formatted prompt (first 500 chars)
print(f"\n{'='*60}")
print(f"User Prompt: {prompt}")
print(f"Formatted Input (truncated):\n{formatted_prompt[:500]}...")
print(f"{'='*60}\n")
# Tokenize
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
input_length = inputs.input_ids.shape[1]
print(f"Input tokens: {input_length}, Max new tokens: {max_tokens}")
# Generate with parameters matching training
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_tokens,
temperature=max(temperature, 0.1),
do_sample=True,
top_p=0.95,
top_k=50,
repetition_penalty=1.1,
eos_token_id=tokenizer.eos_token_id,
pad_token_id=tokenizer.pad_token_id,
)
# Decode
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Debug: Print generated text
print(f"Generated text length: {len(generated_text)} chars")
print(f"Generated text (first 500 chars):\n{generated_text[:500]}...\n")
# Extract code from response (handle ### Response: format)
code = extract_code_from_instruction_format(generated_text)
# Convert to n8n JSON
n8n_json = convert_to_n8n_json(code)
# Create visualization
visualization = create_visualization(n8n_json)
return code, json.dumps(n8n_json, indent=2), visualization
def extract_code_from_instruction_format(text):
"""Extract TypeScript code from ### Response: format"""
# Split by ### Response: and get the part after it
try:
response_part = text.split("### Response:")[-1].strip()
except:
response_part = text
# Remove any subsequent ### markers (like ### Instruction:, ### System:)
for stop_marker in ["### Instruction:", "### System:", "\n\n\n\n"]:
if stop_marker in response_part:
response_part = response_part.split(stop_marker)[0].strip()
# Try to extract code from markdown blocks
code_match = re.search(r'```(?:typescript|ts)?\n(.*?)```', response_part, re.DOTALL)
if code_match:
return code_match.group(1).strip()
# Remove markdown code block markers if present
response_part = re.sub(r'```(?:typescript|ts)?', '', response_part)
return response_part.strip()
def extract_code(text):
"""Legacy extraction function - kept for compatibility"""
return extract_code_from_instruction_format(text)
# ==============================================================================
# N8N JSON CONVERSION
# ==============================================================================
def parse_js_object(js_obj_str):
"""Convert JavaScript object notation to Python dict"""
if not js_obj_str or js_obj_str.strip() == "{}":
return {}
try:
# First try direct JSON parsing
return json.loads(js_obj_str)
except:
pass
try:
# Convert JS object notation to JSON
# Replace single quotes with double quotes
json_str = js_obj_str.replace("'", '"')
# Add quotes around unquoted keys (e.g., {path: "data"} -> {"path": "data"})
json_str = re.sub(r'(\w+):', r'"\1":', json_str)
# Parse the JSON
return json.loads(json_str)
except Exception as e:
print(f"Warning: Could not parse parameters '{js_obj_str}': {e}")
return {}
def extract_balanced_braces(text, start_pos):
"""Extract content within balanced braces starting at start_pos"""
if start_pos >= len(text) or text[start_pos] != '{':
return None
brace_count = 0
in_string = False
escape_next = False
string_char = None
for i in range(start_pos, len(text)):
char = text[i]
if escape_next:
escape_next = False
continue
if char == '\\':
escape_next = True
continue
if char in ('"', "'") and not in_string:
in_string = True
string_char = char
elif char == string_char and in_string:
in_string = False
string_char = None
elif char == '{' and not in_string:
brace_count += 1
elif char == '}' and not in_string:
brace_count -= 1
if brace_count == 0:
return text[start_pos:i+1]
return None
def convert_to_n8n_json(typescript_code):
"""Convert TypeScript DSL to n8n JSON format"""
nodes = []
connections = {}
workflow_name = "Generated Workflow"
# Extract workflow name
name_match = re.search(r"new Workflow\(['\"](.*?)['\"]\)", typescript_code)
if name_match:
workflow_name = name_match.group(1)
# Extract node definitions - find all workflow.add() calls
node_pattern = r'const\s+(\w+)\s*=\s*workflow\.add\([\'"]([^\'\"]+)[\'"]'
node_map = {} # variable name -> node id
position_y = 250
position_x = 300
for match in re.finditer(node_pattern, typescript_code):
var_name = match.group(1)
node_type = match.group(2)
# Look for parameters after the node type
params_str = "{}"
remaining_text = typescript_code[match.end():]
# Check if there's a comma followed by parameters
comma_match = re.match(r'\s*,\s*', remaining_text)
if comma_match:
param_start = match.end() + comma_match.end()
if param_start < len(typescript_code) and typescript_code[param_start] == '{':
params_str = extract_balanced_braces(typescript_code, param_start)
if params_str is None:
params_str = "{}"
# Convert JavaScript object notation to valid JSON
parameters = parse_js_object(params_str)
node_id = str(len(nodes))
node_map[var_name] = node_id
nodes.append({
"id": node_id,
"name": var_name,
"type": node_type,
"typeVersion": 1,
"position": [position_x, position_y],
"parameters": parameters
})
position_x += 300
# Extract connections
connection_pattern = r'(\w+)\.to\((\w+)\)'
connection_matches = re.finditer(connection_pattern, typescript_code)
for match in connection_matches:
source_var = match.group(1)
target_var = match.group(2)
if source_var in node_map and target_var in node_map:
source_id = node_map[source_var]
target_id = node_map[target_var]
# Find source node name
source_node = next((n for n in nodes if n["id"] == source_id), None)
if source_node:
source_name = source_node["name"]
if source_name not in connections:
connections[source_name] = {"main": [[]] }
connections[source_name]["main"][0].append({
"node": target_var,
"type": "main",
"index": 0
})
return {
"name": workflow_name,
"nodes": nodes,
"connections": connections,
"active": False,
"settings": {}
}
# ==============================================================================
# VISUALIZATION
# ==============================================================================
def create_visualization(n8n_json):
"""Create HTML visualization of the workflow"""
nodes = n8n_json.get("nodes", [])
connections = n8n_json.get("connections", {})
if not nodes:
return "<div style='padding:20px;text-align:center;color:#666;'>No nodes found in workflow</div>"
html = """
<div style="font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; border-radius: 8px;">
<h3 style="margin-top:0; color: #ff6d5a;">π Workflow Visualization</h3>
<div style="display: flex; flex-direction: column; gap: 15px;">
"""
# Display nodes
for i, node in enumerate(nodes):
node_name = node.get("name", f"Node{i}")
node_type = node.get("type", "unknown").split(".")[-1]
params = node.get("parameters", {})
# Count outgoing connections
outgoing = 0
for source, conns in connections.items():
if source == node_name:
outgoing = len(conns.get("main", [[]])[0])
# Node card
html += f"""
<div style="background: white; padding: 15px; border-radius: 8px; border-left: 4px solid #ff6d5a; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
<div style="display: flex; justify-content: space-between; align-items: center;">
<div>
<div style="font-weight: bold; font-size: 16px; color: #333;">{node_name}</div>
<div style="color: #666; font-size: 14px; margin-top: 4px;">
<code style="background: #f0f0f0; padding: 2px 6px; border-radius: 3px;">{node_type}</code>
</div>
</div>
<div style="text-align: right; color: #999; font-size: 12px;">
Node #{i+1}
</div>
</div>
"""
# Show key parameters
if params:
html += "<div style='margin-top: 10px; font-size: 13px; color: #555;'>"
html += "<strong>Parameters:</strong><br>"
for key, value in list(params.items())[:3]: # Show first 3 params
value_str = str(value)[:50]
html += f" β’ {key}: <code style='background:#f9f9f9;padding:1px 4px;'>{value_str}</code><br>"
html += "</div>"
# Show connections
if outgoing > 0:
html += f"<div style='margin-top: 8px; color: #4CAF50; font-size: 12px;'>β {outgoing} connection(s)</div>"
html += "</div>"
# Show arrow between nodes
if i < len(nodes) - 1:
html += "<div style='text-align: center; color: #999; font-size: 20px;'>β</div>"
html += """
</div>
<div style="margin-top: 15px; padding: 10px; background: #e3f2fd; border-radius: 4px; font-size: 12px; color: #1976d2;">
π‘ <strong>Tip:</strong> Copy the n8n JSON and import it directly into your n8n instance!
</div>
</div>
"""
return html
# ==============================================================================
# GRADIO INTERFACE
# ==============================================================================
def create_ui():
"""Create Gradio interface"""
with gr.Blocks(title="n8n Workflow Generator", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# π n8n Workflow Generator
Generate n8n workflows using natural language! Powered by fine-tuned **Qwen2.5-Coder-1.5B**.
### How to use:
1. Describe your workflow in plain English
2. Click "Generate Workflow"
3. Copy the generated code or n8n JSON
4. Import into your n8n instance
""")
with gr.Row():
with gr.Column(scale=1):
prompt_input = gr.Textbox(
label="Workflow Description",
placeholder="Example: Create a webhook that receives data, filters active users, and sends to Slack",
lines=3
)
with gr.Row():
temperature = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.5,
step=0.1,
label="Temperature (creativity)",
info="Lower = more consistent, Higher = more creative"
)
max_tokens = gr.Slider(
minimum=256,
maximum=2048,
value=1024,
step=128,
label="Max tokens",
info="Maximum length of generated code"
)
generate_btn = gr.Button("π― Generate Workflow", variant="primary", size="lg")
gr.Markdown("""
### π Example Prompts:
- *Create a webhook that sends data to Slack*
- *Schedule that runs daily and backs up database to Google Drive*
- *Webhook receives form data, validates email, saves to Airtable*
- *Monitor RSS feed and post new items to Twitter*
""")
with gr.Column(scale=1):
visualization_output = gr.HTML(label="Visual Workflow")
with gr.Row():
with gr.Column():
code_output = gr.Code(
label="Generated TypeScript Code",
language="typescript",
lines=15
)
with gr.Column():
json_output = gr.Code(
label="n8n JSON (import this into n8n)",
language="json",
lines=15
)
# Examples
gr.Examples(
examples=[
["Create a webhook that sends data to Slack"],
["Build a workflow that fetches GitHub issues and sends daily summary email"],
["Webhook receives order, if amount > $1000 send to priority queue, else standard processing"],
["Schedule that runs every Monday, fetches data from API, transforms it, and updates Google Sheets"],
["Monitor RSS feeds, remove duplicates, and post to Twitter"],
],
inputs=prompt_input
)
# Event handler
generate_btn.click(
fn=generate_workflow,
inputs=[prompt_input, temperature, max_tokens],
outputs=[code_output, json_output, visualization_output]
)
gr.Markdown("""
---
### βΉοΈ About
This model achieved **92.4% accuracy** on diverse n8n workflow generation tasks.
**Model:** Fine-tuned Qwen2.5-Coder-1.5B with LoRA
**Training:** 247 curated workflow examples
**Performance:** Production-ready quality
[π€ Model Card](https://huggingface.co/{}) | [π GitHub](https://github.com/yourusername/n8n-generator)
""".format(MODEL_REPO))
return demo
# ==============================================================================
# LAUNCH
# ==============================================================================
if __name__ == "__main__":
demo = create_ui()
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)
|