Update app.py
Browse files
app.py
CHANGED
|
@@ -52,8 +52,53 @@ def load_model():
|
|
| 52 |
trust_remote_code=True
|
| 53 |
)
|
| 54 |
|
| 55 |
-
# Load LoRA adapter
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO)
|
| 58 |
|
| 59 |
print("Model loaded successfully!")
|
|
@@ -139,6 +184,31 @@ def extract_code(text):
|
|
| 139 |
# N8N JSON CONVERSION
|
| 140 |
# ==============================================================================
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
def convert_to_n8n_json(typescript_code):
|
| 143 |
"""Convert TypeScript DSL to n8n JSON format"""
|
| 144 |
|
|
@@ -151,8 +221,8 @@ def convert_to_n8n_json(typescript_code):
|
|
| 151 |
if name_match:
|
| 152 |
workflow_name = name_match.group(1)
|
| 153 |
|
| 154 |
-
# Extract node definitions
|
| 155 |
-
node_pattern = r'const\s+(\w+)\s*=\s*workflow\.add\([\'"]([^\'\"]+)[\'"](?:,\s*({[^}]
|
| 156 |
node_matches = re.finditer(node_pattern, typescript_code)
|
| 157 |
|
| 158 |
node_map = {} # variable name -> node id
|
|
@@ -164,11 +234,8 @@ def convert_to_n8n_json(typescript_code):
|
|
| 164 |
node_type = match.group(2)
|
| 165 |
params_str = match.group(3) if match.group(3) else "{}"
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
|
| 169 |
-
parameters = json.loads(params_str)
|
| 170 |
-
except:
|
| 171 |
-
parameters = {}
|
| 172 |
|
| 173 |
node_id = str(i)
|
| 174 |
node_map[var_name] = node_id
|
|
|
|
| 52 |
trust_remote_code=True
|
| 53 |
)
|
| 54 |
|
| 55 |
+
# Load LoRA adapter with error handling for unsupported parameters
|
| 56 |
+
try:
|
| 57 |
+
model = PeftModel.from_pretrained(base_model, MODEL_REPO)
|
| 58 |
+
except TypeError as e:
|
| 59 |
+
if "unexpected keyword argument" in str(e):
|
| 60 |
+
print(f"⚠️ Warning: {e}")
|
| 61 |
+
print("Attempting to load with filtered config...")
|
| 62 |
+
|
| 63 |
+
# Download and modify config
|
| 64 |
+
from huggingface_hub import hf_hub_download
|
| 65 |
+
import tempfile
|
| 66 |
+
import shutil
|
| 67 |
+
|
| 68 |
+
config_path = hf_hub_download(repo_id=MODEL_REPO, filename="adapter_config.json")
|
| 69 |
+
with open(config_path, 'r') as f:
|
| 70 |
+
config = json.load(f)
|
| 71 |
+
|
| 72 |
+
# Remove unsupported parameters
|
| 73 |
+
unsupported_params = ['alora_invocation_tokens', 'alora_invocation_token_ids']
|
| 74 |
+
for param in unsupported_params:
|
| 75 |
+
if param in config:
|
| 76 |
+
print(f"Removing unsupported parameter: {param}")
|
| 77 |
+
del config[param]
|
| 78 |
+
|
| 79 |
+
# Save modified config to temp directory
|
| 80 |
+
temp_dir = tempfile.mkdtemp()
|
| 81 |
+
temp_config_path = f"{temp_dir}/adapter_config.json"
|
| 82 |
+
with open(temp_config_path, 'w') as f:
|
| 83 |
+
json.dump(config, f, indent=2)
|
| 84 |
+
|
| 85 |
+
# Copy other adapter files
|
| 86 |
+
for filename in ['adapter_model.safetensors', 'adapter_model.bin']:
|
| 87 |
+
try:
|
| 88 |
+
src = hf_hub_download(repo_id=MODEL_REPO, filename=filename)
|
| 89 |
+
shutil.copy(src, f"{temp_dir}/{filename}")
|
| 90 |
+
break
|
| 91 |
+
except:
|
| 92 |
+
continue
|
| 93 |
+
|
| 94 |
+
# Load from temp directory
|
| 95 |
+
model = PeftModel.from_pretrained(base_model, temp_dir)
|
| 96 |
+
|
| 97 |
+
# Cleanup
|
| 98 |
+
shutil.rmtree(temp_dir)
|
| 99 |
+
else:
|
| 100 |
+
raise
|
| 101 |
+
|
| 102 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO)
|
| 103 |
|
| 104 |
print("Model loaded successfully!")
|
|
|
|
| 184 |
# N8N JSON CONVERSION
|
| 185 |
# ==============================================================================
|
| 186 |
|
| 187 |
+
def parse_js_object(js_obj_str):
|
| 188 |
+
"""Convert JavaScript object notation to Python dict"""
|
| 189 |
+
if not js_obj_str or js_obj_str.strip() == "{}":
|
| 190 |
+
return {}
|
| 191 |
+
|
| 192 |
+
try:
|
| 193 |
+
# First try direct JSON parsing
|
| 194 |
+
return json.loads(js_obj_str)
|
| 195 |
+
except:
|
| 196 |
+
pass
|
| 197 |
+
|
| 198 |
+
try:
|
| 199 |
+
# Convert JS object notation to JSON
|
| 200 |
+
# Replace single quotes with double quotes
|
| 201 |
+
json_str = js_obj_str.replace("'", '"')
|
| 202 |
+
|
| 203 |
+
# Add quotes around unquoted keys (e.g., {path: "data"} -> {"path": "data"})
|
| 204 |
+
json_str = re.sub(r'(\w+):', r'"\1":', json_str)
|
| 205 |
+
|
| 206 |
+
# Parse the JSON
|
| 207 |
+
return json.loads(json_str)
|
| 208 |
+
except Exception as e:
|
| 209 |
+
print(f"Warning: Could not parse parameters '{js_obj_str}': {e}")
|
| 210 |
+
return {}
|
| 211 |
+
|
| 212 |
def convert_to_n8n_json(typescript_code):
|
| 213 |
"""Convert TypeScript DSL to n8n JSON format"""
|
| 214 |
|
|
|
|
| 221 |
if name_match:
|
| 222 |
workflow_name = name_match.group(1)
|
| 223 |
|
| 224 |
+
# Extract node definitions with improved parameter parsing
|
| 225 |
+
node_pattern = r'const\s+(\w+)\s*=\s*workflow\.add\([\'"]([^\'\"]+)[\'"](?:,\s*(\{[^}]*\}))?\)'
|
| 226 |
node_matches = re.finditer(node_pattern, typescript_code)
|
| 227 |
|
| 228 |
node_map = {} # variable name -> node id
|
|
|
|
| 234 |
node_type = match.group(2)
|
| 235 |
params_str = match.group(3) if match.group(3) else "{}"
|
| 236 |
|
| 237 |
+
# Convert JavaScript object notation to valid JSON
|
| 238 |
+
parameters = parse_js_object(params_str)
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
node_id = str(i)
|
| 241 |
node_map[var_name] = node_id
|