Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files19 unmodified lines2021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767719 unmodified lines# Clean location name for better searchclean_name = re.sub(r'\([^)]*\)', '', location_name).strip()search_query = f"{clean_name} {city}".strip()search_url = "https://maps.googleapis.com/maps/api/place/textsearch/json"params = {'query': search_query,'key': GOOGLE_PLACES_API_KEY,'type': 'establishment|tourist_attraction|park'}try:response = requests.get(search_url, params=params, timeout=10)response.raise_for_status()data = response.json()if data.get('results'):place = data['results'][0]place_id = place['place_id']# Get detailed infodetails_url = "https://maps.googleapis.com/maps/api/place/details/json"details_params = {'place_id': place_id,'fields': 'name,formatted_address,rating,price_level,opening_hours,website,formatted_phone_number,photos,url','key': GOOGLE_PLACES_API_KEY}details_response = requests.get(details_url, params=details_params, timeout=10)details_response.raise_for_status()details_data = details_response.json()if 'result' in details_data:result = details_data['result']location_info = {'name': result.get('name', location_name),'address': result.get('formatted_address', 'Address not available'),'rating': result.get('rating', 'N/A'),'google_maps_url': result.get('url', ''),'website': result.get('website', ''),'phone': result.get('formatted_phone_number', ''),'hours': result.get('opening_hours', {}).get('weekday_text', []) if result.get('opening_hours') else []}return location_info, "Location found"return None, "Could not find location"except requests.exceptions.RequestException as e:return None, f"Network error: {str(e)}"except Exception as e:return None, f"Location lookup error: {str(e)}"def extract_locations_from_recommendations(recommendations_text):"""19 unmodified lines202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511619 unmodified lines# Clean location name for better searchclean_name = re.sub(r'\([^)]*\)', '', location_name).strip()# Try multiple search variations for better resultssearch_queries = [f"{clean_name} {city}",f"{clean_name}", # Sometimes city context confuses the searchf"{location_name} {city}", # Try original name too]search_url = "https://maps.googleapis.com/maps/api/place/textsearch/json"for attempt, search_query in enumerate(search_queries):try:# Remove type restriction for first attempt, be more flexibleparams = {'query': search_query.strip(),'key': GOOGLE_PLACES_API_KEY}# Add type filter only on subsequent attemptsif attempt > 0:params['type'] = 'establishment|tourist_attraction|park|point_of_interest'response = requests.get(search_url, params=params, timeout=10)response.raise_for_status()data = response.json()if data.get('results') and len(data['results']) > 0:place = data['results'][0]place_id = place['place_id']# Get detailed infodetails_url = "https://maps.googleapis.com/maps/api/place/details/json"details_params = {'place_id': place_id,'fields': 'name,formatted_address,rating,price_level,opening_hours,website,formatted_phone_number,photos,url,geometry','key': GOOGLE_PLACES_API_KEY}details_response = requests.get(details_url, params=details_params, timeout=10)details_response.raise_for_status()details_data = details_response.json()if 'result' in details_data:result = details_data['result']# Create Google Maps URL if not providedmaps_url = result.get('url', '')if not maps_url and result.get('geometry', {}).get('location'):lat = result['geometry']['location']['lat']lng = result['geometry']['location']['lng']maps_url = f"https://www.google.com/maps/search/?api=1&query={lat},{lng}"location_info = {'name': result.get('name', location_name),'address': result.get('formatted_address', 'Address not available'),'rating': result.get('rating', 'N/A'),'google_maps_url': maps_url,'website': result.get('website', ''),'phone': result.get('formatted_phone_number', ''),'hours': result.get('opening_hours', {}).get('weekday_text', []) if result.get('opening_hours') else []}return location_info, "Location found"# If no results, try next search variationcontinueexcept requests.exceptions.RequestException as e:if attempt == len(search_queries) - 1: # Last attemptreturn None, f"Network error: {str(e)}"continueexcept Exception as e:if attempt == len(search_queries) - 1: # Last attemptreturn None, f"Location lookup error: {str(e)}"continue# If all attempts failed, create a basic Google Maps search URLfallback_query = f"{location_name} {city}".replace(' ', '+')fallback_url = f"https://www.google.com/maps/search/{fallback_query}"fallback_info = {'name': location_name,'address': f"Search for {location_name} in {city}",'rating': 'N/A','google_maps_url': fallback_url,'website': '','phone': '','hours': []}return fallback_info, "Fallback search link created"def extract_locations_from_recommendations(recommendations_text):"""
src/streamlit_app.py+81-42