Spaces:
Runtime error
Runtime error
| 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) |