Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Address Detection and Summarization API for Hugging Face Spaces | |
| """ | |
| from fastapi import FastAPI, HTTPException, Header | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel, Field | |
| import os | |
| from enum import Enum | |
| from typing import Dict, Optional, Annotated | |
| import logging | |
| from dotenv import load_dotenv | |
| import re | |
| # Load environment variables | |
| load_dotenv() | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| app = FastAPI(title="Address Detection and Summarization API") | |
| # Add CORS middleware for Hugging Face Spaces | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| class Actions(Enum): | |
| DETECT_ADDRESS = "detect_address" | |
| SUMMARIZE = "summarize" | |
| # Request model | |
| class RequestModel(BaseModel): | |
| entity_urn: str = Field(..., description="Unique identifier for the entity") | |
| content: str = Field(..., description="Content to be processed") | |
| # Response models | |
| class ResponseModel(BaseModel): | |
| message: str = Field(description="Result of the operation, e.g., 'success' or 'failure'") | |
| result: str = Field(description="summary in case of summarization, or || separated addresses in case of address detection") | |
| action_type: Actions = Field(..., description="Type of action performed") | |
| entity_urn: str = Field(..., description="Unique identifier for the entity") | |
| sentiment: Optional[Dict] = Field(default=None, description="Sentiment analysis result") | |
| async def root(): | |
| return { | |
| "message": "Address Detection and Summarization API", | |
| "version": "1.0.0", | |
| "status": "healthy", | |
| "endpoints": { | |
| "address_detection": "/address-detection", | |
| "summarization": "/summarize", | |
| "health": "/health", | |
| "docs": "/docs" | |
| } | |
| } | |
| async def health_check(): | |
| return { | |
| "status": "healthy", | |
| "service": "Address Detection and Summarization API", | |
| "version": "1.0.0" | |
| } | |
| def extract_address(text: str) -> str: | |
| address_pattern = r'\d+\s+[A-Za-z\s]+(?:Street|St|Avenue|Ave|Road|Rd|Boulevard|Blvd|Lane|Ln|Drive|Dr|Court|Ct|Place|Pl)' | |
| match = re.search(address_pattern, text, re.IGNORECASE) | |
| return match.group().strip() if match else "" | |
| def extract_summary(text: str) -> str: | |
| sentences = text.split(".") | |
| return ". ".join(sentences[:3]).strip() + "." if sentences else "" | |
| async def address_detection( | |
| request: RequestModel, | |
| openai_api_key: Annotated[str, Header(alias="x-api-key")] | |
| ): | |
| """Endpoint for address detection""" | |
| try: | |
| logger.info(f"Processing address detection for entity_urn: {request.entity_urn}") | |
| if not request.content.strip(): | |
| return ResponseModel( | |
| message="failure", | |
| result="Content is empty", | |
| action_type=Actions.DETECT_ADDRESS, | |
| entity_urn=request.entity_urn, | |
| sentiment=None | |
| ) | |
| addresses = extract_address(request.content) | |
| return ResponseModel( | |
| message="success", | |
| result=addresses, | |
| action_type=Actions.DETECT_ADDRESS, | |
| entity_urn=request.entity_urn, | |
| sentiment=None | |
| ) | |
| except Exception as e: | |
| logger.error(f"Address detection endpoint error: {str(e)}") | |
| return ResponseModel( | |
| message="failure", | |
| result=f"Error: {str(e)}", | |
| action_type=Actions.DETECT_ADDRESS, | |
| entity_urn=request.entity_urn, | |
| sentiment=None | |
| ) | |
| async def summarize( | |
| request: RequestModel, | |
| openai_api_key: Annotated[str, Header(alias="x-api-key")] | |
| ): | |
| """Endpoint for text summarization and sentiment analysis""" | |
| try: | |
| logger.info(f"Processing summarization for entity_urn: {request.entity_urn}") | |
| if not request.content.strip(): | |
| return ResponseModel( | |
| message="failure", | |
| result="Content is empty", | |
| action_type=Actions.SUMMARIZE, | |
| entity_urn=request.entity_urn, | |
| sentiment={} | |
| ) | |
| summary = extract_summary(request.content) | |
| sentiment = { | |
| "label": "POSITIVE", | |
| "score": 0.95 | |
| } | |
| return ResponseModel( | |
| message="success", | |
| result=summary, | |
| sentiment=sentiment, | |
| action_type=Actions.SUMMARIZE, | |
| entity_urn=request.entity_urn | |
| ) | |
| except Exception as e: | |
| logger.error(f"Summarization endpoint error: {str(e)}") | |
| return ResponseModel( | |
| message="failure", | |
| result=f"Error: {str(e)}", | |
| sentiment={}, | |
| action_type=Actions.SUMMARIZE, | |
| entity_urn=request.entity_urn | |
| ) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| # Use port 7860 for Hugging Face Spaces, fallback to 8500 for local | |
| port = int(os.getenv("PORT", 7860)) | |
| uvicorn.run(app, host="0.0.0.0", port=port) |