Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import zipfile
|
| 3 |
+
import os
|
| 4 |
+
from langchain_core.messages import HumanMessage
|
| 5 |
+
from utils.langgraph_pipeline import run_pipeline_and_save
|
| 6 |
+
|
| 7 |
+
TEMP_DIR = "generated_output"
|
| 8 |
+
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 9 |
+
|
| 10 |
+
def save_files(html: str, log: str):
|
| 11 |
+
html_path = os.path.join(TEMP_DIR, "ui.html")
|
| 12 |
+
log_path = os.path.join(TEMP_DIR, "agent_log.txt")
|
| 13 |
+
zip_path = os.path.join(TEMP_DIR, "output_bundle.zip")
|
| 14 |
+
|
| 15 |
+
with open(html_path, "w", encoding="utf-8") as f:
|
| 16 |
+
f.write(html)
|
| 17 |
+
|
| 18 |
+
with open(log_path, "w", encoding="utf-8") as f:
|
| 19 |
+
f.write(log)
|
| 20 |
+
|
| 21 |
+
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 22 |
+
zipf.write(html_path, arcname="ui.html")
|
| 23 |
+
zipf.write(log_path, arcname="agent_log.txt")
|
| 24 |
+
|
| 25 |
+
return html_path, log_path, zip_path
|
| 26 |
+
|
| 27 |
+
def process(user_input):
|
| 28 |
+
messages = [HumanMessage(content=user_input)]
|
| 29 |
+
final_state = run_pipeline_and_save(messages)
|
| 30 |
+
|
| 31 |
+
html = final_state["html_output"]
|
| 32 |
+
log = "\n\n".join([f"{msg.type.upper()}: {msg.content}" for msg in final_state["messages"]])
|
| 33 |
+
|
| 34 |
+
html_file, log_file, zip_file = save_files(html, log)
|
| 35 |
+
preview = f"<iframe src='file/{html_file}' width='100%' height='500px' style='border:1px solid #ccc;'></iframe>"
|
| 36 |
+
|
| 37 |
+
return preview, html, log, html_file, log_file, zip_file
|
| 38 |
+
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("# 🤖 Multi-Agent UI Generator (LangGraph + Mistral)")
|
| 41 |
+
user_prompt = gr.Textbox(label="Describe your UI", lines=4, value="A yoga retreat homepage with schedule and testimonials.")
|
| 42 |
+
run_btn = gr.Button("Run Agents")
|
| 43 |
+
|
| 44 |
+
gr.Markdown("### 🔍 Website Preview")
|
| 45 |
+
preview_html = gr.HTML()
|
| 46 |
+
|
| 47 |
+
gr.Markdown("### 🖥️ Generated HTML Code")
|
| 48 |
+
html_output = gr.Textbox(lines=15, label="Final HTML")
|
| 49 |
+
|
| 50 |
+
gr.Markdown("### 💬 Agent Dialogue")
|
| 51 |
+
log_output = gr.Textbox(lines=15, label="Conversation Log")
|
| 52 |
+
|
| 53 |
+
gr.Markdown("### 📥 Download Files")
|
| 54 |
+
html_download = gr.File(label="Download HTML")
|
| 55 |
+
log_download = gr.File(label="Download Agent Log")
|
| 56 |
+
zip_download = gr.File(label="Download All (.zip)")
|
| 57 |
+
|
| 58 |
+
run_btn.click(
|
| 59 |
+
fn=process,
|
| 60 |
+
inputs=[user_prompt],
|
| 61 |
+
outputs=[preview_html, html_output, log_output, html_download, log_download, zip_download]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
demo.launch()
|