jgyasu commited on
Commit
a4dbf67
1 Parent(s): e6b16e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -440,15 +440,26 @@ def generate_tree(original_sentence: str) -> str:
440
  masked_sentence = mask_non_stopword(first_paraphrased_sentence)
441
  masked_versions = mask(masked_sentence)
442
  dot = graphviz.Digraph()
443
- dot.attr(rankdir='LR', size='8,10!', dpi='72')
444
- dot.node("Original", original_sentence)
445
- dot.node("Paraphrased", first_paraphrased_sentence)
446
- dot.edge("Original", "Paraphrased")
447
- for i, masked in enumerate(masked_versions):
448
- node_id = f"Masked_{i}"
449
- dot.node(node_id, masked)
450
- dot.edge("Paraphrased", node_id)
451
- return masked_sentence, dot.source
 
 
 
 
 
 
 
 
 
 
 
452
 
453
  # Function for the Gradio interface
454
  def model(prompt):
@@ -459,11 +470,11 @@ def model(prompt):
459
  for i in range(len(common_subs)):
460
  common_subs[i]["Paraphrased Sentence"] = res[i]
461
  result = highlight_phrases_with_colors(res, common_grams)
462
- masked_sentence, tree_source = generate_tree(sentence)
463
  graph = graphviz.Source(tree_source)
464
- svg_content = graph.pipe(format='svg').decode('utf-8')
465
  # tree = f'<div style="width: 100%; overflow-x: auto;">{svg_content}</div>'
466
- return generated, generated, result, masked_sentence, svg_content
467
 
468
  with gr.Blocks(theme = gr.themes.Monochrome()) as demo:
469
  gr.Markdown("# Paraphrases the Text and Highlights the Non-melting Points")
@@ -488,12 +499,15 @@ with gr.Blocks(theme = gr.themes.Monochrome()) as demo:
488
  masked_sentence = gr.Textbox(label="Masked Sentence")
489
 
490
  with gr.Row():
491
- tree = gr.HTML(label="Tree")
 
 
 
492
 
493
- submit_button.click(model, inputs=user_input, outputs=[ai_output, selected_sentence, html_output, masked_sentence, tree])
494
  clear_button.click(lambda: "", inputs=None, outputs=user_input)
495
- clear_button.click(lambda: "", inputs=None, outputs=[ai_output, selected_sentence, html_output, masked_sentence, tree])
496
 
497
  # Launch the demo
498
- demo.launch()
499
 
 
440
  masked_sentence = mask_non_stopword(first_paraphrased_sentence)
441
  masked_versions = mask(masked_sentence)
442
  dot = graphviz.Digraph()
443
+ dot.attr(rankdir='LR', size='10,10', dpi=' 2743')
444
+
445
+ existing_nodes = set()
446
+
447
+ def add_paraphrases(parent, paraphrases):
448
+ if parent not in existing_nodes:
449
+ dot.node(parent, parent, shape='box')
450
+ existing_nodes.add(parent)
451
+
452
+ for paraphrase in paraphrases:
453
+ if paraphrase not in existing_nodes:
454
+ dot.node(paraphrase, paraphrase, shape='box')
455
+ existing_nodes.add(paraphrase)
456
+ dot.edge(parent, paraphrase)
457
+
458
+ add_paraphrases(original_sentence, paraphrased_sentences) #whenever a new branch is to be created call this function along with the original sentence and the list of its paraphrases
459
+ add_paraphrases(paraphrased_sentences[0], masked_versions)
460
+
461
+ graph_path = dot.render(filename='paraphrase_tree_dynamic', format='png')
462
+ return masked_sentence, masked_versions, dot.source
463
 
464
  # Function for the Gradio interface
465
  def model(prompt):
 
470
  for i in range(len(common_subs)):
471
  common_subs[i]["Paraphrased Sentence"] = res[i]
472
  result = highlight_phrases_with_colors(res, common_grams)
473
+ masked_sentence, masked_versions, tree_source = generate_tree(sentence)
474
  graph = graphviz.Source(tree_source)
475
+ png_content = graph.render(filename='paraphrase_tree_dynamic', format='png')
476
  # tree = f'<div style="width: 100%; overflow-x: auto;">{svg_content}</div>'
477
+ return generated, generated, result, masked_sentence, masked_versions, png_content
478
 
479
  with gr.Blocks(theme = gr.themes.Monochrome()) as demo:
480
  gr.Markdown("# Paraphrases the Text and Highlights the Non-melting Points")
 
499
  masked_sentence = gr.Textbox(label="Masked Sentence")
500
 
501
  with gr.Row():
502
+ masked_versions = gr.Textbox(label="Sentence Generated by Masking Model")
503
+
504
+ with gr.Row():
505
+ tree = gr.Image(label="Paraphrase Tree")
506
 
507
+ submit_button.click(model, inputs=user_input, outputs=[ai_output, selected_sentence, html_output, masked_sentence, masked_versions, tree])
508
  clear_button.click(lambda: "", inputs=None, outputs=user_input)
509
+ clear_button.click(lambda: "", inputs=None, outputs=[ai_output, selected_sentence, html_output, masked_sentence, masked_versions, tree])
510
 
511
  # Launch the demo
512
+ demo.launch(share=True)
513