ranWang commited on
Commit
5d15022
1 Parent(s): edbd97e

Display loading animation at runtime

Browse files
Files changed (2) hide show
  1. app.css +33 -1
  2. app.py +51 -6
app.css CHANGED
@@ -104,11 +104,43 @@
104
  }
105
 
106
  .solution-content .solution-content {
107
- overflow-y: auto;
108
  padding: 12px;
 
109
  }
110
 
111
  .run-btn {
112
  background: linear-gradient(to right, #ce7e53, #bb470b);
113
  color: white;
114
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  }
105
 
106
  .solution-content .solution-content {
107
+ overflow-y: scroll;
108
  padding: 12px;
109
+ scroll-snap-align: end;
110
  }
111
 
112
  .run-btn {
113
  background: linear-gradient(to right, #ce7e53, #bb470b);
114
  color: white;
115
  }
116
+
117
+ .running-btn {
118
+ background: linear-gradient(to right, #ce7e53, #bb470b);
119
+ color: white;
120
+ display: flex;
121
+ align-items: center;
122
+ justify-content: center;
123
+ position: relative;
124
+ }
125
+
126
+ .running-btn::before {
127
+ content: "";
128
+ position: absolute;
129
+ width: 22px;
130
+ height: 22px;
131
+ border-radius: 50%;
132
+ border: 3px solid white;
133
+ border-top-color: transparent;
134
+ /* border-right-color: transparent;
135
+ border-bottom-color: transparent; */
136
+ animation: spin 1s linear infinite;
137
+ }
138
+
139
+ @keyframes spin {
140
+ 0% {
141
+ transform: rotate(0deg);
142
+ }
143
+ 100% {
144
+ transform: rotate(360deg);
145
+ }
146
+ }
app.py CHANGED
@@ -661,6 +661,9 @@ def process_code(
661
 
662
 
663
  def solve_problem(problem, temperature, progress=gr.Progress()):
 
 
 
664
  problem = apply_template({"prompt": problem}, prompt=config.system_prompt)
665
  print(f"Problem: {problem}")
666
 
@@ -690,9 +693,10 @@ def solve_problem(problem, temperature, progress=gr.Progress()):
690
  for reponse_message, error in generate(messages, temperature):
691
  if reponse_message is not None:
692
  step_reponse += reponse_message
693
- yield step_reponse
694
 
695
  if error:
 
696
  return
697
 
698
  sample["gen_texts"] = step_reponse
@@ -711,12 +715,12 @@ def solve_problem(problem, temperature, progress=gr.Progress()):
711
  for output_mseeage in run_code_reponse:
712
  if output_mseeage is not None:
713
  step_reponse += output_mseeage
714
- yield step_reponse
715
 
716
  if sample["should_prune"]:
717
  break
718
 
719
- yield sample["gen_texts"]
720
 
721
 
722
  example_data = datasets.load_dataset(
@@ -754,7 +758,11 @@ def clear():
754
  return "", 0.1, "", problem_example_text_display, problem_example_text
755
 
756
 
 
 
757
  with gr.Blocks(css=css, title="Math Olympiad Solver") as demo:
 
 
758
  problem_example_text, problem_example_text_display = update_example_problem()
759
 
760
  with gr.Row(elem_classes="title"):
@@ -789,16 +797,51 @@ with gr.Blocks(css=css, title="Math Olympiad Solver") as demo:
789
  with gr.Accordion("Advanced Options", open=False):
790
  temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.1, step=0.1, label="Temperature")
791
 
792
- with gr.Row():
793
- btn_clear = gr.Button("Clear")
794
  btn_run = gr.Button("Run", elem_classes="run-btn")
 
 
795
 
796
  with gr.Column(scale=1, elem_classes="right"):
797
  gr.HTML("Solution", elem_classes="solution-title-content")
798
  out = gr.Markdown(elem_classes="solution-content", latex_delimiters=latex_delimiters)
799
 
800
  problem_example_text_hidden = gr.Markdown(value=problem_example_text, visible=False)
801
- btn_run.click(fn=solve_problem, inputs=[inp, temperature], outputs=out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
802
 
803
  copy_btn.click(fn=lambda example: example, inputs=[problem_example_text_hidden], outputs=[inp])
804
 
@@ -814,6 +857,8 @@ with gr.Blocks(css=css, title="Math Olympiad Solver") as demo:
814
  ],
815
  )
816
 
 
 
817
  demo.load(
818
  update_example_problem,
819
  inputs=None,
 
661
 
662
 
663
  def solve_problem(problem, temperature, progress=gr.Progress()):
664
+ """
665
+ yield token: string, stop: bool
666
+ """
667
  problem = apply_template({"prompt": problem}, prompt=config.system_prompt)
668
  print(f"Problem: {problem}")
669
 
 
693
  for reponse_message, error in generate(messages, temperature):
694
  if reponse_message is not None:
695
  step_reponse += reponse_message
696
+ yield step_reponse, False
697
 
698
  if error:
699
+ yield step_reponse, True
700
  return
701
 
702
  sample["gen_texts"] = step_reponse
 
715
  for output_mseeage in run_code_reponse:
716
  if output_mseeage is not None:
717
  step_reponse += output_mseeage
718
+ yield step_reponse, False
719
 
720
  if sample["should_prune"]:
721
  break
722
 
723
+ yield sample["gen_texts"], True
724
 
725
 
726
  example_data = datasets.load_dataset(
 
758
  return "", 0.1, "", problem_example_text_display, problem_example_text
759
 
760
 
761
+ running_success = False
762
+
763
  with gr.Blocks(css=css, title="Math Olympiad Solver") as demo:
764
+ btn_list = []
765
+
766
  problem_example_text, problem_example_text_display = update_example_problem()
767
 
768
  with gr.Row(elem_classes="title"):
 
797
  with gr.Accordion("Advanced Options", open=False):
798
  temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.1, step=0.1, label="Temperature")
799
 
800
+ with gr.Row() as btn_area:
801
+ btn_clear = gr.Button("Clear", elem_classes="clear-btn")
802
  btn_run = gr.Button("Run", elem_classes="run-btn")
803
+ btn_list.append(btn_clear)
804
+ btn_list.append(btn_run)
805
 
806
  with gr.Column(scale=1, elem_classes="right"):
807
  gr.HTML("Solution", elem_classes="solution-title-content")
808
  out = gr.Markdown(elem_classes="solution-content", latex_delimiters=latex_delimiters)
809
 
810
  problem_example_text_hidden = gr.Markdown(value=problem_example_text, visible=False)
811
+
812
+ def solve_problem_wrapper(inp_text, temperature):
813
+ global running_success
814
+ for token, stop in solve_problem(inp_text, temperature):
815
+ yield token
816
+
817
+ if stop:
818
+ running_success = True
819
+
820
+ def mount_run_btn(btn):
821
+ btn.click(fn=solve_problem_wrapper, inputs=[inp, temperature], outputs=out)
822
+ btn.click(get_running_btns, None, outputs=btn_list)
823
+
824
+ def get_running_btns():
825
+ global running_success
826
+
827
+ btn_clear = gr.Button("Clear")
828
+ btn_run = gr.Button("", elem_classes="run-btn running-btn")
829
+ yield [btn_clear, btn_run]
830
+
831
+ time.sleep(3)
832
+
833
+ btn_clear = gr.Button("Clear")
834
+ btn_run = gr.Button("Run", elem_classes="run-btn")
835
+
836
+ while True:
837
+ if running_success:
838
+ running_success = False
839
+ yield [btn_clear, btn_run]
840
+ time.sleep(1)
841
+ mount_run_btn(btn_run)
842
+ break
843
+
844
+ time.sleep(1)
845
 
846
  copy_btn.click(fn=lambda example: example, inputs=[problem_example_text_hidden], outputs=[inp])
847
 
 
857
  ],
858
  )
859
 
860
+ mount_run_btn(btn_run)
861
+
862
  demo.load(
863
  update_example_problem,
864
  inputs=None,