rairo commited on
Commit
4d37637
Β·
verified Β·
1 Parent(s): 48138d9

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +67 -0
main.py CHANGED
@@ -778,6 +778,73 @@ def get_exam_questions(paper_id):
778
  return jsonify({"paperId": paper_id, "meta": paper.get("meta"), "questions": paper.get("questions", [])})
779
  return jsonify({"error": "Not found"}), 404
780
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
781
  @app.route('/v1/rebuild', methods=['POST'])
782
  def trigger_rebuild():
783
  secret = os.environ.get("REBUILD_SECRET", "")
 
778
  return jsonify({"paperId": paper_id, "meta": paper.get("meta"), "questions": paper.get("questions", [])})
779
  return jsonify({"error": "Not found"}), 404
780
 
781
+
782
+ @app.route('/v1/reset', methods=['GET', 'POST'])
783
+ def reset_and_rebuild():
784
+ """
785
+ One-time cache reset endpoint.
786
+ Wipes data_api/syllabi and data_api/vectors from Firebase,
787
+ then triggers a full fresh rebuild from the PDF files.
788
+
789
+ Protected by REBUILD_SECRET env var. If that var is not set,
790
+ protected by a hardcoded one-time token so it cannot be called accidentally.
791
+
792
+ Call from browser: GET /v1/reset?token=marka-reset-2026
793
+ Or as POST with Authorization: Bearer marka-reset-2026
794
+ """
795
+ # Token check β€” use REBUILD_SECRET if set, otherwise the hardcoded token
796
+ expected = os.environ.get("REBUILD_SECRET", "marka-reset-2026")
797
+ token = (
798
+ request.args.get("token")
799
+ or request.headers.get("Authorization", "").replace("Bearer ", "").strip()
800
+ )
801
+ if token != expected:
802
+ return jsonify({
803
+ "error": "Unauthorized",
804
+ "hint": "Pass ?token=<REBUILD_SECRET> in the URL"
805
+ }), 401
806
+
807
+ def _reset_bg():
808
+ global SYLLABUS_MAP, VECTOR_DB, VECTOR_MATRIX, EXAM_MAP
809
+ logger.info("=== CACHE RESET STARTED ===")
810
+
811
+ # 1. Wipe Firebase cache nodes
812
+ if FIREBASE_AVAILABLE:
813
+ try:
814
+ firebase_db_ref.child("data_api/syllabi").delete()
815
+ logger.info(" βœ“ Deleted data_api/syllabi from Firebase")
816
+ except Exception as e:
817
+ logger.error(f" βœ— Failed to delete syllabi: {e}")
818
+ try:
819
+ firebase_db_ref.child("data_api/vectors").delete()
820
+ logger.info(" βœ“ Deleted data_api/vectors from Firebase")
821
+ except Exception as e:
822
+ logger.error(f" βœ— Failed to delete vectors: {e}")
823
+ else:
824
+ logger.warning(" Firebase not available β€” skipping Firebase wipe")
825
+
826
+ # 2. Clear in-memory state
827
+ SYLLABUS_MAP = {}
828
+ VECTOR_DB = []
829
+ VECTOR_MATRIX = None
830
+ EXAM_MAP = {}
831
+ logger.info(" βœ“ In-memory state cleared")
832
+
833
+ # 3. Full rebuild from PDFs with vision classifier
834
+ logger.info(" Starting full rebuild with vision-based parser ...")
835
+ build_index()
836
+ logger.info("=== CACHE RESET COMPLETE ===")
837
+
838
+ threading.Thread(target=_reset_bg, daemon=True).start()
839
+
840
+ return jsonify({
841
+ "status": "reset started",
842
+ "message": "Firebase cache wiped. Full rebuild running in background. "
843
+ "Check /health in 3-5 minutes to see subject and vector counts update. "
844
+ "All 24 PDFs will be re-parsed with the vision classifier.",
845
+ "watch": "/health"
846
+ }), 202
847
+
848
  @app.route('/v1/rebuild', methods=['POST'])
849
  def trigger_rebuild():
850
  secret = os.environ.get("REBUILD_SECRET", "")