derek-thomas commited on
Commit
b50348a
·
1 Parent(s): ae87f3d

Moving to a docker space

Browse files
Files changed (9) hide show
  1. .dockerignore +13 -0
  2. .gitignore +4 -1
  3. .python-version +1 -0
  4. Dockerfile +21 -0
  5. README.md +9 -4
  6. app.py +38 -22
  7. main.py +6 -0
  8. pyproject.toml +15 -0
  9. requirements.txt +1 -0
.dockerignore ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .git
2
+ .gitignore
3
+ .venv
4
+ __pycache__/
5
+ *.pyc
6
+ *.pyo
7
+ *.pyd
8
+ .pytest_cache/
9
+ .mypy_cache/
10
+ .ruff_cache/
11
+ .env
12
+ .env.*
13
+ client_secret*.json
.gitignore CHANGED
@@ -1,2 +1,5 @@
1
  .env
2
- client_secret_*.json
 
 
 
 
1
  .env
2
+ client_secret_*.json
3
+ .venv/
4
+ uv.lock
5
+ .python-version
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.13
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.13-slim
2
+
3
+ RUN useradd -m -u 1000 user
4
+
5
+ USER user
6
+ ENV HOME=/home/user \
7
+ PATH=/home/user/.local/bin:$PATH \
8
+ PYTHONDONTWRITEBYTECODE=1 \
9
+ PYTHONUNBUFFERED=1
10
+
11
+ WORKDIR $HOME/app
12
+
13
+ COPY --chown=user requirements.txt ./requirements.txt
14
+ RUN pip install --no-cache-dir --upgrade pip && \
15
+ pip install --no-cache-dir -r requirements.txt
16
+
17
+ COPY --chown=user . .
18
+
19
+ EXPOSE 7860
20
+
21
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -3,9 +3,8 @@ title: Orchestrator Evals
3
  emoji: 📊
4
  colorFrom: purple
5
  colorTo: green
6
- sdk: gradio
7
- sdk_version: 6.5.1
8
- app_file: app.py
9
  pinned: false
10
  ---
11
 
@@ -60,7 +59,13 @@ Then open:
60
 
61
  - `https://localhost:7860`
62
 
63
- ## 4) App routes
 
 
 
 
 
 
64
 
65
  - `/` - home/login status
66
  - `/login` - starts Google OAuth
 
3
  emoji: 📊
4
  colorFrom: purple
5
  colorTo: green
6
+ sdk: docker
7
+ app_port: 7860
 
8
  pinned: false
9
  ---
10
 
 
59
 
60
  - `https://localhost:7860`
61
 
62
+ ## 4) Docker Space notes
63
+
64
+ - The Space is built from `Dockerfile` and served on port `7860`.
65
+ - Runtime secrets are injected from Space Settings as environment variables.
66
+ - Keep OAuth secrets in Space Settings only (not in committed files).
67
+
68
+ ## 5) App routes
69
 
70
  - `/` - home/login status
71
  - `/login` - starts Google OAuth
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  from typing import Optional
3
 
4
  import gradio as gr
 
5
  from authlib.integrations.base_client import OAuthError
6
  from authlib.integrations.starlette_client import OAuth
7
  from dotenv import load_dotenv
@@ -19,14 +20,7 @@ app = FastAPI(title="Orchestrator Evals OAuth", version="1.0.0")
19
 
20
  SECRET_KEY = os.getenv("SECRET_KEY", "change-me")
21
  SPACE_HOST = os.getenv("SPACE_HOST", "").strip()
22
-
23
- app.add_middleware(
24
- SessionMiddleware,
25
- secret_key=SECRET_KEY,
26
- max_age=3600,
27
- same_site="lax",
28
- https_only=True,
29
- )
30
 
31
  config = Config(".env")
32
  oauth = OAuth(config)
@@ -46,14 +40,17 @@ def _external_base_url(request: Request) -> str:
46
 
47
 
48
  def _build_redirect_uri(request: Request) -> str:
49
- redirect_uri = f"{_external_base_url(request)}/auth"
50
- # Local Google OAuth setup usually expects https localhost callback.
51
- if "://localhost" in redirect_uri or "://127.0.0.1" in redirect_uri:
 
52
  redirect_uri = redirect_uri.replace("http://", "https://", 1)
53
  return redirect_uri
54
 
55
 
56
  def _user_from_session(request: Request) -> Optional[dict]:
 
 
57
  user = request.session.get("user")
58
  if isinstance(user, dict):
59
  return user
@@ -69,6 +66,13 @@ class GradioAuthMiddleware(BaseHTTPMiddleware):
69
 
70
 
71
  app.add_middleware(GradioAuthMiddleware)
 
 
 
 
 
 
 
72
 
73
 
74
  @app.get("/")
@@ -134,18 +138,30 @@ async def health():
134
  return {"status": "ok"}
135
 
136
 
137
- def _echo(text: str) -> str:
138
- if not text.strip():
139
- return "Enter text to run the demo."
140
- return f"You entered: {text}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
 
142
 
143
- gradio_ui = gr.Interface(
144
- fn=_echo,
145
- inputs=gr.Textbox(label="Input"),
146
- outputs=gr.Textbox(label="Output"),
147
- title="Orchestrator Evals",
148
- description="Protected Gradio UI behind Google OAuth.",
149
- )
150
 
151
  app = gr.mount_gradio_app(app, gradio_ui, path="/gradio")
 
2
  from typing import Optional
3
 
4
  import gradio as gr
5
+ import plotly.graph_objects as go
6
  from authlib.integrations.base_client import OAuthError
7
  from authlib.integrations.starlette_client import OAuth
8
  from dotenv import load_dotenv
 
20
 
21
  SECRET_KEY = os.getenv("SECRET_KEY", "change-me")
22
  SPACE_HOST = os.getenv("SPACE_HOST", "").strip()
23
+ LOCAL_HTTPS_REDIRECT = os.getenv("LOCAL_HTTPS_REDIRECT", "").strip().lower() in {"1", "true", "yes"}
 
 
 
 
 
 
 
24
 
25
  config = Config(".env")
26
  oauth = OAuth(config)
 
40
 
41
 
42
  def _build_redirect_uri(request: Request) -> str:
43
+ external_base = _external_base_url(request)
44
+ redirect_uri = f"{external_base}/auth"
45
+ # For local dev with plain uvicorn, keep http unless explicitly opted-in.
46
+ if LOCAL_HTTPS_REDIRECT and ("://localhost" in redirect_uri or "://127.0.0.1" in redirect_uri):
47
  redirect_uri = redirect_uri.replace("http://", "https://", 1)
48
  return redirect_uri
49
 
50
 
51
  def _user_from_session(request: Request) -> Optional[dict]:
52
+ if "session" not in request.scope:
53
+ return None
54
  user = request.session.get("user")
55
  if isinstance(user, dict):
56
  return user
 
66
 
67
 
68
  app.add_middleware(GradioAuthMiddleware)
69
+ app.add_middleware(
70
+ SessionMiddleware,
71
+ secret_key=SECRET_KEY,
72
+ max_age=3600,
73
+ same_site="lax",
74
+ https_only=True,
75
+ )
76
 
77
 
78
  @app.get("/")
 
138
  return {"status": "ok"}
139
 
140
 
141
+ def _build_dummy_plot() -> go.Figure:
142
+ fig = go.Figure(
143
+ data=[
144
+ go.Scatter(
145
+ x=["Mon", "Tue", "Wed", "Thu", "Fri"],
146
+ y=[2, 4, 3, 5, 6],
147
+ mode="lines+markers",
148
+ name="Demo Series",
149
+ )
150
+ ]
151
+ )
152
+ fig.update_layout(
153
+ title="Dummy Plotly Graph",
154
+ xaxis_title="Day",
155
+ yaxis_title="Value",
156
+ template="plotly_white",
157
+ )
158
+ return fig
159
 
160
 
161
+ with gr.Blocks(title="Orchestrator Evals") as gradio_ui:
162
+ gr.Markdown("## Protected Gradio UI")
163
+ gr.Markdown("This is a dummy Plotly graph behind Google OAuth.")
164
+ plot = gr.Plot(label="Plotly Demo")
165
+ gradio_ui.load(fn=_build_dummy_plot, outputs=plot)
 
 
166
 
167
  app = gr.mount_gradio_app(app, gradio_ui, path="/gradio")
main.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ def main():
2
+ print("Hello from orchestrator-evals!")
3
+
4
+
5
+ if __name__ == "__main__":
6
+ main()
pyproject.toml ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "orchestrator-evals"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.13"
7
+ dependencies = [
8
+ "authlib>=1.6.8",
9
+ "dotenv>=0.9.9",
10
+ "fastapi>=0.129.0",
11
+ "gradio>=6.5.1",
12
+ "itsdangerous>=2.2.0",
13
+ "plotly>=6.0.0",
14
+ "uvicorn>=0.41.0",
15
+ ]
requirements.txt CHANGED
@@ -4,3 +4,4 @@ authlib
4
  python-dotenv
5
  uvicorn
6
  itsdangerous
 
 
4
  python-dotenv
5
  uvicorn
6
  itsdangerous
7
+ plotly