File size: 2,330 Bytes
ae75831
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca12d1b
 
 
 
ae75831
 
 
 
3e08048
ae75831
 
 
 
ca12d1b
ae75831
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca12d1b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import gradio as gr
import zipfile
import os
from langchain_core.messages import HumanMessage
from utils.langgraph_pipeline import run_pipeline_and_save

TEMP_DIR = "generated_output"
os.makedirs(TEMP_DIR, exist_ok=True)

def save_files(html: str, log: str):
    html_path = os.path.join(TEMP_DIR, "ui.html")
    log_path = os.path.join(TEMP_DIR, "agent_log.txt")
    zip_path = os.path.join(TEMP_DIR, "output_bundle.zip")

    with open(html_path, "w", encoding="utf-8") as f:
        f.write(html)

    with open(log_path, "w", encoding="utf-8") as f:
        f.write(log)

    with zipfile.ZipFile(zip_path, "w") as zipf:
        zipf.write(html_path, arcname="ui.html")
        zipf.write(log_path, arcname="agent_log.txt")

    return html_path, log_path, zip_path

def process(user_input):
    messages = [HumanMessage(content=user_input)]
    final_state = run_pipeline_and_save(messages)

    html = final_state["html_output"]
    log = "\n\n".join([f"{msg.type.upper()}: {msg.content}" for msg in final_state["messages"]])

    html_file, log_file, zip_file = save_files(html, log)

    # Escape quotes for safe iframe embedding
    escaped_html = html.replace('"', """)
    preview = f"<iframe srcdoc='{escaped_html}' width='100%' height='600px' style='border:1px solid #ccc;'></iframe>"

    return preview, html, log, html_file, log_file, zip_file

with gr.Blocks() as demo:
    gr.Markdown("# πŸ€– Multi-Agent UI Generator")
    user_prompt = gr.Textbox(label="Describe your UI", lines=4, value="A yoga retreat homepage with schedule and testimonials.")
    run_btn = gr.Button("Run Agents")

    gr.Markdown("### πŸ” Website Preview")
    preview_html = gr.HTML()

    gr.Markdown("### πŸ–₯️ Generated HTML Code")
    html_output = gr.Textbox(lines=15, label="Final HTML")

    gr.Markdown("### πŸ’¬ Agent Dialogue")
    log_output = gr.Textbox(lines=15, label="Conversation Log")

    gr.Markdown("### πŸ“₯ Download Files")
    html_download = gr.File(label="Download HTML")
    log_download = gr.File(label="Download Agent Log")
    zip_download = gr.File(label="Download All (.zip)")

    run_btn.click(
        fn=process,
        inputs=[user_prompt],
        outputs=[preview_html, html_output, log_output, html_download, log_download, zip_download]
    )

if __name__ == "__main__":
    demo.launch()