File size: 6,596 Bytes
9b4edaf
 
 
 
 
 
 
201cfef
9b4edaf
201cfef
9b4edaf
 
 
 
 
 
 
 
201cfef
9b4edaf
 
 
 
 
 
 
 
201cfef
7981904
9b4edaf
 
7981904
9b4edaf
 
 
7981904
9b4edaf
 
 
 
 
 
076bdf6
 
9b4edaf
 
 
 
 
 
 
 
731097f
 
 
 
 
 
 
9b4edaf
731097f
 
9b4edaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a292d76
9b4edaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a292d76
731097f
a292d76
731097f
a292d76
 
 
9b4edaf
731097f
9b4edaf
a292d76
9b4edaf
076bdf6
9b4edaf
 
076bdf6
9b4edaf
 
a292d76
1904c80
9b4edaf
 
 
201cfef
9b4edaf
 
 
 
 
 
 
 
 
 
fc6fefb
 
9b4edaf
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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 = '''
<h1 style="text-align: center; font-size: 40px;"><strong>Reasoning with <span style='color: #ff75b3;'> StarCoder πŸ’«</span></strong></h1>
<br />
<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. 
User 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.  
In 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>
'''


with demo:
    gr.Markdown(description, interactive=False)
    with gr.Row():
        methods = gr.Dropdown(choices=['PAL', 'TA', 'MathPrompter'], value="PAL",interactive=True, label="Evaluation Strategies")
        question_input = gr.Textbox(label="Question", lines=1, placeholder="Enter your question here...")

    instruction = gr.Textbox(label="Instructions", visible=True, interactive=False, value=render_instruction("PAL")['value'])
    methods.change(fn=render_instruction, inputs=methods, outputs=instruction)

    question_output = gr.Textbox(label="Answer", interactive=True)
    code = gr.Code(language="python", interactive=True, label="Generated Code (Editable)")
    submit_btn = gr.Button("Submit")
    edit_btn = gr.Button("Run the edited code", visible=False)

    submit_btn.click(run, inputs=[question_input, methods], outputs=[question_output, code, edit_btn])
    edit_btn.click(run_edits, inputs=code, outputs=[question_output, code])
    gr.Examples(
        examples=[
            [
                "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?",
                "PAL"],
            [
                "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?",
                "MathPrompter"],
            ["What is the value of sin(30)?", "PAL"],
            ["How many subarrays can be made from array of length 5?", "TA"],
            ["Write the code to find 7th Fibonacci number.", "TA"],
            ["Write a program to filter all the odd numbers from a python list", "PAL"],
        ],
        inputs=[question_input, methods],
        outputs=[question_output, code, edit_btn],
        fn=run,
        cache_examples=False,
        label="Sample Questions",
    )

demo.launch()