Spaces:
Paused
Paused
Upload 3 files
Browse files- Dockerfile +16 -0
- app.py +48 -0
- requirements.txt +2 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Base directory where notes are stored
|
| 8 |
+
NOTES_DIR = Path("notes")
|
| 9 |
+
|
| 10 |
+
@app.get("/")
|
| 11 |
+
def read_root():
|
| 12 |
+
return {"message": "Welcome to the Notes API!"}
|
| 13 |
+
|
| 14 |
+
@app.get("/subjects")
|
| 15 |
+
def get_subjects():
|
| 16 |
+
"""Get all subjects."""
|
| 17 |
+
if not NOTES_DIR.exists():
|
| 18 |
+
raise HTTPException(status_code=404, detail="Notes directory not found")
|
| 19 |
+
subjects = [subject.name for subject in NOTES_DIR.iterdir() if subject.is_dir()]
|
| 20 |
+
return {"subjects": subjects}
|
| 21 |
+
|
| 22 |
+
@app.get("/subjects/{subject}/units")
|
| 23 |
+
def get_units(subject: str):
|
| 24 |
+
"""Get all units for a specific subject."""
|
| 25 |
+
subject_path = NOTES_DIR / subject
|
| 26 |
+
if not subject_path.exists() or not subject_path.is_dir():
|
| 27 |
+
raise HTTPException(status_code=404, detail="Subject not found")
|
| 28 |
+
units = [unit.stem for unit in subject_path.glob("*.md")]
|
| 29 |
+
return {"units": units}
|
| 30 |
+
|
| 31 |
+
@app.get("/subjects/{subject}/units/{unit}")
|
| 32 |
+
def get_unit_notes(subject: str, unit: str):
|
| 33 |
+
"""Get the content of a specific unit."""
|
| 34 |
+
unit_path = NOTES_DIR / subject / f"{unit}.md"
|
| 35 |
+
if not unit_path.exists():
|
| 36 |
+
raise HTTPException(status_code=404, detail="Unit notes not found")
|
| 37 |
+
return FileResponse(unit_path)
|
| 38 |
+
|
| 39 |
+
# Optional: Serve notes as plain text or rendered HTML
|
| 40 |
+
@app.get("/subjects/{subject}/units/{unit}/content")
|
| 41 |
+
def get_unit_notes_content(subject: str, unit: str):
|
| 42 |
+
"""Get the content of a specific unit as plain text."""
|
| 43 |
+
unit_path = NOTES_DIR / subject / f"{unit}.md"
|
| 44 |
+
if not unit_path.exists():
|
| 45 |
+
raise HTTPException(status_code=404, detail="Unit notes not found")
|
| 46 |
+
with open(unit_path, "r", encoding="utf-8") as file:
|
| 47 |
+
content = file.read()
|
| 48 |
+
return {"content": content}
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|