Spaces:
Paused
Paused
Commit ·
92f1cfa
1
Parent(s): 6d6636b
Upd textbook fetcher
Browse filesFiles changed (2)
- app/db.py+12-1
- app/routers/import_doc.py+13-2
29 unmodified lines3031323329 unmodified lines# Loglogger.info(f"📦 PDF also stored to textbook bucket at: {TEXTBOOK_URI}")except Exception as e:logger.warning(f"⚠️ Failed to save to textbook storage: {e}")29 unmodified lines30313233343536373839404142434429 unmodified lines# Loglogger.info(f"📦 PDF also stored to textbook bucket at: {TEXTBOOK_URI}")except Exception as e:logger.warning(f"⚠️ Failed to save to textbook storage: {e}")# == PDF FETCHER ==async def fetch_textbook_pdf(doc_id: str):try:textbook_client = AsyncIOMotorClient(TEXTBOOK_URI)textbook_db = textbook_client.get_default_database()textbook_fs = AsyncIOMotorGridFSBucket(textbook_db)return await textbook_fs.open_download_stream_by_name(f"{doc_id}.pdf")except Exception as e:logger.warning(f"⚠️ Failed to fetch textbook PDF for {doc_id}: {e}")raise
123456711 unmodified lines19202122232459 unmodified lines848586# app/routers/import.pyfrom fastapi import APIRouter, HTTPExceptionfrom pydantic import BaseModelfrom app.db import get_db, get_gridfs, save_to_textbook_fsfrom app.services import google_books, open_library, internet_archive, project_gutenbergfrom app.services.ingest import parse_and_indeximport aiofiles, uuid, os11 unmodified linessource: strref: dict@router.post("")async def import_book(req: ImportRequest):logger.info(f"📥 Received import request: {req.dict()}")59 unmodified lineslogger.info(f"📚 Document {req.candidate_id} queued for indexing")return {"document_id": req.candidate_id, "status": "queued"}1234567811 unmodified lines2021222324252659 unmodified lines868788899091929394959697# app/routers/import_doc.pyfrom fastapi import APIRouter, HTTPExceptionfrom fastapi.responses import StreamingResponsefrom pydantic import BaseModelfrom app.db import get_db, get_gridfs, save_to_textbook_fs, fetch_textbook_pdffrom app.services import google_books, open_library, internet_archive, project_gutenbergfrom app.services.ingest import parse_and_indeximport aiofiles, uuid, os11 unmodified linessource: strref: dict# Embedding, query and PDF saver to buckets@router.post("")async def import_book(req: ImportRequest):logger.info(f"📥 Received import request: {req.dict()}")59 unmodified lineslogger.info(f"📚 Document {req.candidate_id} queued for indexing")return {"document_id": req.candidate_id, "status": "queued"}# Fetch textbook on id@router.get("/textbook/{doc_id}")async def get_textbook(doc_id: str):try:stream = await fetch_textbook_pdf(doc_id)return StreamingResponse(stream, media_type="application/pdf")except Exception as e:logger.error(f"❌ Failed to serve textbook {doc_id}: {e}")raise HTTPException(404, "Textbook not found")
app/db.py+12-1
app/routers/import_doc.py+13-2