import gradio as gr from gradio.components.textbox import InputHTMLAttributes from openai import APIConnectionError, APIResponseValidationError, APIStatusError, APITimeoutError, AuthenticationError, RateLimitError from backend import DEFAULT_MODEL, MODEL_OPTIONS, InputError, ModelReplyError, MODES, load_roles, optimize from provider import DEFAULT_BASE_URL, ProviderError EXAMPLE = """int fib(int param_1) { int iVar1; int iVar2; if (param_1 < 2) { iVar2 = param_1; } else { iVar1 = fib(param_1 + -1); iVar2 = fib(param_1 + -2) + iVar1; } return iVar2; }""" CHECK_KEY = """(code, mode, key, model, baseUrl) => { const field = document.querySelector('#openai-api-key input'); if (field && !key?.trim()) { if (!field.hasAttribute('aria-invalid')) { field.setAttribute('aria-invalid', 'true'); field.setAttribute('aria-errormessage', 'api-key-required'); field.addEventListener('input', function clearError() { if (field.value.trim()) { field.removeAttribute('aria-invalid'); field.removeAttribute('aria-errormessage'); field.removeEventListener('input', clearError); } }); } field.focus(); } return [code, mode, key, model, baseUrl]; }""" FORM_CSS = """ :is(#original-code, #revised-code) :is(.cm-editor, .empty) { block-size: 16rem; } #openai-api-key input[aria-invalid="true"] { outline: 2px solid #dc2626 !important; outline-offset: 2px; } #api-key-required { display: none; color: var(--error-text-color, #dc2626); } #api-key-control:has(input[aria-invalid="true"]) #api-key-required { display: block; } """ def run(code, mode, key, model, base_url, progress=gr.Progress()): if not (key or "").strip(): return gr.skip(), "" try: return optimize(code or "", mode, key, model or "", base_url or "", lambda value, description: progress(value, desc=description)) except (InputError, ModelReplyError, ProviderError) as error: return "", str(error) except AuthenticationError: return "", "The provider rejected the API key. Check that it belongs to the selected provider." except RateLimitError: return "", "The provider reported a rate or quota limit. Check your API usage or try again later." except APITimeoutError: return "", "The provider timed out. Try a shorter function or a single cleanup pass." except APIConnectionError: return "", "Could not reach the provider. Check the API base URL or try again shortly." except APIResponseValidationError: return "", "The provider returned an invalid chat completion. Check the API base URL." except APIStatusError: return "", "The provider could not complete the request. Check the model ID and your access to it." with gr.Blocks(title="DeGPT", analytics_enabled=False) as app: gr.Markdown("# DeGPT\nImprove the readability of a decompiled C function.") with gr.Row(): original = gr.Code(value=EXAMPLE, label="Original C", language="c", lines=16, max_lines=16, interactive=True, elem_id="original-code") revision = gr.Code(value="", label="Revised C", language="c", lines=16, max_lines=16, interactive=False, elem_id="revised-code") with gr.Row(): mode = gr.Dropdown(list(MODES), value="Automatic", label="Cleanup mode") model = gr.Dropdown(list(MODEL_OPTIONS), value=DEFAULT_MODEL, allow_custom_value=True, label="Model") with gr.Row(): base_url = gr.Textbox(value=DEFAULT_BASE_URL, label="API base URL") with gr.Column(elem_id="api-key-control"): key = gr.Textbox(label="API key", type="password", elem_id="openai-api-key", html_attributes=InputHTMLAttributes(autocomplete="off", spellcheck=False)) gr.HTML('
Enter your API key to run DeGPT.
', elem_id="api-key-required") gr.Markdown("Your code and API key are sent to the provider at the selected base URL.") submit = gr.Button("Run DeGPT", variant="primary") status = gr.Markdown() submit.click(run, inputs=[original, mode, key, model, base_url], outputs=[revision, status], js=CHECK_KEY, concurrency_limit=2, api_visibility="private") app.queue(max_size=16) if __name__ == "__main__": load_roles() app.launch(css=FORM_CSS, run_history=False, enable_monitoring=False)