EvidenceAIResearch commited on
Commit
a3feb0e
·
verified ·
1 Parent(s): e4253bb

Upload RadGenome demo Space

Browse files
Files changed (3) hide show
  1. demo_log.py +13 -10
  2. render.py +11 -30
  3. templates/template.html +2 -2
demo_log.py CHANGED
@@ -2039,8 +2039,8 @@ class IFrame:
2039
  """
2040
  iframe = """
2041
  <iframe id="diagram-html"
2042
- onload='this.style.height=this.contentWindow.document.body.scrollHeight + 20px;'
2043
- src="{html_path}?t={time}"
2044
  ></iframe>
2045
  """
2046
  template = """
@@ -2056,23 +2056,26 @@ class IFrame:
2056
  </div>
2057
  """
2058
 
2059
- def __init__(self, html_path=""):
2060
- self.html_path = html_path
2061
  self.icon = IFrame.icon
2062
  self.iframe = ""
2063
- if self.html_path:
2064
- self.iframe = IFrame.iframe.format(html_path=self.html_path, time=time.time())
 
2065
  self.icon = ""
2066
-
2067
  @property
2068
  def style(self):
2069
- if self.html_path:
2070
  return ""
2071
  return "min-height: 400px;"
2072
 
2073
  def render(self):
2074
  return self.template.format(style=self.style, label_icon=IFrame.icon, icon=self.icon, iframe=self.iframe)
2075
 
 
 
2076
  class TextBox:
2077
  icon = """
2078
  <svg class="html-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 25.5 18.14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
@@ -2582,8 +2585,8 @@ def build_app(args, device=None, proc=None, tok=None, vlm=None, cxas_model=None)
2582
  img_wrapper_json.update(explain_json)
2583
  json_output = img_wrapper_json
2584
 
2585
- html_path = render_diagram_html(json_output)
2586
- iframe = IFrame(html_path=html_path)
2587
 
2588
  logger.finalize_reasoning(reasoning_wrapper)
2589
 
 
2039
  """
2040
  iframe = """
2041
  <iframe id="diagram-html"
2042
+ onload='this.style.height=this.contentWindow.document.body.scrollHeight + 20 + "px";'
2043
+ srcdoc="{html_content}"
2044
  ></iframe>
2045
  """
2046
  template = """
 
2056
  </div>
2057
  """
2058
 
2059
+ def __init__(self, html_content=""):
2060
+ self.html_content = html_content
2061
  self.icon = IFrame.icon
2062
  self.iframe = ""
2063
+ if self.html_content:
2064
+ import html as _html
2065
+ self.iframe = IFrame.iframe.format(html_content=_html.escape(html_content, quote=True))
2066
  self.icon = ""
2067
+
2068
  @property
2069
  def style(self):
2070
+ if self.html_content:
2071
  return ""
2072
  return "min-height: 400px;"
2073
 
2074
  def render(self):
2075
  return self.template.format(style=self.style, label_icon=IFrame.icon, icon=self.icon, iframe=self.iframe)
2076
 
2077
+
2078
+
2079
  class TextBox:
2080
  icon = """
2081
  <svg class="html-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 25.5 18.14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
 
2585
  img_wrapper_json.update(explain_json)
2586
  json_output = img_wrapper_json
2587
 
2588
+ html_content = render_diagram_html(json_output)
2589
+ iframe = IFrame(html_content=html_content)
2590
 
2591
  logger.finalize_reasoning(reasoning_wrapper)
2592
 
render.py CHANGED
@@ -178,13 +178,6 @@ def render_diagram_html(data):
178
  css = env.get_template("template.css")
179
  js = env.get_template("script.js")
180
 
181
- output_folder = Path("static").resolve()
182
- output_folder.mkdir(parents=True, exist_ok=True)
183
-
184
- html_path = str(output_folder / "diagram.html")
185
- css_path = str(output_folder / "styles.css")
186
- js_path = str(output_folder / "script.js")
187
-
188
  # Setup
189
  svg_html_ls = []
190
  tbox_left_html_ls = []
@@ -268,35 +261,23 @@ def render_diagram_html(data):
268
  connector_css_ls.append(templates.diagram_html.connector_css.render(**data_point).strip("\n"))
269
  i += 1
270
 
271
- # gradio_prefix = ""
272
- # html = html.render(
273
- # stylesheet = f"{gradio_prefix}{css_path.replace('static/', '')}",
274
- # javascript = f"{gradio_prefix}{js_path.replace('static/', '')}",
275
- gradio_prefix = "/gradio_api/file="
276
- html = html.render(
277
- stylesheet = f"{gradio_prefix}{css_path.replace(chr(92), '/')}",
278
- javascript = f"{gradio_prefix}{js_path.replace(chr(92), '/')}",
279
- image_path = to_b64(data["image_path"]),
280
- svgs = "\n".join(svg_html_ls),
281
- tboxs_left = "\n".join(tbox_left_html_ls),
282
- tboxs_right = "\n".join(tbox_right_html_ls),
283
- )
284
- css = css.render(
285
  svgs = "\n".join(svg_css_ls),
286
  tboxs = "\n".join(tbox_css_ls),
287
  connectors = "\n".join(connector_css_ls),
288
  )
 
289
 
290
- with open(html_path, "w") as f:
291
- f.write(html)
292
-
293
- with open(css_path, "w") as f:
294
- f.write(css)
295
-
296
- with open(js_path, "w") as f:
297
- f.write(js.render())
298
 
299
- return f"{gradio_prefix}{html_path}"
300
 
301
 
302
  import lorem
 
178
  css = env.get_template("template.css")
179
  js = env.get_template("script.js")
180
 
 
 
 
 
 
 
 
181
  # Setup
182
  svg_html_ls = []
183
  tbox_left_html_ls = []
 
261
  connector_css_ls.append(templates.diagram_html.connector_css.render(**data_point).strip("\n"))
262
  i += 1
263
 
264
+ css_content = css.render(
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  svgs = "\n".join(svg_css_ls),
266
  tboxs = "\n".join(tbox_css_ls),
267
  connectors = "\n".join(connector_css_ls),
268
  )
269
+ js_content = js.render()
270
 
271
+ html_content = html.render(
272
+ stylesheet_content = css_content,
273
+ javascript_content = js_content,
274
+ image_path = to_b64(data["image_path"]),
275
+ svgs = "\n".join(svg_html_ls),
276
+ tboxs_left = "\n".join(tbox_left_html_ls),
277
+ tboxs_right = "\n".join(tbox_right_html_ls),
278
+ )
279
 
280
+ return html_content
281
 
282
 
283
  import lorem
templates/template.html CHANGED
@@ -4,7 +4,7 @@
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>Medical Image Analysis</title>
7
- <link rel="stylesheet" href="{{stylesheet}}">
8
  </head>
9
  <body>
10
  <div class="container">
@@ -21,6 +21,6 @@
21
  {{tboxs_right}}
22
  </div>
23
  </div>
24
- <script src="{{javascript}}" defer></script>
25
  </body>
26
  </html>
 
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>Medical Image Analysis</title>
7
+ <style>{{stylesheet_content}}</style>
8
  </head>
9
  <body>
10
  <div class="container">
 
21
  {{tboxs_right}}
22
  </div>
23
  </div>
24
+ <script>{{javascript_content}}</script>
25
  </body>
26
  </html>