Spaces:
Runtime error
Runtime error
Download app/app.py from Jordan001237/TP-INF-232: direct link, hf CLI and curl.
- Browser
- Download file 4 kB
-
https://huggingface.co/spaces/Jordan001237/TP-INF-232/resolve/main/app/app.py
- Command line
-
hf download hf://spaces/Jordan001237/TP-INF-232/app/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Jordan001237/TP-INF-232/resolve/main/app/app.py
4 kB
| import os | |
| import json | |
| import logging | |
| from pathlib import Path | |
| from dotenv import load_dotenv | |
| from fastapi import FastAPI, Depends, HTTPException, Header | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from sqlalchemy.orm import Session | |
| from app.database import engine, get_db, Base | |
| from app.models import Response | |
| from app.schemas import SurveySubmission | |
| from app.analysis import compute_stats | |
| logger = logging.getLogger(__name__) | |
| load_dotenv() | |
| app = FastAPI(title="Survey API β IA & PassExamAI", version="1.0.0") | |
| def startup_event(): | |
| logger.info("Ensuring DB schema exists...") | |
| Base.metadata.create_all(bind=engine) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # ββ Frontend statique β montΓ© seulement si le dossier existe ββββββ | |
| FRONTEND = Path(__file__).parent.parent.parent / "frontend" | |
| if FRONTEND.exists(): | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import FileResponse | |
| app.mount("/static", StaticFiles(directory=str(FRONTEND / "static")), name="static") | |
| def serve_form(): | |
| return FileResponse(str(FRONTEND / "index.html")) | |
| def serve_dashboard(): | |
| return FileResponse(str(FRONTEND / "dashboard.html")) | |
| # ββ Admin token β optionnel si non dΓ©fini ββββββββββββββββββββββββ | |
| ADMIN_TOKEN = os.getenv("ADMIN_TOKEN") | |
| def verify_admin(x_admin_token: str = Header(...)): | |
| if not ADMIN_TOKEN or x_admin_token != ADMIN_TOKEN: | |
| raise HTTPException(status_code=403, detail="Token invalide") | |
| # ββ Routes ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def health(): | |
| return {"status": "ok"} | |
| def submit_response(data: SurveySubmission, db: Session = Depends(get_db)): | |
| row = Response( | |
| niveau_etudes=data.niveau_etudes, | |
| faculte=data.faculte, | |
| filiere=data.filiere, | |
| genre=data.genre, | |
| utilise_ia=data.utilise_ia, | |
| outils_ia=json.dumps(data.outils_ia, ensure_ascii=False), | |
| contextes_ia=json.dumps(data.contextes_ia, ensure_ascii=False), | |
| niveau_connaissance_ia=data.niveau_connaissance_ia, | |
| ia_opportunite=data.ia_opportunite, | |
| ia_menace_emploi=data.ia_menace_emploi, | |
| confiance_ia=data.confiance_ia, | |
| dangers_ia=json.dumps(data.dangers_ia, ensure_ascii=False), | |
| frequence_ia_etudes=data.frequence_ia_etudes, | |
| usage_ia_etudes=json.dumps(data.usage_ia_etudes, ensure_ascii=False), | |
| methode_preparation_exam=json.dumps(data.methode_preparation_exam, ensure_ascii=False), | |
| difficulte_preparation=data.difficulte_preparation, | |
| ia_ameliore_reussite=data.ia_ameliore_reussite, | |
| freins_ia_etudes=json.dumps(data.freins_ia_etudes, ensure_ascii=False), | |
| solution_repond_probleme=data.solution_repond_probleme, | |
| fonctionnalites_utiles=json.dumps(data.fonctionnalites_utiles, ensure_ascii=False), | |
| utiliserait_app=data.utiliserait_app, | |
| format_prefere=data.format_prefere, | |
| budget_mensuel=data.budget_mensuel, | |
| suggestions=data.suggestions or "", | |
| ) | |
| db.add(row) | |
| db.commit() | |
| db.refresh(row) | |
| return {"success": True, "id": row.id} | |
| def get_public_stats(db: Session = Depends(get_db)): | |
| return compute_stats(db) | |
| def get_analysis(db: Session = Depends(get_db), _=Depends(verify_admin)): | |
| return compute_stats(db) | |
| def get_responses(db: Session = Depends(get_db), _=Depends(verify_admin)): | |
| rows = db.query(Response).order_by(Response.submitted_at.desc()).all() | |
| return [r.to_dict() for r in rows] |