File size: 6,637 Bytes
98689fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# This file was renamed from main.py for Hugging Face Spaces compatibility
import os
import certifi
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from typing import List, Optional
import uvicorn
from dotenv import load_dotenv

# Import custom modules
from utils.text_processor import normalize_text, clean_address
from utils.ocr import extract_text_from_image
from models.matcher import AddressMatcher

# Load environment variables
load_dotenv()

# Set SSL certificate path
os.environ['REQUESTS_CA_BUNDLE'] = certifi.where()
os.environ['SSL_CERT_FILE'] = certifi.where()

# Configuration
CSV_PATH = os.getenv("CSV_PATH", "../post/all_india_pincode_directory_2025.csv")
PORT = int(os.getenv("ML_PORT", 8000))
HOST = os.getenv("ML_HOST", "0.0.0.0")

# Initialize FastAPI app
app = FastAPI(
    title="AI Delivery Post Office Identification - ML Service",
    description="ML microservice for address matching and OCR extraction (Challenge 1)",
    version="1.0.0"
)

# CORS configuration
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Configure appropriately for production
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Initialize matcher
matcher = None

from contextlib import asynccontextmanager

@asynccontextmanager
async def lifespan(app: FastAPI):
    """Lifespan context manager for FastAPI application"""
    # Startup
    global matcher
    print("πŸš€ Starting ML Microservice...")
    print(f"πŸ“Š Loading dataset from: {CSV_PATH}")
    
    try:
        matcher = AddressMatcher(csv_path=CSV_PATH)
        await matcher.initialize()
        print(f"βœ… ML Service ready with {matcher.total_records} post office records")
    except Exception as e:
        print(f"❌ Failed to initialize matcher: {e}")
        raise
        
    yield  # Application running
    
    # Shutdown
    if matcher:
        print("πŸ“ Cleaning up resources...")
        # Add any cleanup if needed

# Update FastAPI app initialization with lifespan
app = FastAPI(
    title="AI Delivery Post Office Identification - ML Service",
    description="ML microservice for address matching and OCR extraction (Challenge 1)",
    version="1.0.0",
    lifespan=lifespan
)

# Pydantic models
class NormalizeRequest(BaseModel):
    text: str

class MatchRequest(BaseModel):
    text: str
    top_k: int = 5
    include_digipin: bool = True

class MatchResponse(BaseModel):
    query: str
    normalized_query: str
    matches: List[dict]
    processing_time_ms: float

class OCRResponse(BaseModel):
    raw_text: str
    clean_text: str
    confidence: Optional[float] = None

# API Endpoints

@app.get("/")
async def root():
    return {
        "status": "ok",
        "service": "ML Microservice",
        "version": "1.0.0",
        "records_loaded": matcher.total_records if matcher else 0,
        "endpoints": {
            "ocr": "POST /api/ml/ocr",
            "normalize": "POST /api/ml/normalize",
            "match": "POST /api/ml/match"
        }
    }

@app.get("/health")
async def health_check():
    """Detailed health check"""
    if not matcher or not matcher.is_ready:
        raise HTTPException(status_code=503, detail="Service not ready")
    
    return {
        "status": "healthy",
        "model_loaded": matcher.model is not None,
        "index_loaded": matcher.index is not None,
        "total_records": matcher.total_records
    }

@app.post("/api/ml/ocr", response_model=OCRResponse)
async def ocr_extract(file: UploadFile = File(...)):
    try:
        # Read image bytes
        image_bytes = await file.read()
        
        # Extract text using OCR
        raw_text, confidence = extract_text_from_image(image_bytes)
        
        # Clean the extracted text
        clean_text = clean_address(raw_text)
        
        return OCRResponse(
            raw_text=raw_text,
            clean_text=clean_text,
            confidence=confidence
        )
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"OCR extraction failed: {str(e)}")

@app.post("/api/ml/normalize", response_model=dict)
async def normalize_address(request: NormalizeRequest):
    try:
        normalized = normalize_text(request.text)
        cleaned = clean_address(request.text)
        
        return {
            "original": request.text,
            "normalized": normalized,
            "cleaned": cleaned
        }
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Normalization failed: {str(e)}")

@app.post("/api/ml/match", response_model=MatchResponse)
async def match_address(request: MatchRequest):
    if not matcher or not matcher.is_ready:
        raise HTTPException(status_code=503, detail="Matcher not initialized")
    
    try:
        # Perform matching
        results = await matcher.match(
            query_text=request.text,
            top_k=request.top_k,
            include_digipin=request.include_digipin
        )
        
        return results
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Matching failed: {str(e)}")

@app.post("/api/ml/ocr_match")
async def ocr_and_match(file: UploadFile = File(...), top_k: int = 5):
    """
    Combined endpoint: Extract text from image and match to post offices
    
    - **file**: Image file containing address
    - **top_k**: Number of matches to return
    - Returns OCR results and matching post offices
    """
    if not matcher or not matcher.is_ready:
        raise HTTPException(status_code=503, detail="Matcher not initialized")
    
    try:
        # Extract text from image
        image_bytes = await file.read()
        raw_text, ocr_confidence = extract_text_from_image(image_bytes)
        clean_text = clean_address(raw_text)
        
        # Match the extracted text
        results = await matcher.match(
            query_text=clean_text,
            top_k=top_k,
            include_digipin=True
        )
        
        # Combine OCR and matching results
        return {
            "ocr": {
                "raw_text": raw_text,
                "clean_text": clean_text,
                "confidence": ocr_confidence
            },
            "matching": results
        }
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"OCR+Match failed: {str(e)}")

if __name__ == "__main__":
    print(f"🌐 Starting server on {HOST}:{PORT}")
    uvicorn.run(app, host=HOST, port=PORT, log_level="info")