File size: 5,849 Bytes
2542be6
 
 
714407c
94a3c4b
8d947c5
 
 
2b25034
8d947c5
 
 
 
2542be6
9ea563b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31a8117
 
5201f29
31a8117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d947c5
5201f29
8d947c5
 
 
 
 
 
 
 
 
421525c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ea563b
2542be6
c0c869a
2542be6
 
 
 
421525c
2542be6
9ea563b
 
 
 
 
 
2542be6
9ea563b
8d947c5
31a8117
9ea563b
3f5e4b0
2542be6
9ea563b
 
 
 
2542be6
9ea563b
 
2542be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut, GeocoderUnavailable
from models.location_models import LocationData, Coordinates, ErrorResponse
import google.generativeai as genai
import requests
from dotenv import load_dotenv
load_dotenv()
import json
import os

GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel('gemini-1.5-flash')
class LocationService:
    @staticmethod
    def extract_location_entities(ner_results):
        city, state, country = None, None, None
        
        for entity in ner_results:
            word = entity['word'].replace("##", "").strip()
            
            if entity['entity_group'] == 'CITY' and word.isalpha():
                city = word
            elif entity['entity_group'] == 'STATE' and word.isalpha():
                state = word
            elif entity['entity_group'] == 'COUNTRY' and word.isalpha():
                country = word
        
        if city or state or country:
            return {k: v for k, v in {"city": city, "state": state, "country": country}.items() if v is not None}
        else:
            return None

    @staticmethod
    def get_llm_response(system_prompt) -> str:
        url = "https://api.openai.com/v1/chat/completions"
        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}",
            "OpenAI-Organization": os.getenv('ORG_ID')
        }
    
        data = {
            "model": "gpt-4o-mini",
            "max_tokens": 2000,
            "messages": [{"role": "user", "content": f"{system_prompt}"}],
            "temperature": 0.0
        }
        try:
            response = requests.post(url, headers=headers, json=data)
            output = response.json()
        except Exception as e:
            print(f"Error Occurred {e}")
            return f"Error Occurred {e}"
    
        return output['choices'][0]['message']['content']
    
    @staticmethod
    def system_prompt(location : str) -> str:
        return f"""You are a Text Analyser where you will extract city , state , country from given piece of text given below.You will strictly extract following keys from the text country , state , city.
        {location} \n
        Rules:
        1. You will analyse the text and extract the country , city or state from the text , lets say if you have 'Udhampur, JK, India' , here JK means Jammu and Kashmir , so if you get any initials extract the exact name. 
        2. If any value is not found, return null.
        3. If all values are null, return null.
        Ensure the strictly that output is a valid JSON object containing strictly the above keys, without any explanations.
        Generate a JSON response in the following format without using the ```json block. Ensure the output is properly formatted as plain text JSON.
        """

    @staticmethod
    def get_lat_lng(data:dict):
        try:
            location = data.get('location')
        except:
            return Coordinates(latitude=None, longitude=None)
        url = f"https://maps.googleapis.com/maps/api/geocode/json?address={location}&key={os.getenv('GOOGLE_GEO_API_KEY')}"
        try:
            response = requests.get(url)
            json_data = response.json()
            lat = json_data.get('results',None)[0].get('geometry',None).get('location',None).get('lat')
            long = json_data.get('results',None)[0].get('geometry',None).get('location',None).get('lng')
            return Coordinates(
                latitude=lat,
                longitude=long
            ) 
        except Exception as e:
            return Coordinates(latitude=None , longitude=None)
        
        

    
    @staticmethod
    def get_coordinates(data:dict) -> Coordinates | ErrorResponse:
        print("Inside get coordinates")
        print(data)
        try:
            location = data.get('location')
            
        except:
            return Coordinates(latitude=None, longitude=None)
        
        city = None
        state = None
        country = None
        
        if location:
            # Assuming `app.nlp` is already initialized elsewhere and accessible
            llm_prompt = LocationService.system_prompt(location)
            response = LocationService.get_llm_response(llm_prompt)
            # Extract city, state, and country using the logic from extract_location_entities
            location_entities = json.loads(response)
            
            if location_entities:
                city = location_entities.get('city')
                state = location_entities.get('state')
                country = location_entities.get('country')
            
            # Create the location string
            location_string = ' '.join(filter(None, [city, state, country]))
            
            if not location_string:
                location_string = location
        else:
            return ErrorResponse(error="No location information provided")

        geolocator = Nominatim(user_agent="Geolocation")
        print("Printing location string")
        print(location_string)
        if city or state or country :
            location_string = city
        elif country is None:
            location_string = city
        elif city is None:
            location_string = state
        elif state is None:
            location_string = city

        try:
            getLoc = geolocator.geocode(location_string)
            print(getLoc.latitude)
            print(getLoc.longitude)
            return Coordinates(
                latitude=getLoc.latitude,
                longitude=getLoc.longitude
            ) 
        except Exception as e:
            print(f"Error {e}")
            return Coordinates(latitude=None , longitude=None)