Spaces:
Paused
Paused
Upload 3 files
Browse filesFiles changed (3)
- DockerfileAdded+16
- app.pyAdded+48
- requirements.txtAdded+2
12345678910111213141516# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker# you will also find guides on how best to write your Dockerfile
FROM python:3.9
RUN useradd -m -u 1000 userUSER userENV PATH="/home/user/.local/bin:$PATH"
WORKDIR /app
COPY --chown=user ./requirements.txt requirements.txtRUN pip install --no-cache-dir --upgrade -r requirements.txt
COPY --chown=user . /appCMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748from fastapi import FastAPI, HTTPExceptionfrom fastapi.responses import FileResponsefrom pathlib import Path
app = FastAPI()
# Base directory where notes are storedNOTES_DIR = Path("notes")
@app.get("/")def read_root(): return {"message": "Welcome to the Notes API!"}
@app.get("/subjects")def get_subjects(): """Get all subjects.""" if not NOTES_DIR.exists(): raise HTTPException(status_code=404, detail="Notes directory not found") subjects = [subject.name for subject in NOTES_DIR.iterdir() if subject.is_dir()] return {"subjects": subjects}
@app.get("/subjects/{subject}/units")def get_units(subject: str): """Get all units for a specific subject.""" subject_path = NOTES_DIR / subject if not subject_path.exists() or not subject_path.is_dir(): raise HTTPException(status_code=404, detail="Subject not found") units = [unit.stem for unit in subject_path.glob("*.md")] return {"units": units}
@app.get("/subjects/{subject}/units/{unit}")def get_unit_notes(subject: str, unit: str): """Get the content of a specific unit.""" unit_path = NOTES_DIR / subject / f"{unit}.md" if not unit_path.exists(): raise HTTPException(status_code=404, detail="Unit notes not found") return FileResponse(unit_path)
# Optional: Serve notes as plain text or rendered HTML@app.get("/subjects/{subject}/units/{unit}/content")def get_unit_notes_content(subject: str, unit: str): """Get the content of a specific unit as plain text.""" unit_path = NOTES_DIR / subject / f"{unit}.md" if not unit_path.exists(): raise HTTPException(status_code=404, detail="Unit notes not found") with open(unit_path, "r", encoding="utf-8") as file: content = file.read() return {"content": content}12fastapiuvicorn[standard]DockerfileAdded+16
app.pyAdded+48
requirements.txtAdded+2