Spaces:
Sleeping
Sleeping
File size: 5,792 Bytes
2680a94 |
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
import re
import pandas as pd
import gradio as gr
import ast
import random
from main import mg_debug_demo
from groq import Groq
from openai import OpenAI
def debug_code(key, model, code, fixed_code, test_cases):
if model == 'deepseek-coder':
client = OpenAI(
api_key=key,
base_url="https://api.deepseek.com"
)
elif model in ['gemma2-9b-it']:
client = Groq(
api_key=key,
)
else:
client = OpenAI(
api_key=key
)
fixed_code = mg_debug_demo(code, test_cases, given_model=model, given_client=client)
return code, fixed_code, test_cases
app = gr.Blocks(
theme=gr.themes.Default(primary_hue="red", secondary_hue="pink", neutral_hue="gray")
)
with app:
with gr.Row():
gr.Markdown("# MGDebugger Demo")
with gr.Row():
with gr.Column():
with gr.Row():
openai_key_input = gr.Textbox(
label="API Key",
placeholder="Enter your API key here",
type="password",
)
model_selector = gr.Dropdown(
label="Choose Model",
choices=[("gemma2. Please get your api key from https://console.groq.com/keys", "gemma2-9b-it"),
("deepseek-coder. Please get your api key from https://platform.deepseek.com/api-docs/", "deepseek-coder"),
("gpt-4o-mini. Please get your api key from https://platform.openai.com/settings/profile?tab=api-keys", "gpt-4o-mini"),
("gpt-4-1106-preview. Please get your api key from https://platform.openai.com/settings/profile?tab=api-keys", "gpt-4-1106-preview")],
value="deepseek-coder",
)
code_input = gr.TextArea(
label="Code Input",
placeholder="Enter your code here",
lines=10,
)
test_cases = gr.TextArea(
label="Public Test Cases",
placeholder="Enter your public test cases",
lines=10,
)
with gr.Row(): # This Row will contain the buttons
debug_button = gr.Button("Debug", variant="primary")
clear_button = gr.Button("Clear", variant="neutral")
with gr.Column():
fixed_code_output = gr.TextArea(
label="Fixed Code",
placeholder="Fixed code will be shown here",
lines=10,
interactive=False,
visible=True,
)
debug_button.click(
debug_code,
inputs=[
openai_key_input,
model_selector,
code_input,
fixed_code_output,
test_cases,
],
outputs=[ code_input, fixed_code_output, test_cases],
)
def clear_inputs():
return (
"",
"",
pd.DataFrame(
{
"Pass?": [],
"Expression": [],
"Expected Value": [],
"Actual Value": [],
}
),
"",
"",
)
clear_button.click(
clear_inputs,
inputs=[],
outputs=[code_input, test_cases, fixed_code_output],
)
gr.Markdown("## Text Examples")
gr.Examples(
[
[
{'input': {'music_string': 'o o| .| o| o| .| .| .| .| o o'}, 'expected_output': [4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4]}
,
'''
def parse_music(music_string: str) -> List[int]:
""" Input to this function is a string representing musical notes in a special ASCII format.
Your task is to parse this string and return list of integers corresponding to how many beats does each
not last.
Here is a legend:
'o' - whole note, lasts four beats
'o|' - half note, lasts two beats
'.|' - quater note, lasts one beat
>>> parse_music('o o| .| o| o| .| .| .| .| o o')
[4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4]
"""
note_map = {'o': 3, 'o|': 2, '.|': 1}
return [note_map[x] for x in music_string.split(' ') if x]
''',
],
[
{'input': {'numbers': [1.0, 2.0, 3.0], 'threshold': 0.5}, 'expected_output': False},
'''
def has_close_elements(numbers: List[float], threshold: float) -> bool:
""" Check if in given list of numbers, are any two numbers closer to each other than
given threshold.
>>> has_close_elements([1.0, 2.0, 3.0], 0.5)
False
>>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)
True
"""
for idx, elem in enumerate(numbers):
for idx2, elem2 in enumerate(numbers):
if idx != idx2:
distance = elem - elem2
if distance < threshold:
return True
return False
'''
],
[
{'input': {'operations': [1, 2, -4, 5]}, 'expected_output': False},
'''
def below_zero(operations: List[int]) -> bool:
""" You're given a list of deposit and withdrawal operations on a bank account that starts with
zero balance. Your task is to detect if at any point the balance of account fallls below zero, and
at that point function should return True. Otherwise it should return False.
>>> below_zero([1, 2, 3])
False
>>> below_zero([1, 2, -4, 5])
True
"""
balance = 0
for op in operations:
balance += op
if balance == 0:
return True
return False
'''
]
],
inputs=[test_cases, code_input],
)
app.launch() |