File size: 14,471 Bytes
67c0b51
 
 
 
 
 
 
 
 
e46dc8b
67c0b51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e46dc8b
67c0b51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e46dc8b
67c0b51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""

CivicAI Real ML Classification Server

3-Step Pipeline: YOLO (Pothole) β†’ CLIP (Streetlight/Garbage/Water Leak) β†’ Gemini Flash (Fallback)

"""

import os
import io
import json
import traceback
from typing import Optional

from dotenv import load_dotenv
load_dotenv()

from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from PIL import Image

# ─── App Setup ────────────────────────────────────────────────────────────────

app = FastAPI(title="CivicAI Classification Server", version="1.0.0")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ─── Global Model Variables (loaded once at startup) ─────────────────────────

yolo_model = None
clip_model = None
clip_processor = None

# CLIP prompt-to-category mapping
CLIP_PROMPTS = [
    "a deep hole or pothole in the road surface",
    "a cracked, fractured, or damaged road surface",
    "a pile of garbage, waste, or trash on the ground",
    "an overflowing public garbage bin or trash can",
    "water leaking, spraying, or dripping from a pipe or valve",
    "a flooded street or waterlogged road with deep water",
    "a street drain blocked by debris, leaves, or mud",
    "sewage, dirty water, or wastewater overflowing from a sewer",
    "an open manhole on the street, missing its cover",
    "a broken, dark, or non-functioning streetlight at night",
    "a fallen tree or large tree branch blocking the road or sidewalk",
    "a stray dog, cow, or other animal roaming on the street",
    "construction waste, bricks, concrete debris dumped illegally on the road side",
    "a broken, cracked, or damaged pedestrian footpath or sidewalk",
    "a shop extension, vehicle, stall, or structure blocking the public road or sidewalk",
    "something that is not an urban civic grievance",
]

CLIP_CATEGORY_MAP = {
    "a deep hole or pothole in the road surface": "Pothole",
    "a cracked, fractured, or damaged road surface": "Road Crack / Damaged Road",
    "a pile of garbage, waste, or trash on the ground": "Trash Pile",
    "an overflowing public garbage bin or trash can": "Overflowing Garbage Bin",
    "water leaking, spraying, or dripping from a pipe or valve": "Water Leakage",
    "a flooded street or waterlogged road with deep water": "Waterlogging / Flooded Road",
    "a street drain blocked by debris, leaves, or mud": "Blocked Drain",
    "sewage, dirty water, or wastewater overflowing from a sewer": "Sewage Overflow",
    "an open manhole on the street, missing its cover": "Open Manhole",
    "a broken, dark, or non-functioning streetlight at night": "Streetlight Not Working",
    "a fallen tree or large tree branch blocking the road or sidewalk": "Fallen Tree / Large Branch",
    "a stray dog, cow, or other animal roaming on the street": "Stray Animal",
    "construction waste, bricks, concrete debris dumped illegally on the road side": "Illegal Dumping of Construction Debris",
    "a broken, cracked, or damaged pedestrian footpath or sidewalk": "Damaged Footpath / Sidewalk",
    "a shop extension, vehicle, stall, or structure blocking the public road or sidewalk": "Encroachment / Obstruction on Road",
}

CONFIDENCE_THRESHOLD = 0.60


# ─── Model Loading at Startup ────────────────────────────────────────────────

@app.on_event("startup")
async def load_models():
    global yolo_model, clip_model, clip_processor

    print("=" * 60)
    print("  CivicAI ML Classification Server β€” Loading Models...")
    print("=" * 60)

    # 1. Load YOLO pothole detection model (public, non-gated)
    try:
        from ultralytics import YOLO
        print("[YOLO] Loading Pothole-Finetuned-YOLOv8 from Hugging Face ...")
        yolo_model = YOLO("https://huggingface.co/Harisanth/Pothole-Finetuned-YOLOv8/resolve/main/best.pt")
        print("[YOLO] βœ“ Model loaded successfully.")
    except Exception as e:
        print(f"[YOLO] βœ— Failed to load: {e}")
        traceback.print_exc()

    # 2. Load CLIP model and processor
    try:
        from transformers import CLIPModel, CLIPProcessor
        model_name = "openai/clip-vit-base-patch32"
        print(f"[CLIP] Loading {model_name} ...")
        clip_processor = CLIPProcessor.from_pretrained(model_name)
        clip_model = CLIPModel.from_pretrained(model_name)
        print("[CLIP] βœ“ Model loaded successfully.")
    except Exception as e:
        print(f"[CLIP] βœ— Failed to load: {e}")
        traceback.print_exc()

    print("=" * 60)
    print("  All models loaded. Server ready for classification.")
    print("=" * 60)


# ─── Step 1: YOLO Pothole Detection ──────────────────────────────────────────

def run_yolo(image: Image.Image) -> Optional[dict]:
    """Run YOLO pothole detection. Returns result dict if confident, else None."""
    if yolo_model is None:
        print("[YOLO] Model not available, skipping.")
        return None

    try:
        results = yolo_model(image, verbose=False)

        best_conf = 0.0
        for result in results:
            if result.boxes is not None and len(result.boxes) > 0:
                confidences = result.boxes.conf.cpu().numpy()
                max_conf = float(confidences.max())
                if max_conf > best_conf:
                    best_conf = max_conf

        print(f"[YOLO] Best pothole confidence: {best_conf:.4f}")

        if best_conf >= CONFIDENCE_THRESHOLD:
            return {
                "isValid": True,
                "category": "Pothole",
                "confidence": round(best_conf, 4),
                "source": "yolo"
            }
    except Exception as e:
        print(f"[YOLO] Error during inference: {e}")
        traceback.print_exc()

    return None


# ─── Step 2: CLIP Zero-Shot Classification ───────────────────────────────────

def run_clip(image: Image.Image) -> Optional[dict]:
    """Run CLIP zero-shot classification. Returns result dict if confident, else None."""
    if clip_model is None or clip_processor is None:
        print("[CLIP] Model not available, skipping.")
        return None

    try:
        import torch

        inputs = clip_processor(
            text=CLIP_PROMPTS,
            images=image,
            return_tensors="pt",
            padding=True
        )

        with torch.no_grad():
            outputs = clip_model(**inputs)

        logits_per_image = outputs.logits_per_image
        probs = logits_per_image.softmax(dim=1).cpu().numpy()[0]

        # Log all probabilities
        for prompt, prob in zip(CLIP_PROMPTS, probs):
            print(f"[CLIP]   {prompt}: {prob:.4f}")

        top_idx = int(probs.argmax())
        top_prompt = CLIP_PROMPTS[top_idx]
        top_conf = float(probs[top_idx])

        print(f"[CLIP] Top match: \"{top_prompt}\" ({top_conf:.4f})")

        # Check if the top prompt is a valid civic category (not the "not a grievance" prompt)
        if top_prompt in CLIP_CATEGORY_MAP and top_conf >= CONFIDENCE_THRESHOLD:
            return {
                "isValid": True,
                "category": CLIP_CATEGORY_MAP[top_prompt],
                "confidence": round(top_conf, 4),
                "source": "clip"
            }
    except Exception as e:
        print(f"[CLIP] Error during inference: {e}")
        traceback.print_exc()

    return None


# ─── Step 3: Gemini Flash Fallback ───────────────────────────────────────────

def run_gemini(image: Image.Image) -> dict:
    """Gemini Flash fallback for validation and classification."""
    api_key = os.getenv("GEMINI_API_KEY")
    if not api_key:
        print("[GEMINI] No API key found in environment.")
        return {
            "isValid": False,
            "reason": "Gemini API key not configured on server.",
            "source": "gemini"
        }

    try:
        from google import genai

        client = genai.Client(api_key=api_key)

        prompt = (
            "You are a civic grievance validator for an urban municipal platform. "
            "Look at this image carefully. First decide: is this a legitimate urban "
            "civic grievance that a city government should fix? If yes, classify it "
            "into exactly one of these 15 categories:\n"
            "1. Pothole\n"
            "2. Road Crack / Damaged Road\n"
            "3. Trash Pile\n"
            "4. Overflowing Garbage Bin\n"
            "5. Water Leakage\n"
            "6. Waterlogging / Flooded Road\n"
            "7. Blocked Drain\n"
            "8. Sewage Overflow\n"
            "9. Open Manhole\n"
            "10. Streetlight Not Working\n"
            "11. Fallen Tree / Large Branch\n"
            "12. Stray Animal\n"
            "13. Illegal Dumping of Construction Debris\n"
            "14. Damaged Footpath / Sidewalk\n"
            "15. Encroachment / Obstruction on Road\n\n"
            "If no, explain why in one short sentence. "
            "Respond only in JSON: { \"isValid\": true/false, \"category\": \"...\", \"reason\": \"...\" }"
        )

        response = client.models.generate_content(
            model="gemini-3-flash-preview",
            contents=[prompt, image],
        )

        response_text = response.text.strip()
        print(f"[GEMINI] Raw response: {response_text}")

        # Clean markdown code fences if present
        if response_text.startswith("```"):
            lines = response_text.split("\n")
            # Remove first and last lines (the fences)
            lines = [l for l in lines if not l.strip().startswith("```")]
            response_text = "\n".join(lines).strip()

        parsed = json.loads(response_text)

        if parsed.get("isValid", False):
            return {
                "isValid": True,
                "category": parsed.get("category", "Other"),
                "confidence": 0.75,
                "source": "gemini"
            }
        else:
            return {
                "isValid": False,
                "reason": parsed.get("reason", "Image was not recognized as a valid civic grievance."),
                "source": "gemini"
            }

    except Exception as e:
        print(f"[GEMINI] Error: {e}")
        traceback.print_exc()
        return {
            "isValid": False,
            "reason": f"Gemini classification failed: {str(e)}",
            "source": "gemini"
        }


# ─── Main Classification Endpoint ────────────────────────────────────────────

@app.post("/classify")
async def classify_image(file: UploadFile = File(...)):
    """

    3-step classification pipeline:

    1. YOLO (pothole detection)

    2. CLIP (streetlight / garbage / water leak)

    3. Gemini Flash (fallback validator + classifier)

    """
    try:
        print("\n" + "=" * 60)
        print(f"[REQUEST] Received image: {file.filename} ({file.content_type})")
        print("=" * 60)

        # Read and open image
        contents = await file.read()
        image = Image.open(io.BytesIO(contents)).convert("RGB")
        print(f"[IMAGE] Size: {image.size}, Mode: {image.mode}")

        # Step 1: YOLO
        print("\n--- Step 1: YOLO Pothole Detection ---")
        yolo_result = run_yolo(image)
        if (yolo_result is not None):
            # YOLO is only trained on potholes and can easily misclassify open manholes, drains, or sewage.
            # We run Gemini to verify it isn't one of these visually similar categories before finalizing.
            print("[YOLO Validation] Running Gemini Flash to verify it is indeed a pothole and not an open manhole, sewage, or drain...")
            validation_result = run_gemini(image)
            if validation_result.get("isValid", False):
                gemini_category = validation_result.get("category", "Pothole")
                if gemini_category != "Pothole":
                    print(f"[YOLO Validation] Gemini identified the issue as '{gemini_category}' (not a Pothole). Overriding YOLO.")
                    return JSONResponse(content=validation_result)
            
            print(f"[RESULT] YOLO confirmed: {yolo_result}")
            return JSONResponse(content=yolo_result)

        # Step 2: CLIP
        print("\n--- Step 2: CLIP Zero-Shot Classification ---")
        clip_result = run_clip(image)
        if clip_result is not None:
            print(f"[RESULT] CLIP classified: {clip_result}")
            return JSONResponse(content=clip_result)

        # Step 3: Gemini Flash Fallback
        print("\n--- Step 3: Gemini Flash Fallback ---")
        gemini_result = run_gemini(image)
        print(f"[RESULT] Gemini result: {gemini_result}")
        return JSONResponse(content=gemini_result)

    except Exception as e:
        print(f"[ERROR] Classification failed: {e}")
        traceback.print_exc()
        return JSONResponse(
            status_code=500,
            content={
                "isValid": False,
                "reason": f"Server error during classification: {str(e)}",
                "source": "error"
            }
        )


# ─── Health Check ─────────────────────────────────────────────────────────────

@app.get("/health")
async def health_check():
    return {
        "status": "ok",
        "yolo_loaded": yolo_model is not None,
        "clip_loaded": clip_model is not None and clip_processor is not None,
    }