AnomalyDetector / app.py
anuradhakoppala's picture
Update app.py
1b70432 verified
import gradio as gr
import requests
from datetime import datetime
import os
from dotenv import load_dotenv
from utils.salesforce import create_checkin, create_anomaly_log
from utils.gps_validation import is_within_radius
load_dotenv()
THRESHOLD = int(os.getenv("THRESHOLD_FACE_MATCH", 85))
EXPECTED_LAT = float(os.getenv("EXPECTED_LAT"))
EXPECTED_LONG = float(os.getenv("EXPECTED_LONG"))
GPS_RADIUS_METERS = int(os.getenv("GPS_RADIUS_METERS", 200))
def verify_faces_api(image1, image2):
api_url = "https://huggingface.co/spaces/huggingface-projects/face-verification/+/api/predict"
files = {
"data": (None, "file"),
"data": image1,
"data": image2
}
response = requests.post(api_url, files=files)
try:
result = response.json()
score = result["data"][0]["confidence"] * 100
return round(score, 2)
except Exception as e:
return 0.0
def process_checkin(selfie_img, reference_img, agent_id, assignment_id, latitude, longitude):
score = verify_faces_api(reference_img, selfie_img)
gps_match = is_within_radius(latitude, longitude, EXPECTED_LAT, EXPECTED_LONG, GPS_RADIUS_METERS)
status = "OK"
if score < THRESHOLD and not gps_match:
status = "Both"
elif score < THRESHOLD:
status = "FaceMismatch"
elif not gps_match:
status = "GPSMismatch"
timestamp = datetime.utcnow().isoformat()
checkin_id = create_checkin(agent_id, assignment_id, latitude, longitude, timestamp)
create_anomaly_log(checkin_id, score, gps_match, status, f"Auto-check: score={score:.2f}")
return f"\ud83d\udc4c Status: {status}\nFace Match Score: {score:.2f}\nGPS Match: {gps_match}"
iface = gr.Interface(
fn=process_checkin,
inputs=[
gr.Image(label="Selfie Image"),
gr.Image(label="Reference Image"),
gr.Text(label="Agent ID (Salesforce Record ID)"),
gr.Text(label="Assignment ID"),
gr.Number(label="Latitude"),
gr.Number(label="Longitude")
],
outputs="text",
title="\ud83d\udccd Field Agent Anomaly Check-In"
)
if __name__ == "__main__":
iface.launch()