ranWang commited on
Commit
69a51a2
1 Parent(s): bdc939d

Add clear and copy examples problem feature

Browse files
Files changed (1) hide show
  1. app.py +69 -3
app.py CHANGED
@@ -8,6 +8,8 @@ import re
8
  import subprocess
9
  import tempfile
10
  import json
 
 
11
  from typing import Tuple, Dict, Any, List
12
  from sympy import N, simplify
13
  from sympy.parsing.latex import parse_latex
@@ -541,7 +543,7 @@ def generate(message, temperature):
541
  max_tokens=1024,
542
  stop=["```output\n"],
543
  temperature=temperature,
544
- timeout=30
545
  )
546
 
547
  response = stream.response
@@ -715,17 +717,81 @@ def solve_problem(problem, temperature, progress=gr.Progress()):
715
  yield sample["gen_texts"]
716
 
717
 
718
- with gr.Blocks(css=".top-margin { margin-top: 20px; }") as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719
  with gr.Row():
720
  inp = gr.Textbox(placeholder="Problem", label="Problem", lines=5)
721
  with gr.Accordion("Advanced Options", open=False):
722
  temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.1, step=0.1, label="Temperature")
723
  with gr.Row():
724
- out = gr.Markdown(elem_classes=["top-margin"], latex_delimiters=[{"left": "[", "right": "]", "display": True}])
725
 
726
  btn = gr.Button("Run")
 
727
 
728
  btn.click(fn=solve_problem, inputs=[inp, temperature], outputs=out)
 
 
 
729
 
730
 
731
  if __name__ == "__main__":
 
8
  import subprocess
9
  import tempfile
10
  import json
11
+ import datasets
12
+ import random
13
  from typing import Tuple, Dict, Any, List
14
  from sympy import N, simplify
15
  from sympy.parsing.latex import parse_latex
 
543
  max_tokens=1024,
544
  stop=["```output\n"],
545
  temperature=temperature,
546
+ timeout=30,
547
  )
548
 
549
  response = stream.response
 
717
  yield sample["gen_texts"]
718
 
719
 
720
+ def clear_inputs():
721
+ return "", 0.1, ""
722
+
723
+
724
+ example_data = datasets.load_dataset("AI-MO/kaggle-validation-set-medium-extended", split="train")
725
+
726
+
727
+ def get_random_problems():
728
+ examples = random.sample(list(example_data), 2)
729
+ problems = [ex["problem"] for ex in examples]
730
+ return problems[0], problems[1]
731
+
732
+
733
+ def copy_problem_to_input(problem):
734
+ return problem
735
+
736
+
737
+ css = """
738
+ .top-margin {
739
+ margin-top: 20px;
740
+ }
741
+
742
+ .problem-container {
743
+ display: flex;
744
+ align-items: center;
745
+ margin-bottom: 10px;
746
+ border-radius: 8px;
747
+ border: 1px solid rgb(229, 231, 235);
748
+ padding: 10px 12px;
749
+ }
750
+
751
+ # .problem-text {
752
+ # flex: 1;
753
+ # padding: 10px;
754
+ # background-color: #f9f9f9;
755
+ # border: 1px solid #ccc;
756
+ # border-radius: 5px;
757
+ # margin-right: 10px;
758
+ # }
759
+
760
+ .copy-button {
761
+ flex: none;
762
+ }
763
+ """
764
+
765
+ with gr.Blocks(css=css) as demo:
766
+ latex_delimiters = [
767
+ {"left": "[", "right": "]", "display": True},
768
+ ]
769
+
770
+ problem_1_text, problem_2_text = get_random_problems()
771
+
772
+ with gr.Row(elem_classes="problem-container"):
773
+ problem_1_text_contract = problem_1_text[:120] + "..." if len(problem_1_text) > 100 else problem_1_text
774
+ problem_1 = gr.Markdown(value=problem_1_text_contract, latex_delimiters=latex_delimiters)
775
+ copy_btn_1 = gr.Button("Copy", elem_classes="copy-button")
776
+ with gr.Row(elem_classes="problem-container"):
777
+ problem_2_text_contract = problem_2_text[:120] + "..." if len(problem_2_text) > 100 else problem_2_text
778
+ problem_2 = gr.Markdown(value=problem_2_text_contract, latex_delimiters=latex_delimiters)
779
+ copy_btn_2 = gr.Button("Copy", elem_classes="copy-button")
780
+
781
  with gr.Row():
782
  inp = gr.Textbox(placeholder="Problem", label="Problem", lines=5)
783
  with gr.Accordion("Advanced Options", open=False):
784
  temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.1, step=0.1, label="Temperature")
785
  with gr.Row():
786
+ out = gr.Markdown(elem_classes=["top-margin"], latex_delimiters=latex_delimiters)
787
 
788
  btn = gr.Button("Run")
789
+ btn_clear = gr.Button("Clear")
790
 
791
  btn.click(fn=solve_problem, inputs=[inp, temperature], outputs=out)
792
+ btn_clear.click(fn=clear_inputs, inputs=[], outputs=[inp, temperature, out])
793
+ copy_btn_1.click(fn=copy_problem_to_input, inputs=[gr.Markdown(value=problem_1_text, visible=False)], outputs=[inp])
794
+ copy_btn_2.click(fn=copy_problem_to_input, inputs=[gr.Markdown(value=problem_2_text, visible=False)], outputs=[inp])
795
 
796
 
797
  if __name__ == "__main__":