File size: 2,738 Bytes
1d6872e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Restore the pre-retirement full app: App.tsx (frontend) + start.sh (backend).

Upstream smolagents/ml-intern retired the hosted app: frontend App.tsx now
renders RetirementPage ("ML Intern has moved to HuggingChat") and backend/start.sh
now runs retired_main:app. That turns this Space into a static redirect page and
the backend never starts, so our model patches have no visible effect.

This patch overwrites both files with the last pre-retirement versions that this
Space's Dockerfile was built on:
  - frontend/src/App.tsx  -> full AppLayout + useAuth app
  - backend/start.sh      -> uvicorn main:app (the real backend)

Runs from either build stage: it patches whichever target exists and prints
SKIP for the other (idempotent, re-runnable).
"""

import os

FRONTEND_APP = r"""import { Box } from '@mui/material';
import AppLayout from '@/components/Layout/AppLayout';
import { useAuth } from '@/hooks/useAuth';

function App() {
  // Non-blocking auth check -- fires in background, updates store when done.
  // If auth fails later, apiFetch redirects to /auth/login.
  useAuth();

  return (
    <Box sx={{ height: '100vh', display: 'flex' }}>
      <AppLayout />
    </Box>
  );
}

export default App;
"""

BACKEND_START = r"""#!/bin/bash
# Entrypoint for HF Spaces dev mode compatibility.
# Dev mode spawns CMD multiple times simultaneously on restart.
# Only the first instance can bind port 7860 - the rest must exit
# with code 0 so the dev mode daemon doesn't mark the app as crashed.

# Run uvicorn; if it fails due to port conflict, exit cleanly.
uvicorn main:app --host 0.0.0.0 --port 7860
EXIT_CODE=$?

if [ $EXIT_CODE -ne 0 ]; then
    # Check if this was a port-in-use failure (another instance already running)
    echo "uvicorn exited with code $EXIT_CODE, exiting gracefully."
    exit 0
fi
"""


def _write(path: str, content: str, executable: bool = False) -> bool:
    if not os.path.exists(path):
        return False
    with open(path, "w", encoding="utf-8") as f:
        f.write(content)
    if executable:
        os.chmod(path, 0o755)
    return True


changed = False

written = _write("/source/frontend/src/App.tsx", FRONTEND_APP)
if written:
    print("OK: restored /source/frontend/src/App.tsx (full AppLayout app)")
    changed = True
else:
    print("SKIP: /source/frontend/src/App.tsx not present (backend stage?)")

written = _write("/app/backend/start.sh", BACKEND_START, executable=True)
if written:
    print("OK: restored /app/backend/start.sh (uvicorn main:app)")
    changed = True
else:
    print("SKIP: /app/backend/start.sh not present (frontend stage?)")

if not changed:
    raise SystemExit("FAIL: neither target file found - unexpected layout")
print("OK: full-app restore done")