CoolFace
Apppublic

newmark01/EventDataEtractor

sourceHugging Facemitupdated 10mo agoView on Hugging Face
0likes
app.py70 linesDownload Raw Back to root
1from fastapi import FastAPI, Response, status2from pydantic import BaseModel3import uvicorn4from extractor import EventInfoExtractor5import os6 7# --- Pydantic Models ---8# This defines the expected request body9class ExtractRequest(BaseModel):10    text: str11 12# --- App Setup ---13app = FastAPI(14    title="Event Extractor API",15    description="Extracts structured event info (date, time, location, price) from text.",16    version="1.0.0"17)18 19# --- Load the Model ---20# This is crucial: Load the model ONCE at startup, not per-request.21# This might take a few seconds when the container starts.22print("Loading EventInfoExtractor model...")23extractor = EventInfoExtractor()24print("Model loaded successfully.")25 26# --- API Endpoints ---27 28@app.get("/")29def read_root():30    """31    Root endpoint for a simple health check.32    """33    return {"message": "Event Extractor API is running. Go to /docs for API documentation."}34 35 36# --- NEW HEALTH ENDPOINT ---37@app.head("/health", status_code=status.HTTP_200_OK)38@app.get("/health", status_code=status.HTTP_200_OK)39async def read_health():40    """41    Simple health check endpoint.42    43    Returns a 200 OK with a status message.44    FastAPI automatically handles HEAD requests for this route.45    """46    return {"status": "ok"}47 48 49@app.post("/extract")50def extract_event_info(request: ExtractRequest):51    """52    The main endpoint to extract event information.53    54    Accepts a JSON object with a "text" key and returns the55    structured extraction data.56    """57    print(f"Received request for text: {request.text[:50]}...")58    try:59        result = extractor.extract_all(request.text)60        return result61    except Exception as e:62        print(f"Error during extraction: {e}")63        return {"error": "Failed to process text", "details": str(e)}64 65# --- Run the App (for local testing) ---66if __name__ == "__main__":67    # Get port from environment variable or default to 7860 (Hugging Face default)68    port = int(os.environ.get("PORT", 7860))69    print(f"Starting server on http://0.0.0.0:{port}")70    uvicorn.run(app, host="0.0.0.0", port=port)