RJ3vans commited on
Commit
9d6eabc
·
verified ·
1 Parent(s): cc3a7c4

Update app.py

Browse files

Code modified to output the simplified sentence using a DAG.

Files changed (1) hide show
  1. app.py +59 -0
app.py CHANGED
@@ -49,6 +49,8 @@ from collections import defaultdict
49
 
50
  import html
51
 
 
 
52
  PRONOUNS = {
53
  "he", "she", "it", "they", "him", "her", "them",
54
  "his", "hers", "its", "their", "theirs",
@@ -2731,6 +2733,57 @@ def expand_paren_lists(sent: str, lists: list[list[str]]) -> list[str]:
2731
  return outs
2732
  ###############################################################################
2733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2734
  ###############################################################################
2735
  @spaces.GPU(duration=120)
2736
  def process_text(input_text):
@@ -3033,6 +3086,12 @@ def process_text(input_text):
3033
  if para_simplified:
3034
  para_simplified = order_sentences_for_discourse(para_simplified)
3035
  output_paragraphs.append(" ".join(para_simplified))
 
 
 
 
 
 
3036
 
3037
  final_output = "\n\n".join(output_paragraphs)
3038
  print(f"\n=== FINAL OUTPUT ===\n{final_output}")
 
49
 
50
  import html
51
 
52
+ from graphviz import Digraph
53
+
54
  PRONOUNS = {
55
  "he", "she", "it", "they", "him", "her", "them",
56
  "his", "hers", "its", "their", "theirs",
 
2733
  return outs
2734
  ###############################################################################
2735
 
2736
+ ###############################################################################
2737
+ def build_shared_dag(sentences: list[str]):
2738
+ """
2739
+ Nodes are keyed by the remaining word tuple, so any common
2740
+ suffix (or whole remaining span) is a single node.
2741
+ """
2742
+ cache = {} # tuple[str, ...] -> node_id
2743
+ labels = {} # node_id -> str
2744
+ edges = set() # (src, dst)
2745
+
2746
+ def node_for(words: tuple[str, ...]) -> str:
2747
+ if words in cache:
2748
+ return cache[words]
2749
+ nid = f"n{len(cache)}"
2750
+ cache[words] = nid
2751
+ labels[nid] = " ".join(words) if words else "•"
2752
+ return nid
2753
+
2754
+ root = node_for(())
2755
+ for sent in sentences:
2756
+ words = tuple(sent.rstrip(".").split())
2757
+ prev = root
2758
+ for i in range(len(words)):
2759
+ # edge into the shared node for the remaining span
2760
+ nxt = node_for(words[i:])
2761
+ edges.add((prev, nxt))
2762
+ prev = nxt
2763
+ edges.add((prev, node_for(()))) # terminal
2764
+
2765
+ return labels, edges
2766
+
2767
+ ###############################################################################
2768
+ def render_dag(sentences: list[str], path: str = "simp_dag") -> str:
2769
+ labels, edges = build_shared_dag(sentences)
2770
+ g = Digraph("simp", format="svg")
2771
+ g.attr(rankdir="TB", bgcolor="white")
2772
+ g.attr("node", shape="box", style="rounded,filled",
2773
+ fillcolor="#f7f7f7", fontname="Helvetica", fontsize="11")
2774
+ g.attr("edge", color="#555555")
2775
+ for nid, lab in labels.items():
2776
+ if lab == "•":
2777
+ continue
2778
+ g.node(nid, lab)
2779
+ for a, b in edges:
2780
+ if labels.get(a) == "•" or labels.get(b) == "•":
2781
+ continue
2782
+ g.edge(a, b)
2783
+ g.render(path, cleanup=True)
2784
+ return path + ".svg"
2785
+ ###############################################################################
2786
+
2787
  ###############################################################################
2788
  @spaces.GPU(duration=120)
2789
  def process_text(input_text):
 
3086
  if para_simplified:
3087
  para_simplified = order_sentences_for_discourse(para_simplified)
3088
  output_paragraphs.append(" ".join(para_simplified))
3089
+
3090
+ svg_path = render_dag(para_simplified)
3091
+ # return the SVG (or a PNG) instead of, or beside, the flat text
3092
+ with open(svg_path) as f:
3093
+ svg = f.read()
3094
+ return f'<div class="simp-out">{svg}</div>'
3095
 
3096
  final_output = "\n\n".join(output_paragraphs)
3097
  print(f"\n=== FINAL OUTPUT ===\n{final_output}")