aaron1141 commited on
Commit
57b0592
·
1 Parent(s): c34be21

feat: render QA preview as markdown with image placeholders

Browse files
Files changed (2) hide show
  1. app.py +37 -12
  2. requirements.txt +1 -0
app.py CHANGED
@@ -8,6 +8,20 @@ import traceback
8
  import zipfile
9
 
10
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  _REPO_ROOT = os.path.dirname(os.path.abspath(__file__))
13
  sys.path.insert(0, _REPO_ROOT)
@@ -121,29 +135,32 @@ def _render_preview(jsonl_path: str, lang: str = "en") -> str:
121
 
122
  cards = []
123
  for i, item in enumerate(items):
124
- q = str(item.get("question", "")).replace("<", "&lt;").replace(">", "&gt;")
125
- a = str(item.get("answer", "")).replace("<", "&lt;").replace(">", "&gt;")
126
- sol = str(item.get("solution", "")).replace("<", "&lt;").replace(">", "&gt;")
127
- name = item.get("name", "")
128
- sol_short = (sol[:300] + "…") if len(sol) > 300 else sol
 
 
 
129
  sol_block = (
130
- f'<div style="margin-top:10px">'
131
  f'<span style="font-weight:600;color:#374151">{label_s}:</span>'
132
- f'<div style="margin-top:4px;white-space:pre-wrap;font-size:13px;color:#4b5563">{sol_short}</div>'
133
  f'</div>'
134
- ) if sol and sol != a else ""
135
 
136
  cards.append(f"""
137
  <div style="border:1px solid #e5e7eb;border-radius:12px;padding:18px;margin-bottom:12px;
138
  background:#ffffff;box-shadow:0 1px 4px rgba(0,0,0,.06);">
139
  <div style="font-size:11px;color:#9ca3af;margin-bottom:10px">#{i+1} &nbsp;·&nbsp; {name}</div>
140
- <div style="margin-bottom:10px">
141
  <span style="font-weight:600;color:#111827">{label_q}:</span>
142
- <div style="margin-top:4px;white-space:pre-wrap;font-size:14px">{q}</div>
143
  </div>
144
- <div style="background:#f0fdf4;border-radius:8px;padding:10px">
145
  <span style="font-weight:600;color:#15803d">{label_a}:</span>
146
- <div style="margin-top:4px;font-size:14px;color:#166534">{a}</div>
147
  </div>
148
  {sol_block}
149
  </div>""")
@@ -335,6 +352,14 @@ CSS = """
335
  #title-row { align-items: center; }
336
  #lang-btn { min-width: 90px; }
337
  .example-btn { margin: 4px 0 !important; }
 
 
 
 
 
 
 
 
338
  """
339
 
340
  _L = _DEFAULT_LANG # shorthand for initial render
 
8
  import zipfile
9
 
10
  import gradio as gr
11
+ try:
12
+ import markdown as _md
13
+ def _md2html(text: str) -> str:
14
+ # Replace local image refs (inside zip) with a styled badge
15
+ text = re.sub(
16
+ r'!\[([^\]]*)\]\(vqa_images/[^\)]+\)',
17
+ r'<span style="display:inline-block;background:#f3f4f6;border:1px solid'
18
+ r' #d1d5db;border-radius:4px;padding:1px 7px;font-size:12px;color:#6b7280">📷 \1</span>',
19
+ text,
20
+ )
21
+ return _md.markdown(text, extensions=["nl2br", "tables"])
22
+ except ImportError:
23
+ def _md2html(text: str) -> str:
24
+ return text.replace("\n", "<br>")
25
 
26
  _REPO_ROOT = os.path.dirname(os.path.abspath(__file__))
27
  sys.path.insert(0, _REPO_ROOT)
 
135
 
136
  cards = []
137
  for i, item in enumerate(items):
138
+ q_html = _md2html(str(item.get("question", "")))
139
+ a_html = _md2html(str(item.get("answer", "")))
140
+ sol_raw = str(item.get("solution", ""))
141
+ name = item.get("name", "")
142
+
143
+ # Truncate solution before converting (avoids cutting mid-tag)
144
+ sol_short = (sol_raw[:400] + "\n\n…") if len(sol_raw) > 400 else sol_raw
145
+ sol_html = _md2html(sol_short)
146
  sol_block = (
147
+ f'<div style="margin-top:12px;padding-top:10px;border-top:1px solid #e5e7eb">'
148
  f'<span style="font-weight:600;color:#374151">{label_s}:</span>'
149
+ f'<div class="md-body" style="margin-top:6px;font-size:13px;color:#4b5563">{sol_html}</div>'
150
  f'</div>'
151
+ ) if sol_raw and sol_raw != item.get("answer", "") else ""
152
 
153
  cards.append(f"""
154
  <div style="border:1px solid #e5e7eb;border-radius:12px;padding:18px;margin-bottom:12px;
155
  background:#ffffff;box-shadow:0 1px 4px rgba(0,0,0,.06);">
156
  <div style="font-size:11px;color:#9ca3af;margin-bottom:10px">#{i+1} &nbsp;·&nbsp; {name}</div>
157
+ <div style="margin-bottom:12px">
158
  <span style="font-weight:600;color:#111827">{label_q}:</span>
159
+ <div class="md-body" style="margin-top:6px;font-size:14px">{q_html}</div>
160
  </div>
161
+ <div style="background:#f0fdf4;border-radius:8px;padding:12px">
162
  <span style="font-weight:600;color:#15803d">{label_a}:</span>
163
+ <div class="md-body" style="margin-top:6px;font-size:14px;color:#166534">{a_html}</div>
164
  </div>
165
  {sol_block}
166
  </div>""")
 
352
  #title-row { align-items: center; }
353
  #lang-btn { min-width: 90px; }
354
  .example-btn { margin: 4px 0 !important; }
355
+ .md-body p { margin: 0 0 6px; }
356
+ .md-body ul, .md-body ol { margin: 4px 0 4px 18px; padding: 0; }
357
+ .md-body li { margin-bottom: 2px; }
358
+ .md-body code { background:#f3f4f6; border-radius:3px; padding:1px 4px; font-size:12px; }
359
+ .md-body pre { background:#f3f4f6; border-radius:6px; padding:8px; overflow-x:auto; font-size:12px; }
360
+ .md-body table { border-collapse:collapse; width:100%; font-size:13px; }
361
+ .md-body th, .md-body td { border:1px solid #e5e7eb; padding:4px 8px; }
362
+ .md-body th { background:#f9fafb; }
363
  """
364
 
365
  _L = _DEFAULT_LANG # shorthand for initial render
requirements.txt CHANGED
@@ -4,6 +4,7 @@ open-dataflow[pdf2vqa] @ git+https://github.com/OpenDCAI/DataFlow.git
4
  # Runtime dependencies used by curate_data.py
5
  json5
6
  pandas
 
7
 
8
  # Pin gradio>=5.10 to avoid the bool-schema crash in gradio_client/utils.py
9
  # (open-dataflow requires gradio>5, HF Spaces pins to sdk_version above)
 
4
  # Runtime dependencies used by curate_data.py
5
  json5
6
  pandas
7
+ markdown
8
 
9
  # Pin gradio>=5.10 to avoid the bool-schema crash in gradio_client/utils.py
10
  # (open-dataflow requires gradio>5, HF Spaces pins to sdk_version above)