File size: 2,144 Bytes
cff882e
b5d0e7a
12bb7aa
cff882e
 
b5d0e7a
12bb7aa
 
cff882e
12bb7aa
cff882e
12bb7aa
 
 
 
cff882e
b5d0e7a
 
 
 
 
 
 
 
 
 
1b70432
b5d0e7a
 
 
 
 
 
12bb7aa
b5d0e7a
12bb7aa
 
 
 
 
 
 
b5d0e7a
12bb7aa
 
 
cff882e
1b70432
b5d0e7a
cff882e
12bb7aa
cff882e
b5d0e7a
 
12bb7aa
 
 
 
cff882e
 
1b70432
cff882e
 
 
12bb7aa
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
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()