Luis Chaves
added answers and improved how context is fetched from the chunks
ca919d4
from fastapi import FastAPI, UploadFile, HTTPException
from typing import List
from everycure.extractor import Entity, extract_entities_from_pdf
import logging
import uvicorn
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI(
title="Medical Entity Extraction API",
description="This API allows users to extract medically relevant entities from PDF documents using a pre-trained NER model.",
version="1.0.0"
)
from fastapi.middleware.cors import CORSMiddleware
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# Rest of your existing code
@app.post("/api/v1/extract", response_model=List[Entity])
async def extract_entities(file: UploadFile):
logger.info(f"Received request for file: {file.filename}")
if not file:
logger.error("No file provided")
raise HTTPException(status_code=400, detail="Bad request, file not included or empty filename")
if not file.filename.lower().endswith('.pdf'):
logger.error(f"Invalid file type: {file.filename}")
raise HTTPException(status_code=415, detail="Unsupported file type")
try:
logger.info("Starting entity extraction")
result = extract_entities_from_pdf(file)
logger.info(f"Successfully extracted {len(result)} entities")
return result
except Exception as e:
logger.error(f"Error during extraction: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)