AI Travel Chatbot in Python [Python +Ai]

AI Travel Chatbot in Python

Introduction

Traveling is exciting, but planning a trip can often feel overwhelming. From finding the best destinations to booking hotels, flights, and activities — the process can take a lot of time. To solve this problem, the AI Travel Chatbot was developed using Python.

This chatbot acts like a personal travel assistant. It helps users get quick answers to travel-related questions, suggests destinations, and guides them with essential information like weather, hotel options, and transportation. For students, it’s an excellent project to understand Natural Language Processing (NLP), Machine Learning, and Python integration with real-world applications.

1. Imports & API Setup

import streamlit as st
import wikipedia
import google.generativeai as genai
import urllib.parse
  • streamlit: For creating the web app.
  • wikipedia: To fetch a short city description.
  • google.generativeai: To use Gemini API for travel suggestions.
  • urllib.parse: Helps create safe URLs for Google Maps links.

API Configuration:

genai.configure(api_key="")  # Add your Gemini API key

2. Helper Function – Google Maps Link

def get_maps_link(place, city):
    query = urllib.parse.quote(f"{place} {city}")
    return f"https://www.google.com/maps/search/?api=1&query={query}"
  • Combines place + city → formats into a Google Maps search link.
  • Ensures no spaces/special characters break the link.

3. Get City Description (Wikipedia)

def get_city_description(city):
    try:
        summary = wikipedia.summary(city, sentences=3, auto_suggest=False)
        return summary
    except wikipedia.DisambiguationError as e:
        try:
            summary = wikipedia.summary(e.options[0], sentences=3, auto_suggest=False)
            return summary
        except:
            return "❗ Couldn't find a proper description for this city."
    except:
        return "❗ Couldn't find a proper description for this city."

4. Get Travel Info

def get_travel_info_gemini(city):
    prompt = f"""Provide concise travel details for {city} including:
    1. Three famous places to visit
    2. Three popular local foods
    3. Three best malls
    4. Three recommended restaurants
    Only give names in bullet points, no extra description."""
    
    model = genai.GenerativeModel('models')
    response = model.generate_content(prompt)
    return response.text
  • Sends a prompt to Gemini asking for travel data.
  • Gemini replies with bullet-pointed names of places, foods, malls, and restaurants.

5. Stylish HTML + CSS for UI

st.markdown("""
    <style>
        .title { ... }
        .section-header { ... }
        .item { ... }
        .map-link { ... }
        .description { ... }
    </style>
""", unsafe_allow_html=True)
  • Custom CSS for:
    • Title (.title)
    • Headers like “Famous Places” (.section-header)
    • Each item row (.item)
    • Google Maps link styling (.map-link)
    • Description paragraph (.description)
  • Why use this? → Streamlit’s default look is basic, so this gives your app a clean, modern, and colorful design.
image-30 AI Travel Chatbot in Python [Python +Ai]
AI Travel Chatbot in Python
image-31-1024x567 AI Travel Chatbot in Python [Python +Ai]
AI Travel Chatbot in Python
image-32-1024x751 AI Travel Chatbot in Python [Python +Ai]
AI Travel Chatbot in Python

6. Main App UI

city = st.text_input("Enter City Name")
if st.button("Get Travel Info"):
    ...
  • Input field for the city name.
  • Button triggers all the logic (fetch Wikipedia + Gemini info + display results).

7. Parsing Gemini Response

lines = [line.strip('-• ').strip() for line in details.split('\n') if line.strip()]
current_section = None
for line in lines:
    ...
  • Splits the Gemini response line by line.
  • Filters each line into one of the 4 lists:
    • famous_places, foods, malls, restaurants
  • Limits to 3 items per list (skips extras).
  • If fewer than 3 items are found → fills with N/A later.

8. Reusable Display Function

def display_table(title, items, icon):
    st.markdown(f'<div class="section-header">{icon} {title}</div>', unsafe_allow_html=True)
    if items:
        for item in items:
            link = get_maps_link(item, city)
            st.markdown(f'<div class="item">🔹 <b>{item}</b> | <a class="map-link" href="{link}" target="_blank">Open in Google Maps</a></div>', unsafe_allow_html=True)
    else:
        for _ in range(3):
            st.markdown(f'<div class="item">🔹 <i>N/A</i></div>', unsafe_allow_html=True)
  • Displays each section (title + items + Google Maps links).
  • Repeats for all 4 categories.
  • Items always show as 3 lines (with links or “N/A”).

9. Explore City on Google Maps

st.markdown(f'<div class="section-header">🌐 Explore {city.title()} on <a class="map-link" href="{get_maps_link(city, "")}" target="_blank">Google Maps</a></div>', unsafe_allow_html=True)
  • General Google Maps link to explore the whole city.

 

Share this content:

Post Comment