Spaces:
Runtime error
Runtime error
Upload langgraph_pipeline.py
Browse files- utils/langgraph_pipeline.py +97 -57
utils/langgraph_pipeline.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import uuid, zipfile, re
|
2 |
from pathlib import Path
|
3 |
from typing import TypedDict, List, Dict, Any, Tuple
|
4 |
|
@@ -157,70 +157,110 @@ def parse_spec(spec: str) -> Dict[str, List[str]]:
|
|
157 |
def run_pipeline_and_save(prompt: str) -> Tuple[List[Dict[str, Any]], str]:
|
158 |
# a) invoke agents
|
159 |
initial_state = {"messages": [HumanMessage(content=prompt)], "chat_log": [], "iteration": 0, "feedback": ""}
|
160 |
-
final_state
|
161 |
-
|
162 |
-
chat_log
|
163 |
-
|
164 |
-
|
165 |
-
# b) parse spec
|
166 |
-
spec = parse_spec(qa_output)
|
167 |
-
features = spec.get("Key features", [])
|
168 |
-
testimonials = spec.get("User stories", [])
|
169 |
-
|
170 |
-
# c) build HTML
|
171 |
-
title = prompt.title()
|
172 |
-
domain = prompt.replace(" ", "").lower() + ".com"
|
173 |
-
cards_html = "\n".join(f"<div class='card'><h3>{f}</h3></div>" for f in features)
|
174 |
-
test_html = "\n".join(f"<blockquote>{t}</blockquote>" for t in testimonials)
|
175 |
-
|
176 |
-
html_code = f"""<!DOCTYPE html>
|
177 |
-
<html lang="en">
|
178 |
-
<head>
|
179 |
-
<meta charset="UTF-8">
|
180 |
-
<meta name="viewport" content="width=device-width,initial-scale=1">
|
181 |
-
<title>{title}</title>
|
182 |
-
<link rel="stylesheet" href="styles.css">
|
183 |
-
</head>
|
184 |
-
<body>
|
185 |
-
<header><h1>{title}</h1></header>
|
186 |
-
<section id="features">
|
187 |
-
<h2>Features</h2>
|
188 |
-
<div class="cards">
|
189 |
-
{cards_html}
|
190 |
-
</div>
|
191 |
-
</section>
|
192 |
-
<section id="testimonials">
|
193 |
-
<h2>Testimonials</h2>
|
194 |
-
{test_html or '<p>No testimonials provided.</p>'}
|
195 |
-
</section>
|
196 |
-
<section id="contact">
|
197 |
-
<h2>Contact Us</h2>
|
198 |
-
<p>Email: info@{domain}</p>
|
199 |
-
</section>
|
200 |
-
</body>
|
201 |
-
</html>"""
|
202 |
-
|
203 |
-
# d) basic CSS
|
204 |
-
css_code = """
|
205 |
-
body { font-family: Arial, sans-serif; margin: 1em; line-height: 1.5; }
|
206 |
-
header { text-align: center; margin-bottom: 2em; }
|
207 |
-
.cards { display: grid; grid-template-columns: repeat(auto-fit,minmax(150px,1fr)); gap: 1em; }
|
208 |
-
.card { background: #f9f9f9; padding: 1em; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); text-align: center; }
|
209 |
-
blockquote { font-style: italic; margin: 1em; padding: 0.5em; background: #eef; border-left: 4px solid #99f; }
|
210 |
-
"""
|
211 |
|
212 |
-
#
|
213 |
-
|
214 |
-
|
|
|
|
|
|
|
215 |
site_dir = out_dir / f"site_{site_id}"
|
216 |
site_dir.mkdir(parents=True, exist_ok=True)
|
217 |
|
218 |
-
|
219 |
-
(site_dir / "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
zip_path = out_dir / f"site_{site_id}.zip"
|
222 |
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
223 |
for f in site_dir.iterdir():
|
224 |
zf.write(f, arcname=f.name)
|
225 |
|
226 |
return chat_log, str(zip_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import uuid, zipfile, re, json
|
2 |
from pathlib import Path
|
3 |
from typing import TypedDict, List, Dict, Any, Tuple
|
4 |
|
|
|
157 |
def run_pipeline_and_save(prompt: str) -> Tuple[List[Dict[str, Any]], str]:
|
158 |
# a) invoke agents
|
159 |
initial_state = {"messages": [HumanMessage(content=prompt)], "chat_log": [], "iteration": 0, "feedback": ""}
|
160 |
+
final_state = compiled_graph.invoke(initial_state)
|
161 |
+
|
162 |
+
chat_log = final_state["chat_log"]
|
163 |
+
dev_output = final_state["dev_output"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
|
165 |
+
# b) parse the developer output to extract code sections
|
166 |
+
sections = parse_code_sections(dev_output)
|
167 |
+
|
168 |
+
# c) write & zip
|
169 |
+
site_id = uuid.uuid4().hex
|
170 |
+
out_dir = Path("output")
|
171 |
site_dir = out_dir / f"site_{site_id}"
|
172 |
site_dir.mkdir(parents=True, exist_ok=True)
|
173 |
|
174 |
+
# Write HTML file
|
175 |
+
(site_dir / "index.html").write_text(sections.get("HTML Structure", ""), encoding="utf-8")
|
176 |
+
|
177 |
+
# Write CSS file
|
178 |
+
(site_dir / "styles.css").write_text(sections.get("CSS Styles", ""), encoding="utf-8")
|
179 |
+
|
180 |
+
# Write JavaScript file
|
181 |
+
(site_dir / "script.js").write_text(sections.get("JavaScript", ""), encoding="utf-8")
|
182 |
+
|
183 |
+
# Write Tailwind config
|
184 |
+
(site_dir / "tailwind.config.js").write_text(sections.get("Tailwind Config", ""), encoding="utf-8")
|
185 |
+
|
186 |
+
# Create package.json for dependencies
|
187 |
+
package_json = {
|
188 |
+
"name": f"site_{site_id}",
|
189 |
+
"version": "1.0.0",
|
190 |
+
"description": "Generated responsive website",
|
191 |
+
"scripts": {
|
192 |
+
"build": "tailwindcss -i ./styles.css -o ./dist/output.css",
|
193 |
+
"watch": "tailwindcss -i ./styles.css -o ./dist/output.css --watch"
|
194 |
+
},
|
195 |
+
"dependencies": {
|
196 |
+
"tailwindcss": "^3.4.1",
|
197 |
+
"alpinejs": "^3.13.3"
|
198 |
+
}
|
199 |
+
}
|
200 |
+
|
201 |
+
(site_dir / "package.json").write_text(
|
202 |
+
json.dumps(package_json, indent=2),
|
203 |
+
encoding="utf-8"
|
204 |
+
)
|
205 |
+
|
206 |
+
# Create README
|
207 |
+
readme_content = f"""# Generated Website
|
208 |
+
|
209 |
+
This is a responsive website generated by the Multi-Agent UI Generator.
|
210 |
|
211 |
+
## Setup
|
212 |
+
|
213 |
+
1. Install dependencies:
|
214 |
+
```bash
|
215 |
+
npm install
|
216 |
+
```
|
217 |
+
|
218 |
+
2. Build the CSS:
|
219 |
+
```bash
|
220 |
+
npm run build
|
221 |
+
```
|
222 |
+
|
223 |
+
3. For development with live reload:
|
224 |
+
```bash
|
225 |
+
npm run watch
|
226 |
+
```
|
227 |
+
|
228 |
+
## Features
|
229 |
+
|
230 |
+
- Responsive design using Tailwind CSS
|
231 |
+
- Interactive elements with JavaScript
|
232 |
+
- Modern animations and transitions
|
233 |
+
- Mobile-first approach
|
234 |
+
"""
|
235 |
+
|
236 |
+
(site_dir / "README.md").write_text(readme_content, encoding="utf-8")
|
237 |
+
|
238 |
+
# Create zip file
|
239 |
zip_path = out_dir / f"site_{site_id}.zip"
|
240 |
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
241 |
for f in site_dir.iterdir():
|
242 |
zf.write(f, arcname=f.name)
|
243 |
|
244 |
return chat_log, str(zip_path)
|
245 |
+
|
246 |
+
def parse_code_sections(output: str) -> Dict[str, str]:
|
247 |
+
"""Parse code sections from the developer output"""
|
248 |
+
sections = {}
|
249 |
+
current_section = None
|
250 |
+
current_code = []
|
251 |
+
|
252 |
+
for line in output.split("\n"):
|
253 |
+
if line.startswith("## "):
|
254 |
+
if current_section:
|
255 |
+
sections[current_section] = "\n".join(current_code)
|
256 |
+
current_section = line[3:].strip()
|
257 |
+
current_code = []
|
258 |
+
elif line.startswith("```"):
|
259 |
+
continue
|
260 |
+
elif current_section:
|
261 |
+
current_code.append(line)
|
262 |
+
|
263 |
+
if current_section:
|
264 |
+
sections[current_section] = "\n".join(current_code)
|
265 |
+
|
266 |
+
return sections
|