Spaces:
Sleeping
Sleeping
File size: 5,413 Bytes
532e19f | 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 | """
challenge.py β Unified Challenge entry point.
Usage (GUI setup page):
python challenge.py [--port 7860] [--share]
Usage (skip setup, run directly):
python challenge.py <github_url> --name "Alice" [--nesting-level 3] [--num-bugs N]
[--refactoring] [--debug] [--timer N] [--port 7860] [--share]
If github_url is provided the setup page is skipped: the pipeline runs
immediately and the challenge interface opens directly.
"""
import argparse
import os
import sys
from dotenv import load_dotenv
# ββ Resolve .env (LegacyInterview2/.env first, then parent AiAgent/.env) ββ
_base = os.path.dirname(os.path.abspath(__file__))
_env_local = os.path.join(_base, ".env")
_env_fallback = os.path.join(_base, "..", "First Year", "Advanced Deep Neural Neworks",
"Home Exercises", "AiAgent", ".env")
if os.path.exists(_env_local):
load_dotenv(dotenv_path=_env_local)
elif os.path.exists(_env_fallback):
load_dotenv(dotenv_path=_env_fallback)
else:
load_dotenv()
# ββ Apply proxy settings from .env if defined ββ
if os.getenv("http_proxy"):
os.environ["http_proxy"] = os.getenv("http_proxy")
if os.getenv("https_proxy"):
os.environ["https_proxy"] = os.getenv("https_proxy")
if os.getenv("no_proxy"):
os.environ["no_proxy"] = os.getenv("no_proxy")
# Note: API key is optional in hosted mode (users provide their own via login page)
# Only enforce it when github_url is provided (CLI mode requires API key to run pipeline)
import gradio as gr # noqa: E402
import student_interface # noqa: E402
def main() -> None:
parser = argparse.ArgumentParser(
description="Legacy Challenge β launch the student GUI (with optional CLI shortcuts)."
)
# Optional positional β if omitted, the GUI setup page is shown instead
parser.add_argument(
"github_url", nargs="?", default=None,
help="GitHub repo URL. If given, skip the setup page and run immediately.",
)
parser.add_argument(
"--name", type=str, default="",
help="Student name shown in the header. Required when github_url is provided.",
)
parser.add_argument(
"--nesting-level", type=int, choices=range(1, 7), default=3, metavar="1-6",
help="Call-chain depth for bug hiding (default: 3). Only used when github_url is provided.",
)
parser.add_argument(
"--num-bugs", type=int, default=1,
help="Number of bugs to inject (default: 1). Only used when github_url is provided.",
)
parser.add_argument(
"--refactoring", action="store_true",
help="Enable refactoring/obfuscation transforms. Only used when github_url is provided.",
)
parser.add_argument(
"--debug", action="store_true",
help="Debug mode β verbose output, show bug locations. Only used when github_url is provided.",
)
parser.add_argument(
"--timer", type=int, default=0,
help="Countdown timer in minutes (0 = no timer). Only used when github_url is provided.",
)
parser.add_argument(
"--port", type=int, default=7860,
help="Port for the Gradio GUI (default: 7860).",
)
parser.add_argument(
"--share", action="store_true",
help="Create a public Gradio share link.",
)
args = parser.parse_args()
base_kwargs = dict(
server_name="127.0.0.1",
server_port=args.port,
share=args.share,
inbrowser=True,
theme=gr.themes.Base(),
css=student_interface._CSS,
)
if args.github_url:
# ββ Fast path: CLI args supplied β skip setup page ββββββββββββββ
# CLI mode requires API key to run the pipeline
if not os.getenv("OPENAI_API_KEY"):
print("ERROR: OPENAI_API_KEY not set. Add it to .env or export it.")
print("(CLI mode requires API key to generate challenges)")
sys.exit(1)
if not args.name:
parser.error("--name is required when providing a GitHub URL (e.g. --name 'Alice Smith')")
print(
f"\n[challenge] CLI mode β URL: {args.github_url} "
f"Nesting: {args.nesting_level} Bugs: {args.num_bugs} "
f"Refactoring: {args.refactoring} Debug: {args.debug} Name: {args.name}\n"
)
print("[challenge] Running pipelineβ¦")
workspace_path = student_interface._run_pipeline(
args.github_url.strip(), args.nesting_level, args.num_bugs,
refactoring_enabled=args.refactoring, debug_mode=args.debug,
)
print(f"[challenge] Workspace ready: {workspace_path}")
print(f"[challenge] Opening http://localhost:{args.port}\n")
demo = student_interface.create_interface(
workspace_path, student_name=args.name, timer_minutes=args.timer
)
# JS is embedded in gr.Blocks(js=_make_js(timer)) inside create_interface
demo.launch(**base_kwargs)
else:
# ββ Normal path: open setup page in the browser ββββββββββββββββββ
print(f"\n[challenge] Opening setup page at http://localhost:{args.port}\n")
demo = student_interface.create_full_interface()
demo.launch(**base_kwargs, js=student_interface._JS)
if __name__ == "__main__":
main()
|