Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,9 @@ import os
|
|
| 3 |
from dataclasses import dataclass, field
|
| 4 |
from typing import List, Optional, Dict
|
| 5 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
import numpy as np
|
| 8 |
import pandas as pd
|
|
@@ -155,8 +158,8 @@ def get_path():
|
|
| 155 |
def display_dialogue(hf_path: str):
|
| 156 |
with hf_fs.open(hf_path, "rb") as f:
|
| 157 |
txt = f.read().decode("utf-8")
|
| 158 |
-
|
| 159 |
-
|
| 160 |
#display_dialogue_with_numbers(txt)
|
| 161 |
#st.markdown(
|
| 162 |
# f"<details><summary><b>Herr Laus</b></summary><div>{txt}</div></details><br>",
|
|
@@ -165,38 +168,68 @@ def display_dialogue(hf_path: str):
|
|
| 165 |
import html as py_html
|
| 166 |
from streamlit.components.v1 import html as st_html
|
| 167 |
|
| 168 |
-
def
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
|
| 198 |
-
#st.markdown(f"**{title}**")
|
| 199 |
-
st.code(txt, language="text", line_numbers=True)
|
| 200 |
|
| 201 |
def display_image(image_path):
|
| 202 |
with hf_fs.open(image_path) as f:
|
|
@@ -253,7 +286,7 @@ def show_field(f: Field, index: int, data_collected):
|
|
| 253 |
display_image(os.path.join(input_repo_path, 'images', value))
|
| 254 |
elif f.name == 'dialogue_name':
|
| 255 |
#display_dialogue(os.path.join(input_repo_path, 'dialogues', value))
|
| 256 |
-
|
| 257 |
elif f.name == 'patient':
|
| 258 |
st.markdown(f"**Patient:** {value}")
|
| 259 |
else:
|
|
|
|
| 3 |
from dataclasses import dataclass, field
|
| 4 |
from typing import List, Optional, Dict
|
| 5 |
from PIL import Image
|
| 6 |
+
import html as py_html
|
| 7 |
+
import re
|
| 8 |
+
import uuid
|
| 9 |
|
| 10 |
import numpy as np
|
| 11 |
import pandas as pd
|
|
|
|
| 158 |
def display_dialogue(hf_path: str):
|
| 159 |
with hf_fs.open(hf_path, "rb") as f:
|
| 160 |
txt = f.read().decode("utf-8")
|
| 161 |
+
txt = load_text(hf_path)
|
| 162 |
+
display_numbered_markdown_container(txt, width_chars=80, max_height_px=500)
|
| 163 |
#display_dialogue_with_numbers(txt)
|
| 164 |
#st.markdown(
|
| 165 |
# f"<details><summary><b>Herr Laus</b></summary><div>{txt}</div></details><br>",
|
|
|
|
| 168 |
import html as py_html
|
| 169 |
from streamlit.components.v1 import html as st_html
|
| 170 |
|
| 171 |
+
def _md_inline_to_html(line: str) -> str:
|
| 172 |
+
"""
|
| 173 |
+
Minimal inline Markdown: **bold**, *italic*.
|
| 174 |
+
(Avoids bringing a full markdown parser; extend if needed.)
|
| 175 |
+
"""
|
| 176 |
+
esc = py_html.escape(line)
|
| 177 |
+
# bold: **text**
|
| 178 |
+
esc = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", esc)
|
| 179 |
+
# italic: *text* (ignore **bold** we already handled)
|
| 180 |
+
esc = re.sub(r"(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)", r"<em>\1</em>", esc)
|
| 181 |
+
return esc
|
| 182 |
+
|
| 183 |
+
def display_numbered_markdown_container(text: str,
|
| 184 |
+
title: str = "Dialogue",
|
| 185 |
+
width_chars: int = 80,
|
| 186 |
+
max_height_px: int = 520):
|
| 187 |
+
"""
|
| 188 |
+
Render a numbered, readable block with inline bold/italic preserved,
|
| 189 |
+
wrapped to `width_chars` and scrollable within a bordered container.
|
| 190 |
+
"""
|
| 191 |
+
block_id = f"dlg-{uuid.uuid4().hex[:8]}"
|
| 192 |
+
lines = text.replace("\r\n", "\n").replace("\r", "\n").split("\n")
|
| 193 |
+
|
| 194 |
+
# Convert each source line to HTML while preserving inline bold/italic
|
| 195 |
+
rows = []
|
| 196 |
+
for i, line in enumerate(lines, 1):
|
| 197 |
+
html_line = _md_inline_to_html(line) if line.strip() else " "
|
| 198 |
+
rows.append(f'''
|
| 199 |
+
<div class="row">
|
| 200 |
+
<span class="num">{i}</span>
|
| 201 |
+
<span class="txt">{html_line}</span>
|
| 202 |
+
</div>
|
| 203 |
+
''')
|
| 204 |
+
body = "\n".join(rows)
|
| 205 |
+
|
| 206 |
+
st.markdown(f"""
|
| 207 |
+
<style>
|
| 208 |
+
#{block_id} {{
|
| 209 |
+
border: 1px solid #e6e6e6; border-radius: .5rem; padding: .75rem;
|
| 210 |
+
max-height: {max_height_px}px; overflow-y: auto; background: #fff;
|
| 211 |
+
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
|
| 212 |
+
line-height: 1.5;
|
| 213 |
+
}}
|
| 214 |
+
#{block_id} .row {{
|
| 215 |
+
display: grid; grid-template-columns: 4ch 1fr; column-gap: 1ch; align-items: start;
|
| 216 |
+
margin: 0; padding: 0;
|
| 217 |
+
}}
|
| 218 |
+
#{block_id} .num {{
|
| 219 |
+
color: rgba(0,0,0,.55); text-align: right; user-select: none;
|
| 220 |
+
}}
|
| 221 |
+
/* Wrap the text column to a visual width of `width_chars` characters */
|
| 222 |
+
#{block_id} .txt {{
|
| 223 |
+
max-width: {width_chars}ch; white-space: pre-wrap; word-break: break-word;
|
| 224 |
+
}}
|
| 225 |
+
</style>
|
| 226 |
+
""", unsafe_allow_html=True)
|
| 227 |
+
|
| 228 |
+
with st.container(border=True):
|
| 229 |
+
#st.markdown(f"**{title}** \n*(wrapped to {width_chars} chars)*")
|
| 230 |
+
st.markdown(f'<div id="{block_id}">{body}</div>', unsafe_allow_html=True)
|
| 231 |
|
| 232 |
+
|
|
|
|
|
|
|
| 233 |
|
| 234 |
def display_image(image_path):
|
| 235 |
with hf_fs.open(image_path) as f:
|
|
|
|
| 286 |
display_image(os.path.join(input_repo_path, 'images', value))
|
| 287 |
elif f.name == 'dialogue_name':
|
| 288 |
#display_dialogue(os.path.join(input_repo_path, 'dialogues', value))
|
| 289 |
+
display_dialogue(value)
|
| 290 |
elif f.name == 'patient':
|
| 291 |
st.markdown(f"**Patient:** {value}")
|
| 292 |
else:
|