Spaces:
Running
Running
| """ | |
| FastAPI REST API for Foundation 1.2 Clinical Trial System | |
| Production-ready Docker space with proper REST endpoints | |
| """ | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| import time | |
| import logging | |
| # Import the foundation engine | |
| import foundation_engine | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| app = FastAPI( | |
| title="Clinical Trial API", | |
| description="Production REST API for clinical trial analysis powered by Foundation 1.2 pipeline", | |
| version="1.0.0", | |
| docs_url="/docs", | |
| redoc_url="/redoc" | |
| ) | |
| # Add CORS middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Request/Response models | |
| class QueryRequest(BaseModel): | |
| query: str | |
| class QueryResponse(BaseModel): | |
| summary: str | |
| processing_time: float | |
| class HealthResponse(BaseModel): | |
| status: str | |
| trials_loaded: int | |
| embeddings_loaded: bool | |
| async def startup_event(): | |
| """Initialize the foundation engine on startup""" | |
| logger.info("=== API Startup ===") | |
| logger.info("Loading Foundation 1.2 engine...") | |
| # The foundation_engine will load embeddings when first accessed | |
| foundation_engine.load_embeddings() | |
| logger.info("=== API Ready ===") | |
| async def root(): | |
| """API information""" | |
| return { | |
| "service": "Clinical Trial API", | |
| "version": "1.0.0", | |
| "description": "Production REST API for Foundation 1.2", | |
| "status": "healthy", | |
| "endpoints": { | |
| "POST /query": "Query clinical trials and get AI-generated summary", | |
| "GET /health": "Health check", | |
| "GET /docs": "Interactive API documentation (Swagger UI)", | |
| "GET /redoc": "Alternative API documentation (ReDoc)" | |
| }, | |
| "features": [ | |
| "Drug Scoring", | |
| "355M foundation model" | |
| ] | |
| } | |
| async def health_check(): | |
| """Health check endpoint""" | |
| embeddings_loaded = foundation_engine.doc_embeddings is not None | |
| chunks_loaded = len(foundation_engine.doc_chunks) if foundation_engine.doc_chunks else 0 | |
| return HealthResponse( | |
| status="healthy", | |
| trials_loaded=chunks_loaded, | |
| embeddings_loaded=embeddings_loaded | |
| ) | |
| async def query_trials(request: QueryRequest): | |
| """ | |
| Query clinical trials and get AI-generated summary | |
| - **query**: Your question about clinical trials (e.g., "What trials exist for Dekavil?") | |
| Returns a structured medical analysis with: | |
| - Drug/Intervention background | |
| - Clinical trial results and data | |
| - Treatment considerations | |
| - NCT trial IDs and references | |
| """ | |
| try: | |
| logger.info(f"API Query received: {request.query[:100]}...") | |
| start_time = time.time() | |
| # Call the foundation engine | |
| result = foundation_engine.process_query(request.query) | |
| processing_time = time.time() - start_time | |
| logger.info(f"Query completed in {processing_time:.2f}s") | |
| return QueryResponse( | |
| summary=result, | |
| processing_time=processing_time | |
| ) | |
| except Exception as e: | |
| logger.error(f"Error processing query: {str(e)}") | |
| raise HTTPException(status_code=500, detail=f"Error processing query: {str(e)}") | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=7860) | |