Spaces:
Sleeping
Sleeping
Update app.py
Browse files36 unmodified lines37383940414243444546474830 unmodified lines7980818283848527 unmodified lines11311411511611711811912012112212312419 unmodified lines14414514614714814915022 unmodified lines17317417517617717817936 unmodified lines# --- Map related functions ---def extract_places(itinerary_text):# Simple heuristic: capitalized words (place-like)pattern = r"\b([A-Z][a-z]+(?:\s[A-Z][a-z]+)*)\b"matches = re.findall(pattern, itinerary_text)blacklist = {"Day", "Morning", "Evening", "Afternoon", "Breakfast", "Lunch", "Dinner"}places = [m for m in matches if m not in blacklist and len(m) > 2]return list(dict.fromkeys(places)) # Remove duplicatesdef geocode_location(place_name):url = "https://nominatim.openstreetmap.org/search"30 unmodified lines"""return html# --- Main itinerary function ---def generate_itinerary(mood, location, budget, days):mood_text = "any mood" if mood == "Any" else moodbudget_text = "any budget" if budget == "Any" else budget27 unmodified lineslat, lng = geocode_location(p)if lat and lng:locations.append({"name": p, "lat": lat, "lng": lng})time.sleep(1) # respect Nominatim rate limitif not locations:locations = [{"name": "World", "lat": 20, "lng": 0}]map_html = create_leaflet_map(locations)return itinerary_text, image_path, gr.update(visible=True), map_html, gr.update(visible=True)def generate_pdf(itinerary_text):if not itinerary_text.strip():19 unmodified linesc.save()return temp_pdf.name# --- Gradio UI ---with gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")) as demo:header_html = """<div style='text-align: center;'>22 unmodified linesgenerate_btn.click(generate_itinerary,inputs=[mood, location, budget, days],outputs=[itinerary_output, image_output, image_output, map_output, map_output])download_btn.click(generate_pdf, inputs=[itinerary_output], outputs=pdf_file)36 unmodified lines373839404142434445464730 unmodified lines78798081828327 unmodified lines11111211311411511611711811912012112219 unmodified lines14214314414514614722 unmodified lines17017117217317417517636 unmodified lines# --- Map related functions ---def extract_places(itinerary_text):pattern = r"\b([A-Z][a-z]+(?:\s[A-Z][a-z]+)*)\b"matches = re.findall(pattern, itinerary_text)blacklist = {"Day", "Morning", "Evening", "Afternoon", "Breakfast", "Lunch", "Dinner"}places = [m for m in matches if m not in blacklist and len(m) > 2]return list(dict.fromkeys(places)) # unique order preserveddef geocode_location(place_name):url = "https://nominatim.openstreetmap.org/search"30 unmodified lines"""return htmldef generate_itinerary(mood, location, budget, days):mood_text = "any mood" if mood == "Any" else moodbudget_text = "any budget" if budget == "Any" else budget27 unmodified lineslat, lng = geocode_location(p)if lat and lng:locations.append({"name": p, "lat": lat, "lng": lng})time.sleep(1) # respect Nominatim usage policyif not locations:locations = [{"name": "World", "lat": 20, "lng": 0}]map_html = create_leaflet_map(locations)return itinerary_text, image_path, gr.update(value=map_html, visible=True)def generate_pdf(itinerary_text):if not itinerary_text.strip():19 unmodified linesc.save()return temp_pdf.namewith gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")) as demo:header_html = """<div style='text-align: center;'>22 unmodified linesgenerate_btn.click(generate_itinerary,inputs=[mood, location, budget, days],outputs=[itinerary_output, image_output, map_output])download_btn.click(generate_pdf, inputs=[itinerary_output], outputs=pdf_file)
app.py+4-7