Spaces:
Running
Running
Update services/location_service.py
Browse files- services/location_service.py +21 -4
services/location_service.py
CHANGED
@@ -4,6 +4,13 @@ from geopy.geocoders import Nominatim
|
|
4 |
from geopy.exc import GeocoderTimedOut, GeocoderUnavailable
|
5 |
from models.location_models import LocationData, Coordinates, ErrorResponse
|
6 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
class LocationService:
|
8 |
@staticmethod
|
9 |
def extract_location_entities(ner_results):
|
@@ -23,7 +30,17 @@ class LocationService:
|
|
23 |
return {k: v for k, v in {"city": city, "state": state, "country": country}.items() if v is not None}
|
24 |
else:
|
25 |
return None
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
@staticmethod
|
29 |
def get_coordinates(data:dict,model) -> Coordinates | ErrorResponse:
|
@@ -40,10 +57,10 @@ class LocationService:
|
|
40 |
|
41 |
if location:
|
42 |
# Assuming `app.nlp` is already initialized elsewhere and accessible
|
43 |
-
|
44 |
-
|
45 |
# Extract city, state, and country using the logic from extract_location_entities
|
46 |
-
location_entities =
|
47 |
|
48 |
if location_entities:
|
49 |
city = location_entities.get('city')
|
|
|
4 |
from geopy.exc import GeocoderTimedOut, GeocoderUnavailable
|
5 |
from models.location_models import LocationData, Coordinates, ErrorResponse
|
6 |
import requests
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
load_dotenv()
|
9 |
+
import json
|
10 |
+
|
11 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
12 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
13 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
14 |
class LocationService:
|
15 |
@staticmethod
|
16 |
def extract_location_entities(ner_results):
|
|
|
30 |
return {k: v for k, v in {"city": city, "state": state, "country": country}.items() if v is not None}
|
31 |
else:
|
32 |
return None
|
33 |
+
@staticmethod
|
34 |
+
def system_prompt(location : str) -> str:
|
35 |
+
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.
|
36 |
+
{location} \n
|
37 |
+
Rules:
|
38 |
+
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.
|
39 |
+
2. If any value is not found, return null.
|
40 |
+
3. If all values are null, return null.
|
41 |
+
Ensure the strictly that output is a valid JSON object containing strictly the above keys, without any explanations.
|
42 |
+
Generate a JSON response in the following format without using the ```json block. Ensure the output is properly formatted as plain text JSON.
|
43 |
+
"""
|
44 |
|
45 |
@staticmethod
|
46 |
def get_coordinates(data:dict,model) -> Coordinates | ErrorResponse:
|
|
|
57 |
|
58 |
if location:
|
59 |
# Assuming `app.nlp` is already initialized elsewhere and accessible
|
60 |
+
llm_prompt = LocationService.system_prompt(location)
|
61 |
+
response = model.generate_content(llm_prompt)
|
62 |
# Extract city, state, and country using the logic from extract_location_entities
|
63 |
+
location_entities = json.loads(response)
|
64 |
|
65 |
if location_entities:
|
66 |
city = location_entities.get('city')
|