broadfield-dev commited on
Commit
37adb99
·
verified ·
1 Parent(s): 12021f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -6
app.py CHANGED
@@ -73,38 +73,56 @@ def parse_model_response(response_text: str) -> dict:
73
  if match:
74
  json_content = match.group(1).strip()
75
  files = json.loads(json_content)
76
- if not isinstance(files, dict) or "files" not in files:
77
  raise ValueError("Invalid JSON structure")
78
  socketio.emit('progress', {'percent': 90, 'message': 'JSON parsed'})
79
  return files
80
  else:
81
  app.logger.warning("No JSON found within START/STOP tokens, attempting custom parsing")
82
- # Fallback to the existing regex parsing
83
  pattern = r"### (.+?)\n```(?:\w+)?\n(.*?)\n```"
84
  matches = re.findall(pattern, response_text, re.DOTALL)
85
  if not matches:
86
- raise ValueError(f"Could not parse response into files. Response start: {response_text[:200]}") # Log response start
87
  files = {
88
  "files": {
89
  filename.strip(): {"content": content.strip()}
90
  for filename, content in matches
91
  }
92
  }
 
 
 
 
 
 
 
 
 
 
93
  socketio.emit('progress', {'percent': 90, 'message': 'Parsed with fallback method'})
94
  return files
95
 
96
  except json.JSONDecodeError:
97
- app.logger.warning(f"Response is not valid JSON, attempting custom parsing. Response start: {response_text[:200]}") # Log response start
98
  pattern = r"### (.+?)\n```(?:\w+)?\n(.*?)\n```"
99
  matches = re.findall(pattern, response_text, re.DOTALL)
100
  if not matches:
101
- raise ValueError(f"Could not parse response into files. Response start: {response_text[:200]}") # Log response start
102
  files = {
103
  "files": {
104
  filename.strip(): {"content": content.strip()}
105
- for filename, content in matches
106
  }
107
  }
 
 
 
 
 
 
 
 
 
108
  socketio.emit('progress', {'percent': 90, 'message': 'Parsed with fallback method'})
109
  return files
110
  except ValueError as e: # Catch ValueErrors too
 
73
  if match:
74
  json_content = match.group(1).strip()
75
  files = json.loads(json_content)
76
+ if not isinstance(files, dict) or "files" not in files or not isinstance(files["files"], dict):
77
  raise ValueError("Invalid JSON structure")
78
  socketio.emit('progress', {'percent': 90, 'message': 'JSON parsed'})
79
  return files
80
  else:
81
  app.logger.warning("No JSON found within START/STOP tokens, attempting custom parsing")
 
82
  pattern = r"### (.+?)\n```(?:\w+)?\n(.*?)\n```"
83
  matches = re.findall(pattern, response_text, re.DOTALL)
84
  if not matches:
85
+ raise ValueError(f"Could not parse response into files. Response start: {response_text[:200]}")
86
  files = {
87
  "files": {
88
  filename.strip(): {"content": content.strip()}
89
  for filename, content in matches
90
  }
91
  }
92
+ # Extract body content from index.html if present
93
+ index_html_content = files["files"].get("index.html", {}).get("content", "")
94
+ body_match = re.search(r"<body.*?>(.*?)</body>", index_html_content, re.DOTALL | re.IGNORECASE)
95
+ if body_match:
96
+ files["files"]["index.html"]["content"] = body_match.group(1).strip()
97
+ app.logger.info("Extracted body content from index.html")
98
+ else:
99
+ app.logger.warning("Could not extract body content from index.html, using full content.")
100
+
101
+
102
  socketio.emit('progress', {'percent': 90, 'message': 'Parsed with fallback method'})
103
  return files
104
 
105
  except json.JSONDecodeError:
106
+ app.logger.warning(f"Response is not valid JSON, attempting custom parsing. Response start: {response_text[:200]}")
107
  pattern = r"### (.+?)\n```(?:\w+)?\n(.*?)\n```"
108
  matches = re.findall(pattern, response_text, re.DOTALL)
109
  if not matches:
110
+ raise ValueError(f"Could not parse response into files. Response start: {response_text[:200]}")
111
  files = {
112
  "files": {
113
  filename.strip(): {"content": content.strip()}
114
+ for filename, content in matches
115
  }
116
  }
117
+ # Extract body content from index.html if present
118
+ index_html_content = files["files"].get("index.html", {}).get("content", "")
119
+ body_match = re.search(r"<body.*?>(.*?)</body>", index_html_content, re.DOTALL | re.IGNORECASE)
120
+ if body_match:
121
+ files["files"]["index.html"]["content"] = body_match.group(1).strip()
122
+ app.logger.info("Extracted body content from index.html")
123
+ else:
124
+ app.logger.warning("Could not extract body content from index.html, using full content.")
125
+
126
  socketio.emit('progress', {'percent': 90, 'message': 'Parsed with fallback method'})
127
  return files
128
  except ValueError as e: # Catch ValueErrors too