Rahul-8799 commited on
Commit
d6076e8
Β·
verified Β·
1 Parent(s): 8529f5a

Update app.py

Browse files

added tailwind snippet by S

Files changed (1) hide show
  1. app.py +53 -54
app.py CHANGED
@@ -1,98 +1,97 @@
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
- TEMP_DIR = "generated_output"
12
- os.makedirs(TEMP_DIR, exist_ok=True)
13
-
14
- # Extract CSS
15
- import re
16
- css_match = re.search(r"<style[^>]*>(.*?)</style>", html, flags=re.DOTALL)
17
- css = css_match.group(1).strip() if css_match else ""
18
-
19
- # Remove <style> tag and replace with <link>
20
- html_clean = re.sub(r"<style[^>]*>.*?</style>", '<link rel="stylesheet" href="styles.css">', html, flags=re.DOTALL)
21
-
22
- # Paths
23
  html_path = os.path.join(TEMP_DIR, "ui.html")
24
- css_path = os.path.join(TEMP_DIR, "styles.css")
25
  log_path = os.path.join(TEMP_DIR, "agent_log.txt")
26
  zip_path = os.path.join(TEMP_DIR, "output_bundle.zip")
27
 
28
- # Save files
29
  with open(html_path, "w", encoding="utf-8") as f:
30
- f.write(html_clean)
31
-
32
- with open(css_path, "w", encoding="utf-8") as f:
33
- f.write(css)
34
 
35
  with open(log_path, "w", encoding="utf-8") as f:
36
  f.write(log)
37
 
38
  with zipfile.ZipFile(zip_path, "w") as zipf:
39
  zipf.write(html_path, arcname="ui.html")
40
- zipf.write(css_path, arcname="styles.css")
41
  zipf.write(log_path, arcname="agent_log.txt")
42
 
43
- return html_path, css_path, log_path, zip_path
44
-
45
 
 
46
  def process(user_input):
47
  messages = [HumanMessage(content=user_input)]
48
  final_state = run_pipeline_and_save(messages)
49
 
50
- html = final_state["html_output"]
 
51
  log = "\n\n".join([f"{msg.type.upper()}: {msg.content}" for msg in final_state["messages"]])
52
 
53
- html_file, css_file, log_file, zip_file = save_files(html, log)
54
- preview = f"<iframe src='file/{html_file}' width='100%' height='500px' style='border:1px solid #ccc;'></iframe>"
55
-
56
- return preview, html, log, html_file, css_file, log_file, zip_file
57
 
 
 
 
58
 
59
- with gr.Blocks() as demo:
60
- gr.Markdown("# πŸ€– Multi-Agent UI Generator")
61
- user_prompt = gr.Textbox(label="Describe your UI", lines=4, value="A yoga retreat homepage with schedule and testimonials.")
62
- run_btn = gr.Button("Run Agents")
63
 
64
- gr.Markdown("### πŸ” Website Preview")
65
- preview_html = gr.HTML()
 
 
66
 
67
- gr.Markdown("### πŸ–₯️ Generated HTML Code")
68
- html_output = gr.Textbox(lines=15, label="Final HTML")
 
69
 
70
- gr.Markdown("### πŸ’¬ Agent Dialogue")
71
- log_output = gr.Textbox(lines=15, label="Conversation Log")
72
 
73
- gr.Markdown("### πŸ“₯ Download Files")
74
- html_download = gr.File(label="Download HTML")
75
- log_download = gr.File(label="Download Agent Log")
76
- zip_download = gr.File(label="Download All (.zip)")
77
 
78
- html_download = gr.File(label="Download HTML")
79
- css_download = gr.File(label="Download CSS")
80
- log_download = gr.File(label="Download Agent Log")
81
- zip_download = gr.File(label="Download All (.zip)")
 
82
 
83
  run_btn.click(
84
  fn=process,
85
  inputs=[user_prompt],
86
- outputs=[
87
- preview_html,
88
- html_output,
89
- log_output,
90
- html_download,
91
- css_download,
92
- log_download,
93
- zip_download
94
- ]
95
- )
96
 
97
  if __name__ == "__main__":
98
  demo.launch()
 
1
  import gradio as gr
2
  import zipfile
3
  import os
4
+ 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
+ # Tailwind CDN snippet
12
+ TAILWIND_CDN = """
13
+ <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
14
+ """
15
+
16
+ # Format and beautify HTML
17
+ def format_html(html_body: str):
18
+ full_html = f"""<!DOCTYPE html>
19
+ <html lang="en">
20
+ <head>
21
+ <meta charset="UTF-8">
22
+ <title>Generated UI</title>
23
+ {TAILWIND_CDN}
24
+ </head>
25
+ <body class="bg-white text-gray-900">
26
+ {html_body}
27
+ </body>
28
+ </html>"""
29
+ soup = BeautifulSoup(full_html, "html.parser")
30
+ return soup.prettify()
31
+
32
+ # Save HTML and log to files and zip
33
  def save_files(html: str, log: str):
 
 
 
 
 
 
 
 
 
 
 
 
34
  html_path = os.path.join(TEMP_DIR, "ui.html")
 
35
  log_path = os.path.join(TEMP_DIR, "agent_log.txt")
36
  zip_path = os.path.join(TEMP_DIR, "output_bundle.zip")
37
 
 
38
  with open(html_path, "w", encoding="utf-8") as f:
39
+ f.write(html)
 
 
 
40
 
41
  with open(log_path, "w", encoding="utf-8") as f:
42
  f.write(log)
43
 
44
  with zipfile.ZipFile(zip_path, "w") as zipf:
45
  zipf.write(html_path, arcname="ui.html")
 
46
  zipf.write(log_path, arcname="agent_log.txt")
47
 
48
+ return html_path, log_path, zip_path
 
49
 
50
+ # Core processing logic
51
  def process(user_input):
52
  messages = [HumanMessage(content=user_input)]
53
  final_state = run_pipeline_and_save(messages)
54
 
55
+ raw_html = final_state["html_output"]
56
+ html = format_html(raw_html) # 🧼 Beautify and wrap with Tailwind
57
  log = "\n\n".join([f"{msg.type.upper()}: {msg.content}" for msg in final_state["messages"]])
58
 
59
+ html_file, log_file, zip_file = save_files(html, log)
 
 
 
60
 
61
+ # Escape quotes for safe iframe rendering
62
+ escaped_html = html.replace('"', "&quot;")
63
+ preview = f"<iframe srcdoc='{escaped_html}' width='100%' height='600px' style='border:1px solid #ccc; border-radius:8px;'></iframe>"
64
 
65
+ return preview, html, log, html_file, log_file, zip_file
 
 
 
66
 
67
+ # Gradio Interface
68
+ with gr.Blocks(css=".gr-box { border-radius: 12px; padding: 10px; }") as demo:
69
+ gr.Markdown("## πŸ€– Multi-Agent UI Generator")
70
+ gr.Markdown("Describe the kind of UI you want, and our AI agents will generate a complete HTML page for you!")
71
 
72
+ with gr.Group():
73
+ user_prompt = gr.Textbox(label="πŸ“„ Describe Your Web Page", lines=4, placeholder="e.g., A yoga retreat homepage with schedule and testimonials.")
74
+ run_btn = gr.Button("πŸš€ Generate with Agents")
75
 
76
+ with gr.Tab("🌐 Live Preview"):
77
+ preview_html = gr.HTML()
78
 
79
+ with gr.Tab("🧩 Generated HTML & Agent Log"):
80
+ with gr.Row():
81
+ html_output = gr.Textbox(label="πŸ–₯️ HTML Code", lines=15, interactive=True, show_copy_button=True)
82
+ log_output = gr.Textbox(label="πŸ’¬ Agent Conversation Log", lines=15, interactive=True, show_copy_button=True)
83
 
84
+ with gr.Tab("πŸ“₯ Downloads"):
85
+ with gr.Row():
86
+ html_download = gr.File(label="Download HTML")
87
+ log_download = gr.File(label="Download Agent Log")
88
+ zip_download = gr.File(label="Download All (.zip)")
89
 
90
  run_btn.click(
91
  fn=process,
92
  inputs=[user_prompt],
93
+ outputs=[preview_html, html_output, log_output, html_download, log_download, zip_download]
94
+ )
 
 
 
 
 
 
 
 
95
 
96
  if __name__ == "__main__":
97
  demo.launch()