File size: 3,180 Bytes
ae75831
 
 
 
 
 
 
 
 
 
17aa104
 
 
 
 
 
 
 
 
 
 
 
ae75831
17aa104
ae75831
 
 
17aa104
ae75831
17aa104
 
 
 
ae75831
 
 
 
 
 
17aa104
ae75831
 
17aa104
 
ae75831
 
 
 
 
 
 
 
17aa104
 
ca12d1b
17aa104
ae75831
 
 
3e08048
ae75831
 
 
 
ca12d1b
ae75831
 
 
 
 
 
 
 
 
 
 
 
17aa104
 
 
 
 
ae75831
 
 
17aa104
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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):
    TEMP_DIR = "generated_output"
    os.makedirs(TEMP_DIR, exist_ok=True)

    # Extract CSS
    import re
    css_match = re.search(r"<style[^>]*>(.*?)</style>", html, flags=re.DOTALL)
    css = css_match.group(1).strip() if css_match else ""

    # Remove <style> tag and replace with <link>
    html_clean = re.sub(r"<style[^>]*>.*?</style>", '<link rel="stylesheet" href="styles.css">', html, flags=re.DOTALL)

    # Paths
    html_path = os.path.join(TEMP_DIR, "ui.html")
    css_path = os.path.join(TEMP_DIR, "styles.css")
    log_path = os.path.join(TEMP_DIR, "agent_log.txt")
    zip_path = os.path.join(TEMP_DIR, "output_bundle.zip")

    # Save files
    with open(html_path, "w", encoding="utf-8") as f:
        f.write(html_clean)

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

    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(css_path, arcname="styles.css")
        zipf.write(log_path, arcname="agent_log.txt")

    return html_path, css_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, css_file, log_file, zip_file = save_files(html, log)
    preview = f"<iframe src='file/{html_file}' width='100%' height='500px' style='border:1px solid #ccc;'></iframe>"

    return preview, html, log, html_file, css_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)")

    html_download = gr.File(label="Download HTML")
    css_download = gr.File(label="Download CSS")
    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,
        css_download,
        log_download,
        zip_download
    ]
)

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