Spaces:
Sleeping
Sleeping
Update app.py by S
Browse files
app.py
CHANGED
|
@@ -5,61 +5,16 @@ from bs4 import BeautifulSoup
|
|
| 5 |
from langchain_core.messages import HumanMessage
|
| 6 |
from utils.langgraph_pipeline import run_pipeline_and_save
|
| 7 |
|
|
|
|
| 8 |
TEMP_DIR = "generated_output"
|
| 9 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
<!DOCTYPE html>
|
| 14 |
-
<html lang="en">
|
| 15 |
-
<head>
|
| 16 |
-
<meta charset="UTF-8">
|
| 17 |
-
<title>{title}</title>
|
| 18 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 19 |
-
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
|
| 20 |
-
</head>
|
| 21 |
-
<body class="bg-white text-gray-800 font-sans leading-relaxed antialiased">
|
| 22 |
-
<header class="bg-gradient-to-r from-blue-500 to-indigo-600 p-6 text-white shadow">
|
| 23 |
-
<div class="max-w-7xl mx-auto text-center">
|
| 24 |
-
<h1 class="text-3xl font-bold">{title}</h1>
|
| 25 |
-
<p class="text-lg mt-2">Generated by our multi-agent AI system</p>
|
| 26 |
-
</div>
|
| 27 |
-
</header>
|
| 28 |
-
<main class="max-w-5xl mx-auto px-6 py-10 space-y-10">
|
| 29 |
-
{content}
|
| 30 |
-
</main>
|
| 31 |
-
<footer class="bg-gray-100 text-center text-sm text-gray-500 py-4">
|
| 32 |
-
© 2025 Multi-Agent Generator. All rights reserved.
|
| 33 |
-
</footer>
|
| 34 |
-
</body>
|
| 35 |
-
</html>
|
| 36 |
-
"""
|
| 37 |
-
|
| 38 |
-
# Try to extract the body content and remove nested HTML/head/style
|
| 39 |
-
def clean_agent_html(raw_html: str):
|
| 40 |
soup = BeautifulSoup(raw_html, "html.parser")
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
if soup.body:
|
| 44 |
-
return soup.body.decode_contents()
|
| 45 |
-
|
| 46 |
-
# Otherwise remove common structural tags
|
| 47 |
-
for tag in soup(["html", "head", "body", "style", "script"]):
|
| 48 |
-
tag.decompose()
|
| 49 |
-
|
| 50 |
-
return str(soup)
|
| 51 |
-
|
| 52 |
-
# Build final HTML by injecting cleaned content and a dynamic title
|
| 53 |
-
def format_html(agent_html: str, user_input: str):
|
| 54 |
-
content = clean_agent_html(agent_html)
|
| 55 |
-
|
| 56 |
-
# Try to infer a title from user input (e.g., "yoga retreat" becomes "Yoga Retreat")
|
| 57 |
-
title = user_input.split("for")[-1].strip().capitalize() if "for" in user_input.lower() else "Generated Website"
|
| 58 |
-
|
| 59 |
-
final_html = TAILWIND_TEMPLATE.format(title=title, content=content)
|
| 60 |
-
return BeautifulSoup(final_html, "html.parser").prettify()
|
| 61 |
-
|
| 62 |
-
# Save output files to zip
|
| 63 |
def save_files(html: str, log: str):
|
| 64 |
html_path = os.path.join(TEMP_DIR, "ui.html")
|
| 65 |
log_path = os.path.join(TEMP_DIR, "agent_log.txt")
|
|
@@ -77,47 +32,44 @@ def save_files(html: str, log: str):
|
|
| 77 |
|
| 78 |
return html_path, log_path, zip_path
|
| 79 |
|
| 80 |
-
# Main
|
| 81 |
def process(user_input):
|
| 82 |
messages = [HumanMessage(content=user_input)]
|
| 83 |
final_state = run_pipeline_and_save(messages)
|
| 84 |
|
| 85 |
raw_html = final_state["html_output"]
|
| 86 |
-
|
|
|
|
| 87 |
log = "\n\n".join([f"{msg.type.upper()}: {msg.content}" for msg in final_state["messages"]])
|
| 88 |
|
| 89 |
-
html_file, log_file, zip_file = save_files(
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
preview = f"<iframe srcdoc='{escaped}' width='100%' height='600px' style='border:1px solid #ccc; border-radius:8px;'></iframe>"
|
| 93 |
|
| 94 |
-
return preview,
|
| 95 |
|
| 96 |
# Gradio UI
|
| 97 |
-
with gr.Blocks(
|
| 98 |
-
gr.Markdown("
|
| 99 |
-
gr.
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
value="Create a yoga retreat homepage with a hero section, class schedule, testimonials, and contact info."
|
| 105 |
-
)
|
| 106 |
-
run_btn = gr.Button("π Generate")
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
html_output = gr.Textbox(label="π₯οΈ Final HTML Code", lines=15, interactive=True, show_copy_button=True)
|
| 114 |
-
log_output = gr.Textbox(label="π Agent Conversation Log", lines=15, interactive=True, show_copy_button=True)
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
zip_download = gr.File(label="ποΈ Download All Files")
|
| 121 |
|
| 122 |
run_btn.click(
|
| 123 |
fn=process,
|
|
@@ -125,5 +77,6 @@ with gr.Blocks(css=".gr-box { border-radius: 12px; padding: 10px; }") as demo:
|
|
| 125 |
outputs=[preview_html, html_output, log_output, html_download, log_download, zip_download]
|
| 126 |
)
|
| 127 |
|
|
|
|
| 128 |
if __name__ == "__main__":
|
| 129 |
demo.launch()
|
|
|
|
| 5 |
from langchain_core.messages import HumanMessage
|
| 6 |
from utils.langgraph_pipeline import run_pipeline_and_save
|
| 7 |
|
| 8 |
+
# Directory to store generated files
|
| 9 |
TEMP_DIR = "generated_output"
|
| 10 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 11 |
|
| 12 |
+
# Function to beautify HTML
|
| 13 |
+
def clean_html(raw_html: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
soup = BeautifulSoup(raw_html, "html.parser")
|
| 15 |
+
return soup.prettify()
|
| 16 |
|
| 17 |
+
# Save HTML, log, and bundle into zip
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def save_files(html: str, log: str):
|
| 19 |
html_path = os.path.join(TEMP_DIR, "ui.html")
|
| 20 |
log_path = os.path.join(TEMP_DIR, "agent_log.txt")
|
|
|
|
| 32 |
|
| 33 |
return html_path, log_path, zip_path
|
| 34 |
|
| 35 |
+
# Main processing function
|
| 36 |
def process(user_input):
|
| 37 |
messages = [HumanMessage(content=user_input)]
|
| 38 |
final_state = run_pipeline_and_save(messages)
|
| 39 |
|
| 40 |
raw_html = final_state["html_output"]
|
| 41 |
+
html = clean_html(raw_html) # Beautify the HTML
|
| 42 |
+
|
| 43 |
log = "\n\n".join([f"{msg.type.upper()}: {msg.content}" for msg in final_state["messages"]])
|
| 44 |
|
| 45 |
+
html_file, log_file, zip_file = save_files(html, log)
|
| 46 |
+
|
| 47 |
+
# Escape quotes for safe iframe preview
|
| 48 |
+
escaped_html = html.replace('"', """).replace("\n", " ")
|
| 49 |
|
| 50 |
+
preview = f"<iframe srcdoc='{escaped_html}' width='100%' height='600px' style='border:1px solid #ccc;'></iframe>"
|
|
|
|
| 51 |
|
| 52 |
+
return preview, html, log, html_file, log_file, zip_file
|
| 53 |
|
| 54 |
# Gradio UI
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("# π€ Multi-Agent UI Generator")
|
| 57 |
+
user_prompt = gr.Textbox(label="Describe your UI", lines=4, value="A yoga retreat homepage with schedule and testimonials.")
|
| 58 |
+
run_btn = gr.Button("Run Agents")
|
| 59 |
+
|
| 60 |
+
gr.Markdown("### π Website Preview")
|
| 61 |
+
preview_html = gr.HTML()
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
gr.Markdown("### π₯οΈ Generated HTML Code")
|
| 64 |
+
html_output = gr.Textbox(lines=15, label="Final HTML")
|
| 65 |
|
| 66 |
+
gr.Markdown("### π¬ Agent Dialogue")
|
| 67 |
+
log_output = gr.Textbox(lines=15, label="Conversation Log")
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
gr.Markdown("### π₯ Download Files")
|
| 70 |
+
html_download = gr.File(label="Download HTML")
|
| 71 |
+
log_download = gr.File(label="Download Agent Log")
|
| 72 |
+
zip_download = gr.File(label="Download All (.zip)")
|
|
|
|
| 73 |
|
| 74 |
run_btn.click(
|
| 75 |
fn=process,
|
|
|
|
| 77 |
outputs=[preview_html, html_output, log_output, html_download, log_download, zip_download]
|
| 78 |
)
|
| 79 |
|
| 80 |
+
# Run the app
|
| 81 |
if __name__ == "__main__":
|
| 82 |
demo.launch()
|