TRIPEO commited on
Commit
561e4d7
·
verified ·
1 Parent(s): 062aaf5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -30,27 +30,29 @@ def generate_signature():
30
  def get_coordinates(location_name):
31
  prompt_template = PromptTemplate(
32
  input_variables=["location"],
33
- template="What are the exact latitude and longitude of {location}? Please provide numeric values only."
34
  )
35
 
36
  llm = ChatOpenAI(model="gpt-4", openai_api_key=openai.api_key)
37
  chain = LLMChain(llm=llm, prompt=prompt_template)
38
 
39
- # Attempt to get a valid response multiple times if necessary
40
- for attempt in range(3): # Let's try up to three times
41
- result = chain.run(location=location_name).strip()
42
- coordinates = result.split(",")
43
- try:
44
- # Try to convert received strings into float values
45
- lat = float(coordinates[0])
46
- lon = float(coordinates[1])
47
- return lat, lon
48
- except ValueError:
49
- st.warning(f"Attempt {attempt+1}: Unable to obtain valid coordinates. Received: '{result}'. Retrying...")
50
-
51
- # If after several attempts no valid coordinates were received, inform the user
52
- st.error("Failed to obtain valid geographical coordinates from the AI model. Please check the location name or try again later.")
53
- return None, None
 
 
54
 
55
  # Function to make the POST request to the Hotelbeds API and handle pagination
56
  def fetch_activities(latitude, longitude, from_date, to_date, language="en", paxes=[{"age": 5}, {"age": 70}], order="DEFAULT"):
 
30
  def get_coordinates(location_name):
31
  prompt_template = PromptTemplate(
32
  input_variables=["location"],
33
+ template="What are the exact latitude and longitude of {location}? Please provide the values in decimal degrees."
34
  )
35
 
36
  llm = ChatOpenAI(model="gpt-4", openai_api_key=openai.api_key)
37
  chain = LLMChain(llm=llm, prompt=prompt_template)
38
 
39
+ result = chain.run(location=location_name).strip()
40
+ # This line is new: processing the response to handle directional letters
41
+ try:
42
+ # Clean the result to remove degrees symbol and directional letters, then split by comma
43
+ cleaned_result = result.replace('°', '').replace(' N', '').replace(' S', '').replace(' E', '').replace(' W', '')
44
+ coordinates = cleaned_result.split(',')
45
+ # Convert to float, and handle negative values for southern and western hemispheres
46
+ latitude = float(coordinates[0].strip())
47
+ longitude = float(coordinates[1].strip())
48
+ if 'S' in result.split(',')[0]:
49
+ latitude = -latitude
50
+ if 'W' in result.split(',')[1]:
51
+ longitude = -longitude
52
+ return latitude, longitude
53
+ except ValueError as e:
54
+ st.error(f"Unable to parse coordinates from the AI's response: '{result}'. Error: {str(e)}")
55
+ return None, None
56
 
57
  # Function to make the POST request to the Hotelbeds API and handle pagination
58
  def fetch_activities(latitude, longitude, from_date, to_date, language="en", paxes=[{"age": 5}, {"age": 70}], order="DEFAULT"):