Update app.py
Browse files
app.py
CHANGED
|
@@ -17,23 +17,24 @@ import re
|
|
| 17 |
MODEL_REPO = "Nishan30/n8n-workflow-generator" # Update with your HF repo
|
| 18 |
BASE_MODEL = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
|
| 19 |
|
| 20 |
-
SYSTEM_PROMPT = """You are an expert n8n workflow generator. Given a user's request, you generate clean, functional TypeScript code using the @n8n-generator/core DSL.
|
|
|
|
|
|
|
| 21 |
|
| 22 |
Your output should:
|
| 23 |
- Only contain the code, no explanations
|
| 24 |
- Use the Workflow class from @n8n-generator/core
|
| 25 |
-
- Use workflow.add() to create nodes
|
| 26 |
- Use .to() or workflow.connect() for connections
|
|
|
|
| 27 |
- Be ready to compile directly to n8n JSON
|
| 28 |
|
| 29 |
-
|
| 30 |
-
User: "Create a webhook that sends data to Slack"
|
| 31 |
-
Assistant:
|
| 32 |
```typescript
|
| 33 |
-
const workflow = new Workflow('
|
| 34 |
-
const
|
| 35 |
-
const
|
| 36 |
-
|
| 37 |
```"""
|
| 38 |
|
| 39 |
# ==============================================================================
|
|
@@ -113,7 +114,7 @@ print("✅ Model loaded and ready!")
|
|
| 113 |
# CODE GENERATION
|
| 114 |
# ==============================================================================
|
| 115 |
|
| 116 |
-
def generate_workflow(prompt, temperature=0.
|
| 117 |
"""Generate n8n workflow code from prompt"""
|
| 118 |
|
| 119 |
if not prompt.strip():
|
|
@@ -132,23 +133,37 @@ def generate_workflow(prompt, temperature=0.3, max_tokens=512):
|
|
| 132 |
add_generation_prompt=True
|
| 133 |
)
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
# Tokenize
|
| 136 |
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
# Generate
|
| 139 |
with torch.no_grad():
|
| 140 |
outputs = model.generate(
|
| 141 |
**inputs,
|
| 142 |
max_new_tokens=max_tokens,
|
| 143 |
-
temperature=temperature,
|
| 144 |
-
do_sample=True
|
| 145 |
top_p=0.9,
|
| 146 |
-
|
|
|
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
# Decode
|
| 150 |
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
# Extract code from response
|
| 153 |
code = extract_code(generated_text)
|
| 154 |
|
|
@@ -394,15 +409,15 @@ def create_ui():
|
|
| 394 |
temperature = gr.Slider(
|
| 395 |
minimum=0.0,
|
| 396 |
maximum=1.0,
|
| 397 |
-
value=0.
|
| 398 |
step=0.1,
|
| 399 |
label="Temperature (creativity)",
|
| 400 |
info="Lower = more consistent, Higher = more creative"
|
| 401 |
)
|
| 402 |
max_tokens = gr.Slider(
|
| 403 |
-
minimum=
|
| 404 |
-
maximum=
|
| 405 |
-
value=
|
| 406 |
step=128,
|
| 407 |
label="Max tokens",
|
| 408 |
info="Maximum length of generated code"
|
|
|
|
| 17 |
MODEL_REPO = "Nishan30/n8n-workflow-generator" # Update with your HF repo
|
| 18 |
BASE_MODEL = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
|
| 19 |
|
| 20 |
+
SYSTEM_PROMPT = """You are an expert n8n workflow generator. Given a user's request, you MUST generate clean, functional TypeScript code that EXACTLY matches their specific requirements using the @n8n-generator/core DSL.
|
| 21 |
+
|
| 22 |
+
CRITICAL: Generate code based ONLY on the user's request. Do NOT use example workflows. Create the workflow the user asks for.
|
| 23 |
|
| 24 |
Your output should:
|
| 25 |
- Only contain the code, no explanations
|
| 26 |
- Use the Workflow class from @n8n-generator/core
|
| 27 |
+
- Use workflow.add() to create nodes with appropriate parameters
|
| 28 |
- Use .to() or workflow.connect() for connections
|
| 29 |
+
- Match the user's specific requirements exactly
|
| 30 |
- Be ready to compile directly to n8n JSON
|
| 31 |
|
| 32 |
+
Format:
|
|
|
|
|
|
|
| 33 |
```typescript
|
| 34 |
+
const workflow = new Workflow('Descriptive Name');
|
| 35 |
+
const node1 = workflow.add('n8n-nodes-base.nodetype', { param: 'value' });
|
| 36 |
+
const node2 = workflow.add('n8n-nodes-base.nodetype', { param: 'value' });
|
| 37 |
+
node1.to(node2);
|
| 38 |
```"""
|
| 39 |
|
| 40 |
# ==============================================================================
|
|
|
|
| 114 |
# CODE GENERATION
|
| 115 |
# ==============================================================================
|
| 116 |
|
| 117 |
+
def generate_workflow(prompt, temperature=0.5, max_tokens=1024):
|
| 118 |
"""Generate n8n workflow code from prompt"""
|
| 119 |
|
| 120 |
if not prompt.strip():
|
|
|
|
| 133 |
add_generation_prompt=True
|
| 134 |
)
|
| 135 |
|
| 136 |
+
# Debug: Print formatted prompt (first 500 chars)
|
| 137 |
+
print(f"\n{'='*60}")
|
| 138 |
+
print(f"User Prompt: {prompt}")
|
| 139 |
+
print(f"Formatted Input (truncated):\n{text[:500]}...")
|
| 140 |
+
print(f"{'='*60}\n")
|
| 141 |
+
|
| 142 |
# Tokenize
|
| 143 |
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 144 |
+
input_length = inputs.input_ids.shape[1]
|
| 145 |
+
print(f"Input tokens: {input_length}, Max new tokens: {max_tokens}")
|
| 146 |
|
| 147 |
+
# Generate with better sampling parameters
|
| 148 |
with torch.no_grad():
|
| 149 |
outputs = model.generate(
|
| 150 |
**inputs,
|
| 151 |
max_new_tokens=max_tokens,
|
| 152 |
+
temperature=max(temperature, 0.1), # Ensure minimum temperature
|
| 153 |
+
do_sample=True, # Always sample for variety
|
| 154 |
top_p=0.9,
|
| 155 |
+
top_k=50, # Add top-k sampling
|
| 156 |
+
repetition_penalty=1.15, # Increase to reduce repetition
|
| 157 |
+
no_repeat_ngram_size=3 # Prevent repeating 3-grams
|
| 158 |
)
|
| 159 |
|
| 160 |
# Decode
|
| 161 |
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 162 |
|
| 163 |
+
# Debug: Print generated text
|
| 164 |
+
print(f"Generated text length: {len(generated_text)} chars")
|
| 165 |
+
print(f"Generated text (first 300 chars):\n{generated_text[:300]}...\n")
|
| 166 |
+
|
| 167 |
# Extract code from response
|
| 168 |
code = extract_code(generated_text)
|
| 169 |
|
|
|
|
| 409 |
temperature = gr.Slider(
|
| 410 |
minimum=0.0,
|
| 411 |
maximum=1.0,
|
| 412 |
+
value=0.5,
|
| 413 |
step=0.1,
|
| 414 |
label="Temperature (creativity)",
|
| 415 |
info="Lower = more consistent, Higher = more creative"
|
| 416 |
)
|
| 417 |
max_tokens = gr.Slider(
|
| 418 |
+
minimum=256,
|
| 419 |
+
maximum=2048,
|
| 420 |
+
value=1024,
|
| 421 |
step=128,
|
| 422 |
label="Max tokens",
|
| 423 |
info="Maximum length of generated code"
|