File size: 2,567 Bytes
7cc0170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from models import Database
from geopy.geocoders import Nominatim
import os
from dotenv import load_dotenv
from models import NLPProcessor


nlp = NLPProcessor()
load_dotenv()

def match_technician(text: str, user_number: str):
    db = Database()
    try:
        service_type = nlp.extract_service(text)
        location = nlp.extract_location(text)
        
        if service_type == "unknown":
            return None, "Could not determine service. Please specify (e.g., 'AC repair', 'plumber')"
            
        if not location:
            return None, "Could not detect location. Try: 'AC repair in Nairobi'"
        
        longitude, latitude = geocode_location(location)
        technician = db.find_technician(service_type, longitude, latitude)
        
        if not technician:
            return None, f"No {service_type} technicians near {location}"
            
        db.save_request(user_number, technician["id"], service_type)
        return technician, None
        
    except Exception as e:
        return None, f"System error: {str(e)}"
    finally:
        db.close()

# def match_technician(text: str, user_number: str):
#     technician = None
#     db = Database()
#     try:
        
#         # Step 1: Extract service type and location using DistilBERT
#         service_type = nlp.extract_service(text)
#         location = nlp.extract_location(text)
        

#         if not location:
#             return None, "Could not detect your location. Please specify (e.g., 'AC repair in Nairobi')."
        
#         # Step 2: Get coordinates (mock function - replace with real geocoding)
#         longitude, latitude = geocode_location(location)
        
#         # Step 3: Find nearest technician
#         technician = db.find_technician(service_type, longitude, latitude)
#         if not technician:
#             db.close()
#             return None, f"No available {service_type} technicians near {location}."
        
#         # Step 4: Save request to DB
#         request_id = db.save_request(user_number, technician["id"], service_type)
#         return technician, None
#     except Exception as e:
#         return None, f"System error: {str(e)}"
#     finally:
#         db.close()

def geocode_location(location: str) -> tuple[float, float]:
    geolocator = Nominatim(user_agent="technician_matcher")
    location = geolocator.geocode(location + ", Kenya")  # Adjust country as needed
    if location:
        return (location.longitude, location.latitude)
    return (-1.2921, 36.8219)  # Fallback to Nairobi