Adding files
Browse files- Dockerfile +19 -0
- app.py +41 -0
- requirements.txt +9 -0
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
+
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY --chown=user . /app
|
| 16 |
+
# Expose the port the app will run on
|
| 17 |
+
EXPOSE 7860
|
| 18 |
+
|
| 19 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, BackgroundTasks, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Define the request body for the training job
|
| 8 |
+
class TrainingJob(BaseModel):
|
| 9 |
+
code: str
|
| 10 |
+
token: str
|
| 11 |
+
|
| 12 |
+
# This is where you'll put your training logic
|
| 13 |
+
def run_training(user_code: str):
|
| 14 |
+
"""
|
| 15 |
+
This function runs the user's code.
|
| 16 |
+
In a real application, you would add more sophisticated logic
|
| 17 |
+
here,
|
| 18 |
+
such as saving the results to a file or pushing them to a Hub
|
| 19 |
+
model repo.
|
| 20 |
+
"""
|
| 21 |
+
print("--- Starting Training ---")
|
| 22 |
+
try:
|
| 23 |
+
# WARNING: Running exec() on user-provided code is a security risk.
|
| 24 |
+
# Make sure you understand the implications.
|
| 25 |
+
exec(user_code)
|
| 26 |
+
print("--- Training Finished ---")
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"--- Training Failed: {e} ---")
|
| 29 |
+
|
| 30 |
+
@app.post("/train")
|
| 31 |
+
def start_training(job: TrainingJob, background_tasks: BackgroundTasks):
|
| 32 |
+
|
| 33 |
+
# Authenticate the request
|
| 34 |
+
if job.token != os.environ.get("AUTH_TOKEN"):
|
| 35 |
+
raise HTTPException(status_code=401, detail="Unauthorized"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Start the training in the background
|
| 39 |
+
background_tasks.add_task(run_training, job.code)
|
| 40 |
+
|
| 41 |
+
return {"message": "Training job started successfully!"}
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
stable-baselines3[extra]
|
| 4 |
+
gymnasium[all]
|
| 5 |
+
pettingzoo[all]
|
| 6 |
+
torch
|
| 7 |
+
huggingface_hub
|
| 8 |
+
pydantic==2.12.4
|
| 9 |
+
pydantic_core==2.41.5
|