import gradio as gr from pal import solve_pal from mathprompter import solve_mp from TA import solve_ta from utils import run_code def run(question, method): if method == "PAL": code_op, generated_code = solve_pal(question) if code_op is not None: return code_op, gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True) else: return ( "Code execution failed, please review it from below and re-run it or try-asking again with more details", gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True)) elif method == "TA": code_op, generated_code = solve_ta(question) if code_op is not None: return code_op, gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True) else: return ( "Code execution failed, please review it from below and re-run it or try-asking again with more details", gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True)) elif method == "MathPrompter": exp_op, code_op, generated_code, generated_exp = solve_mp(question) display_value = generated_exp + "\n\n" + generated_code if code_op is not None: ans = f"Answer from Expression execution: {exp_op} \nAnswer from Code execution: {code_op} " return ans, gr.Code.update(value=display_value, interactive=True), gr.Button.update(visible=True) else: return ( "Code execution failed, please review it from below and re-run it or try-asking again with more details", gr.Code.update(value=display_value, interactive=True), gr.Button.update(visible=True)) else: raise gr.Error("Please select the evaluating strategy from dropdown") def run_edits(code): if "input(" in code: return "Code execution failed, Please remove any input statement or bugs", code try: code_op = run_code(code) return code_op, code except: return "Code execution failed, please review it from below and re-run it or try-asking again with more details", code # User Interface Part theme = gr.themes.Monochrome( primary_hue="indigo", secondary_hue="blue", neutral_hue="slate", radius_size=gr.themes.sizes.radius_sm, font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"], ) demo = gr.Blocks(title="Reasoning with StarCoder 💫", theme=theme) def render_instruction(mtd): if mtd == "PAL": return gr.Textbox.update( value=''' 💫 Query can be an instruction or a direct question starting with What, Find, etc. 💫 Use numbers wherever possible, i.e use 2 instead of two 💫 Example: What is the value of sin(30)? ''', visible=True ) if mtd == "TA": return gr.Textbox.update( value=''' 💫 Query should be a direct instruction or a question, try to provide much context as possible 💫 Use numbers wherever possible, i.e use 2 instead of two 💫 Example: Write the code to find 7th Fibonacci number. ''', visible=True ) if mtd == "MathPrompter": return gr.Textbox.update( value=''' 💫 Query should be a direct question, can start by giving a context and then asking the intended segment. 💫 Use numbers wherever possible, i.e use 2 instead of two 💫 Example: The dimensions of the input image are 80x80, if the filter of 3x3 size is convoluted on the image, what are the dimensions of output image? ''', visible=True ) description = '''