Spaces:
Sleeping
Sleeping
Commit ·
ddb046e
1
Parent(s): 6c348e7
Show cleanup pass progress
Browse filesFiles changed (2)
- app.py+3-2
- backend.py+8-4
54 unmodified lines555657585960616263646554 unmodified lines"""def run(code, mode, key, model, base_url):if not (key or "").strip():return gr.skip(), ""try:return optimize(code or "", mode, key, model or "", base_url or "")except (InputError, ModelReplyError, ProviderError) as error:return "", str(error)except AuthenticationError:54 unmodified lines55565758596061626364656654 unmodified lines"""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:
173 unmodified lines17417517617717817918010 unmodified lines1911921931941951965 unmodified lines2022032042052062072081 unmodified line2102112122132142152162172 unmodified lines220221222223224225226173 unmodified linesreturn not root.has_error and sum(node.type == "function_definition" for node in root.children) == 1def optimize(code, mode, key, model=DEFAULT_MODEL, base_url=DEFAULT_BASE_URL):if not code.strip():raise InputError("Paste a decompiled C function.")if len(code) > MAX_CODE_LENGTH:10 unmodified linesraise InputError("Select a model or enter a valid model ID.")url = parse_base_url(base_url)roles, _ = load_roles()if not _is_single_function(code, roles):raise InputError("Paste one complete C function with valid syntax.")5 unmodified linestimeout=25, max_retries=0, http_client=http_client) as client:token = _request.set((client, model, compatible))try:revision = _apply_passes(code, mode, roles)finally:_request.reset(token)1 unmodified linereturn revision, statusdef _apply_passes(code, mode, roles):if mode == "Automatic":try:_, choices = roles.Referee().get_direction(code)except AssertionError:2 unmodified lineselse:passes = [roles.DType[MODES[mode]]]for cleanup in passes:candidate = roles.single_opt(code, cleanup)["output"]if (candidate.strip() != code.strip() and len(candidate) <= MAX_CODE_LENGTHand _is_single_function(candidate, roles)):173 unmodified lines17417517617717817918010 unmodified lines1911921931941951961975 unmodified lines2032042052062072082091 unmodified line2112122132142152162172182192 unmodified lines222223224225226227228229230173 unmodified linesreturn not root.has_error and sum(node.type == "function_definition" for node in root.children) == 1def optimize(code, mode, key, model=DEFAULT_MODEL, base_url=DEFAULT_BASE_URL, progress=lambda *args: None):if not code.strip():raise InputError("Paste a decompiled C function.")if len(code) > MAX_CODE_LENGTH:10 unmodified linesraise InputError("Select a model or enter a valid model ID.")url = parse_base_url(base_url)progress(0, "Validating function")roles, _ = load_roles()if not _is_single_function(code, roles):raise InputError("Paste one complete C function with valid syntax.")5 unmodified linestimeout=25, max_retries=0, http_client=http_client) as client:token = _request.set((client, model, compatible))try:revision = _apply_passes(code, mode, roles, progress)finally:_request.reset(token)1 unmodified linereturn revision, statusdef _apply_passes(code, mode, roles, progress):if mode == "Automatic":progress(None, "Choosing cleanup passes")try:_, choices = roles.Referee().get_direction(code)except AssertionError:2 unmodified lineselse:passes = [roles.DType[MODES[mode]]]names = {roles.DType[value]: name for name, value in MODES.items() if value}for index, cleanup in enumerate(passes):progress(index / len(passes), f"Pass {index + 1} of {len(passes)}: {names.get(cleanup, str(cleanup))}")candidate = roles.single_opt(code, cleanup)["output"]if (candidate.strip() != code.strip() and len(candidate) <= MAX_CODE_LENGTHand _is_single_function(candidate, roles)):
app.py+3-2
backend.py+8-4