uvpatel7271 commited on
Commit
babfd28
·
verified ·
1 Parent(s): 7605afb

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. server/app.py +70 -10
server/app.py CHANGED
@@ -5,7 +5,7 @@ from __future__ import annotations
5
  import os
6
 
7
  from fastapi import APIRouter, HTTPException
8
- from fastapi.responses import RedirectResponse
9
 
10
  try:
11
  from compat import create_app
@@ -42,20 +42,80 @@ app = create_app(
42
  PythonCodeReviewObservation,
43
  max_concurrent_envs=MAX_CONCURRENT_ENVS,
44
  )
45
- router = APIRouter(tags=["python-code-review"])
46
-
47
-
48
  @router.get("/", include_in_schema=False)
49
- def root() -> RedirectResponse:
50
- """Redirect root to API documentation."""
51
- return RedirectResponse(url="/docs")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
 
54
  @router.get("/web", include_in_schema=False)
55
  @router.get("/web/", include_in_schema=False)
56
- def root_web() -> RedirectResponse:
57
- """Redirect Hugging Face Spaces base-path requests to API documentation."""
58
- return RedirectResponse(url="/docs")
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
 
61
  @router.get("/health", response_model=HealthResponse)
 
5
  import os
6
 
7
  from fastapi import APIRouter, HTTPException
8
+ from fastapi.responses import HTMLResponse, RedirectResponse
9
 
10
  try:
11
  from compat import create_app
 
42
  PythonCodeReviewObservation,
43
  max_concurrent_envs=MAX_CONCURRENT_ENVS,
44
  )
45
+ router = APIRouter(tags=["python-code-review"])
46
+
47
+
48
  @router.get("/", include_in_schema=False)
49
+ def root() -> HTMLResponse:
50
+ """Serve a small homepage with links and a live demo button."""
51
+ return HTMLResponse(
52
+ """
53
+ <!doctype html>
54
+ <html>
55
+ <head>
56
+ <meta charset="utf-8">
57
+ <meta name="viewport" content="width=device-width, initial-scale=1">
58
+ <title>python_code_review_env</title>
59
+ <style>
60
+ body { font-family: ui-sans-serif, system-ui, sans-serif; max-width: 860px; margin: 40px auto; padding: 0 16px; color: #111827; }
61
+ .card { border: 1px solid #e5e7eb; border-radius: 12px; padding: 16px; margin: 16px 0; }
62
+ button { background: #111827; color: white; border: 0; border-radius: 8px; padding: 10px 14px; cursor: pointer; }
63
+ a { color: #2563eb; text-decoration: none; }
64
+ pre { background: #f3f4f6; padding: 16px; border-radius: 10px; overflow: auto; white-space: pre-wrap; }
65
+ .muted { color: #6b7280; }
66
+ </style>
67
+ </head>
68
+ <body>
69
+ <h1>python_code_review_env</h1>
70
+ <p class="muted">This Space is an API environment. Use the live demo below or open the API docs.</p>
71
+ <div class="card">
72
+ <p><a href="/docs">Open API Docs</a></p>
73
+ <p><a href="/health">Health</a></p>
74
+ <p><a href="/tasks">Tasks</a></p>
75
+ <p><a href="/schema">Schema</a></p>
76
+ </div>
77
+ <div class="card">
78
+ <h3>Live Demo</h3>
79
+ <p>Runs one safe reset + analyze step and shows the actual JSON output.</p>
80
+ <button onclick="runDemo()">Run demo</button>
81
+ <pre id="output">Click "Run demo" to see output.</pre>
82
+ </div>
83
+ <script>
84
+ async function runDemo() {
85
+ const output = document.getElementById('output');
86
+ output.textContent = 'Loading...';
87
+ try {
88
+ const response = await fetch('/demo');
89
+ const data = await response.json();
90
+ output.textContent = JSON.stringify(data, null, 2);
91
+ } catch (error) {
92
+ output.textContent = 'Demo failed: ' + String(error);
93
+ }
94
+ }
95
+ </script>
96
+ </body>
97
+ </html>
98
+ """
99
+ )
100
 
101
 
102
  @router.get("/web", include_in_schema=False)
103
  @router.get("/web/", include_in_schema=False)
104
+ def root_web() -> HTMLResponse:
105
+ """Serve the same homepage for Hugging Face Spaces base-path requests."""
106
+ return root()
107
+
108
+
109
+ @router.get("/demo", include_in_schema=False)
110
+ def demo() -> dict:
111
+ """Return a live demo payload so users can see actual environment output."""
112
+ demo_env = PythonCodeReviewEnvironment(verbose=False)
113
+ observation = demo_env.reset(task_id="syntax-fix-easy")
114
+ next_observation = demo_env.step(PythonCodeReviewAction(action_type="analyze_code"))
115
+ return {
116
+ "reset": observation.model_dump(),
117
+ "step": next_observation.model_dump(),
118
+ }
119
 
120
 
121
  @router.get("/health", response_model=HealthResponse)