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