Rahul-8799 commited on
Commit
17aa104
·
verified ·
1 Parent(s): 31beef4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -9
app.py CHANGED
@@ -8,21 +8,40 @@ 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)]
@@ -31,13 +50,11 @@ def process(user_input):
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
 
36
- # Escape quotes for safe iframe embedding
37
- escaped_html = html.replace('"', """)
38
- preview = f"<iframe srcdoc='{escaped_html}' width='100%' height='600px' style='border:1px solid #ccc;'></iframe>"
39
 
40
- return preview, html, log, html_file, log_file, zip_file
41
 
42
  with gr.Blocks() as demo:
43
  gr.Markdown("# 🤖 Multi-Agent UI Generator")
@@ -58,11 +75,24 @@ with gr.Blocks() as demo:
58
  log_download = gr.File(label="Download Agent Log")
59
  zip_download = gr.File(label="Download All (.zip)")
60
 
 
 
 
 
 
61
  run_btn.click(
62
  fn=process,
63
  inputs=[user_prompt],
64
- outputs=[preview_html, html_output, log_output, html_download, log_download, zip_download]
65
- )
 
 
 
 
 
 
 
 
66
 
67
  if __name__ == "__main__":
68
  demo.launch()
 
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)]
 
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")
 
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()