File size: 1,782 Bytes
8c43f19
31b1f3c
8c43f19
31b1f3c
ca19479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
780381d
ca19479
 
 
 
 
 
 
 
 
 
 
 
 
8c43f19
 
ca19479
 
 
 
 
 
 
31b1f3c
ca19479
8c43f19
ca19479
 
 
 
 
 
 
8c43f19
ca19479
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
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import json

def load_qwen_n8n_model():
    """Load the Qwen2.5-7B-n8n model with fallback tokenizer"""
    model_name = "npv2k1/Qwen2.5-7B-n8n"
    
    # Load tokenizer with fallback
    try:
        tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
    except:
        print("Using base Qwen tokenizer...")
        tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct", trust_remote_code=True)
    
    # Load model
    model = AutoModelForCausalLM.from_pretrained(
        model_name,
        torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
        device_map="auto" if torch.cuda.is_available() else None,
        trust_remote_code=True
    )
    
    return tokenizer, model

def generate_n8n_workflow(tokenizer, model, prompt, max_length=1024):
    """Generate n8n workflow from prompt"""
    formatted_prompt = f"Create an n8n workflow: {prompt}\n\nJSON:"
    
    inputs = tokenizer(formatted_prompt, return_tensors="pt", truncation=True)
    
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_length=max_length,
            temperature=0.7,
            do_sample=True,
            pad_token_id=tokenizer.eos_token_id
        )
    
    result = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    # Extract JSON
    json_start = result.find('{')
    if json_start != -1:
        return result[json_start:]
    return result

# Usage
if __name__ == "__main__":
    tokenizer, model = load_qwen_n8n_model()
    
    workflow = generate_n8n_workflow(
        tokenizer, 
        model, 
        "Send email when new GitHub issue is created"
    )
    
    print(workflow)