Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions backend/hf_api_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
# Zero-Shot Image Classification Model
CLIP_API_URL = "https://router.huggingface.co/models/openai/clip-vit-base-patch32"

# Zero-Shot Text Classification Model
ZERO_SHOT_API_URL = "https://router.huggingface.co/models/facebook/bart-large-mnli"

# Image Captioning Model
CAPTION_API_URL = "https://router.huggingface.co/models/Salesforce/blip-image-captioning-large"

Expand Down Expand Up @@ -456,3 +459,46 @@ async def detect_abandoned_vehicle_clip(image: Union[Image.Image, bytes], client
labels = ["abandoned car", "rusted vehicle", "car with flat tires", "wrecked car", "normal parked car"]
targets = ["abandoned car", "rusted vehicle", "car with flat tires", "wrecked car"]
return await _detect_clip_generic(image, labels, targets, client)

async def detect_category_text(text: str, client: httpx.AsyncClient = None):
"""
Classifies text into categories using Zero-Shot Classification.
"""
if not text: return {"category": "unknown", "confidence": 0}

labels = ["road issue", "water supply", "garbage", "streetlight", "infrastructure", "drainage", "safety"]

payload = {
"inputs": text,
"parameters": {"candidate_labels": labels}
}

if client:
result = await _make_request(client, ZERO_SHOT_API_URL, payload)
else:
async with httpx.AsyncClient() as new_client:
result = await _make_request(new_client, ZERO_SHOT_API_URL, payload)

# Result format: {'labels': ['road issue', ...], 'scores': [0.9, ...]}
if isinstance(result, dict) and 'labels' in result and 'scores' in result:
top_label = result['labels'][0]
top_score = result['scores'][0]

# Map to internal categories
category_map = {
"road issue": "road",
"water supply": "water",
"garbage": "garbage",
"streetlight": "streetlight",
"infrastructure": "college_infra",
"drainage": "water",
"safety": "women_safety"
}

return {
"category": category_map.get(top_label, "road"),
"confidence": top_score,
"original_label": top_label
}
Comment on lines +483 to +502
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Consider handling potential IndexError for empty result arrays.

If the API returns valid structure but empty labels or scores arrays, accessing index [0] would raise an IndexError. Add a length check for robustness.

🛡️ Proposed defensive fix
     # Result format: {'labels': ['road issue', ...], 'scores': [0.9, ...]}
-    if isinstance(result, dict) and 'labels' in result and 'scores' in result:
+    if isinstance(result, dict) and 'labels' in result and 'scores' in result and len(result['labels']) > 0 and len(result['scores']) > 0:
         top_label = result['labels'][0]
         top_score = result['scores'][0]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if isinstance(result, dict) and 'labels' in result and 'scores' in result:
top_label = result['labels'][0]
top_score = result['scores'][0]
# Map to internal categories
category_map = {
"road issue": "road",
"water supply": "water",
"garbage": "garbage",
"streetlight": "streetlight",
"infrastructure": "college_infra",
"drainage": "water",
"safety": "women_safety"
}
return {
"category": category_map.get(top_label, "road"),
"confidence": top_score,
"original_label": top_label
}
if isinstance(result, dict) and 'labels' in result and 'scores' in result and len(result['labels']) > 0 and len(result['scores']) > 0:
top_label = result['labels'][0]
top_score = result['scores'][0]
# Map to internal categories
category_map = {
"road issue": "road",
"water supply": "water",
"garbage": "garbage",
"streetlight": "streetlight",
"infrastructure": "college_infra",
"drainage": "water",
"safety": "women_safety"
}
return {
"category": category_map.get(top_label, "road"),
"confidence": top_score,
"original_label": top_label
}
🤖 Prompt for AI Agents
In `@backend/hf_api_service.py` around lines 483 - 502, The block that assumes
non-empty result['labels'] and result['scores'] can raise IndexError; modify the
branch that checks isinstance(result, dict) and 'labels'/'scores' to also verify
that result['labels'] and result['scores'] are non-empty (e.g., check len(...) >
0) before accessing [0], and if empty return a safe default (for example
category_map.get with a fallback like "road", confidence 0.0, and original_label
set to None or ""), optionally logging a warning; update the variables
top_label/top_score usage accordingly so the function returns a robust default
when arrays are empty.


return {"category": "unknown", "confidence": 0}
Loading