Team Ai
Apppublic

bigcode/Reasoning-with-StarCoder

sourceHugging Faceupdated 3y agoView on Hugging Face
32likes
app.py140 linesDownload Raw Back to root
1import gradio as gr2from pal import solve_pal3from mathprompter import solve_mp4from TA import solve_ta5from utils import run_code6 7 8def run(question, method):9    if method == "PAL":10        code_op, generated_code = solve_pal(question)11        if code_op is not None:12            return code_op, gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True)13        else:14            return (15                "Code execution failed, please review it from below and re-run it or try-asking again with more details",16                gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True))17 18    elif method == "TA":19        code_op, generated_code = solve_ta(question)20        if code_op is not None:21            return code_op, gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True)22        else:23            return (24                "Code execution failed, please review it from below and re-run it or try-asking again with more details",25                gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True))26 27    elif method == "MathPrompter":28        exp_op, code_op, generated_code, generated_exp = solve_mp(question)29        display_value = generated_exp + "\n\n" + generated_code30        if code_op is not None:31            ans = f"Answer from Expression execution: {exp_op} \nAnswer from Code execution: {code_op}  "32            return ans, gr.Code.update(value=display_value, interactive=True), gr.Button.update(visible=True)33        else:34            return (35                "Code execution failed, please review it from below and re-run it or try-asking again with more details",36                gr.Code.update(value=display_value, interactive=True), gr.Button.update(visible=True))37 38    else:39        raise gr.Error("Please select the evaluating strategy from dropdown")40 41 42def run_edits(code):43    if "input(" in code:44        return "Code execution failed, Please remove any input statement or bugs", code45    try:46        code_op = run_code(code)47        return code_op, code48    except:49        return "Code execution failed, please review it from below and re-run it or try-asking again with more details", code50 51 52# User Interface Part53theme = gr.themes.Monochrome(54    primary_hue="indigo",55    secondary_hue="blue",56    neutral_hue="slate",57    radius_size=gr.themes.sizes.radius_sm,58    font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],59)60 61 62demo = gr.Blocks(title="Reasoning with StarCoder ๐Ÿ’ซ", theme=theme)63 64 65def render_instruction(mtd):66    if mtd == "PAL":67        return gr.Textbox.update(68            value='''69            ๐Ÿ’ซ Query can be an instruction or a direct question starting with What, Find, etc.70            ๐Ÿ’ซ Use numbers wherever possible, i.e use 2 instead of two71            ๐Ÿ’ซ Example: What is the value of sin(30)?72            ''',73            visible=True74        )75    if mtd == "TA":76        return gr.Textbox.update(77            value='''78            ๐Ÿ’ซ Query should be a direct instruction or a question, try to provide much context as possible79            ๐Ÿ’ซ Use numbers wherever possible, i.e use 2 instead of two80            ๐Ÿ’ซ Example: Write the  code to find 7th Fibonacci number.81            ''',82            visible=True83        )84    if mtd == "MathPrompter":85        return gr.Textbox.update(86            value='''87            ๐Ÿ’ซ Query should be a direct question, can start by giving a context and then asking the intended segment.88            ๐Ÿ’ซ Use numbers wherever possible, i.e use 2 instead of two89            ๐Ÿ’ซ 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?90            ''',91            visible=True92        )93 94description = '''95<h1 style="text-align: center; font-size: 40px;"><strong>Reasoning with <span style='color: #ff75b3;'> StarCoder ๐Ÿ’ซ</span></strong></h1>96<br />97<span style='font-size: 18px;'>This space is a playground for allowing users to interact with <a href="https://huggingface.co/bigcode/large-model" style="color: #ff75b3;">StarCoder</a> and assessing its reasoning capabilities. 98User can select any of the evaluating strategies from the available ones <u>PAL</u>, <u>TA</u> and <u>MathPrompter</u> following with asking the query in English. The model generated code with respect to the selected strategy will be executed in the server and result is displayed.  99In addition, the space enables users to edit the generated code and re-run it, providing a high degree of flexibility and customization in the solution process.</span>100'''101 102 103with demo:104    gr.Markdown(description, interactive=False)105    with gr.Row():106        methods = gr.Dropdown(choices=['PAL', 'TA', 'MathPrompter'], value="PAL",interactive=True, label="Evaluation Strategies")107        question_input = gr.Textbox(label="Question", lines=1, placeholder="Enter your question here...")108 109    instruction = gr.Textbox(label="Instructions", visible=True, interactive=False, value=render_instruction("PAL")['value'])110    methods.change(fn=render_instruction, inputs=methods, outputs=instruction)111 112    question_output = gr.Textbox(label="Answer", interactive=True)113    code = gr.Code(language="python", interactive=True, label="Generated Code (Editable)")114    submit_btn = gr.Button("Submit")115    edit_btn = gr.Button("Run the edited code", visible=False)116 117    submit_btn.click(run, inputs=[question_input, methods], outputs=[question_output, code, edit_btn])118    edit_btn.click(run_edits, inputs=code, outputs=[question_output, code])119    gr.Examples(120        examples=[121            [122                "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?",123                "PAL"],124            [125                "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?",126                "MathPrompter"],127            ["What is the value of sin(30)?", "PAL"],128            ["How many subarrays can be made from array of length 5?", "TA"],129            ["Write the code to find 7th Fibonacci number.", "TA"],130            ["Write a program to filter all the odd numbers from a python list", "PAL"],131        ],132        inputs=[question_input, methods],133        outputs=[question_output, code, edit_btn],134        fn=run,135        cache_examples=False,136        label="Sample Questions",137    )138 139demo.launch()140