unijoh commited on
Commit
139eae0
·
verified ·
1 Parent(s): 6d15942

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -29
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os, re, string, json
 
2
  import inspect
3
  import importlib.resources as importlib_resources
4
  from collections import defaultdict
@@ -163,25 +164,36 @@ CSS = """
163
  height:auto !important;
164
  }
165
 
166
- /* Dataframes: never wrap any cell; allow sideways scroll instead. */
167
- #out_df, #out_mean_df{
168
- max-width:100% !important;
169
- }
170
- /* The scroll container is not always the outer div in Gradio, so target common wrappers too */
171
- #out_df, #out_mean_df,
172
- #out_df .table-wrap, #out_mean_df .table-wrap,
173
- #out_df .wrap, #out_mean_df .wrap{
174
  overflow-x:auto !important;
175
- display:block !important;
176
  }
177
- #out_df table, #out_mean_df table{
178
- table-layout:auto !important;
179
  width:max-content !important;
180
  min-width:100% !important;
181
  }
182
  #out_df th, #out_df td,
183
  #out_mean_df th, #out_mean_df td{
184
  white-space:nowrap !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  }
186
  """
187
 
@@ -596,19 +608,37 @@ def run_model(sentence: str):
596
  vec_i += 1
597
  return rows
598
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
599
  def render(rows_state, lang: str):
600
  lang = "fo" if lang=="fo" else "en"
601
- df_cols = [UI[lang]["w"], UI[lang]["t"], UI[lang]["s"]]
602
- dfm_cols = [UI[lang]["w"], UI[lang]["t"], UI[lang]["m"]]
603
  if not rows_state:
604
- return (pd.DataFrame(columns=df_cols), pd.DataFrame(columns=dfm_cols), build_overview(lang))
 
605
  out_main, out_mean = [], []
606
  for r in rows_state:
607
  vec = torch.tensor(r["vec"])
608
  tag = vector_to_tag(vec)
609
  out_main.append([r["word"], tag, analysis_text(vec, lang)])
610
  out_mean.append([r["word"], tag, expanded_text(vec, lang)])
611
- return (pd.DataFrame(out_main, columns=df_cols), pd.DataFrame(out_mean, columns=dfm_cols), build_overview(lang))
 
612
 
613
  with gr.Blocks(css=CSS, title="Marka") as demo:
614
  with gr.Row(equal_height=False):
@@ -633,23 +663,11 @@ with gr.Blocks(css=CSS, title="Marka") as demo:
633
  btn_lang_fo_off = gr.Button("Føroyskt", variant="secondary", elem_id="lang_fo_off", visible=False)
634
  btn_lang_en_on = gr.Button("English", variant="primary", elem_id="lang_en_on", visible=False)
635
  btn_lang_en_off = gr.Button("English", variant="secondary", elem_id="lang_en_off", visible=False)
636
-
637
- out_df = gr.Dataframe(
638
- elem_id="out_df",
639
- value=pd.DataFrame(columns=[UI["fo"]["w"], UI["fo"]["t"], UI["fo"]["s"]]),
640
- wrap=False, interactive=False, show_label=False,
641
- row_count=(0, "fixed"), col_count=(3, "fixed"),
642
- visible=False,
643
- )
644
 
645
  expanded_acc = gr.Accordion("Útgreinað marking / Expanded tags", open=False, visible=False)
646
  with expanded_acc:
647
- out_mean_df = gr.Dataframe(
648
- elem_id="out_mean_df",
649
- value=pd.DataFrame(columns=[UI["fo"]["w"], UI["fo"]["t"], UI["fo"]["m"]]),
650
- wrap=False, interactive=False, show_label=False,
651
- row_count=(0, "fixed"), col_count=(3, "fixed"),
652
- )
653
 
654
  overview_acc = gr.Accordion("Markayvirlit / Tag Overview", open=False, visible=True)
655
  with overview_acc:
 
1
  import os, re, string, json
2
+ import html
3
  import inspect
4
  import importlib.resources as importlib_resources
5
  from collections import defaultdict
 
164
  height:auto !important;
165
  }
166
 
167
+ /* Results tables (rendered as HTML so we fully control wrapping/scrolling). */
168
+ #out_df .df-scroll, #out_mean_df .df-scroll{
 
 
 
 
 
 
169
  overflow-x:auto !important;
170
+ width:100% !important;
171
  }
172
+ #out_df table.df-table, #out_mean_df table.df-table{
173
+ border-collapse:collapse !important;
174
  width:max-content !important;
175
  min-width:100% !important;
176
  }
177
  #out_df th, #out_df td,
178
  #out_mean_df th, #out_mean_df td{
179
  white-space:nowrap !important;
180
+ padding:10px 12px !important;
181
+ border:1px solid rgba(0,0,0,0.12) !important;
182
+ text-align:left !important;
183
+ vertical-align:top !important;
184
+ }
185
+ #out_df thead th, #out_mean_df thead th{
186
+ font-weight:600 !important;
187
+ background: rgba(0,0,0,0.03) !important;
188
+ }
189
+ @media (prefers-color-scheme: dark){
190
+ #out_df th, #out_df td,
191
+ #out_mean_df th, #out_mean_df td{
192
+ border:1px solid rgba(255,255,255,0.14) !important;
193
+ }
194
+ #out_df thead th, #out_mean_df thead th{
195
+ background: rgba(255,255,255,0.06) !important;
196
+ }
197
  }
198
  """
199
 
 
608
  vec_i += 1
609
  return rows
610
 
611
+ def _make_html_table(headers, rows):
612
+ # We render results as plain HTML so we can force:
613
+ # - no wrapping anywhere
614
+ # - horizontal scrolling when content is wider than the page
615
+ th = "".join(f"<th>{html.escape(str(h))}</th>" for h in headers)
616
+ body_rows = []
617
+ for row in rows:
618
+ tds = "".join(f"<td>{html.escape(str(c))}</td>" for c in row)
619
+ body_rows.append(f"<tr>{tds}</tr>")
620
+ body = "".join(body_rows)
621
+ return (
622
+ '<div class="df-scroll">'
623
+ f'<table class="df-table"><thead><tr>{th}</tr></thead><tbody>{body}</tbody></table>'
624
+ '</div>'
625
+ )
626
+
627
  def render(rows_state, lang: str):
628
  lang = "fo" if lang=="fo" else "en"
629
+ cols_main = [UI[lang]["w"], UI[lang]["t"], UI[lang]["s"]]
630
+ cols_mean = [UI[lang]["w"], UI[lang]["t"], UI[lang]["m"]]
631
  if not rows_state:
632
+ return (_make_html_table(cols_main, []), _make_html_table(cols_mean, []), build_overview(lang))
633
+
634
  out_main, out_mean = [], []
635
  for r in rows_state:
636
  vec = torch.tensor(r["vec"])
637
  tag = vector_to_tag(vec)
638
  out_main.append([r["word"], tag, analysis_text(vec, lang)])
639
  out_mean.append([r["word"], tag, expanded_text(vec, lang)])
640
+
641
+ return (_make_html_table(cols_main, out_main), _make_html_table(cols_mean, out_mean), build_overview(lang))
642
 
643
  with gr.Blocks(css=CSS, title="Marka") as demo:
644
  with gr.Row(equal_height=False):
 
663
  btn_lang_fo_off = gr.Button("Føroyskt", variant="secondary", elem_id="lang_fo_off", visible=False)
664
  btn_lang_en_on = gr.Button("English", variant="primary", elem_id="lang_en_on", visible=False)
665
  btn_lang_en_off = gr.Button("English", variant="secondary", elem_id="lang_en_off", visible=False)
666
+ out_df = gr.HTML(value="", elem_id="out_df", visible=False)
 
 
 
 
 
 
 
667
 
668
  expanded_acc = gr.Accordion("Útgreinað marking / Expanded tags", open=False, visible=False)
669
  with expanded_acc:
670
+ out_mean_df = gr.HTML(value="", elem_id="out_mean_df")
 
 
 
 
 
671
 
672
  overview_acc = gr.Accordion("Markayvirlit / Tag Overview", open=False, visible=True)
673
  with overview_acc: