File size: 3,817 Bytes
23a2cfa
 
 
 
 
 
 
 
 
 
 
 
 
a1bab47
23a2cfa
 
 
 
 
 
 
 
 
a1bab47
23a2cfa
a1bab47
 
23a2cfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1ac7ac
23a2cfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1bab47
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import streamlit as st
import requests
from fpdf import FPDF
import os
from dotenv import load_dotenv
from datetime import datetime
import zipfile

# Langchain Imports
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
from langchain_community.llms import HuggingFaceHub


# πŸ”“ Extract .streamlit folder if zipped
if not os.path.exists(".streamlit"):
    with zipfile.ZipFile(".streamlit.zip", 'r') as zip_ref:
        zip_ref.extractall(".")

# βœ… Load .env variables
load_dotenv()
HF_API_TOKEN = os.getenv("HF_API_TOKEN")

# βœ… Initialize LangChain LLM
llm = HuggingFaceHub(
    repo_id="mistralai/Mistral-7B-Instruct-v0.1",
    model_kwargs={"temperature": 0.7, "max_new_tokens": 2048},
    huggingfacehub_api_token=HF_API_TOKEN
)

# βœ… Setup LangChain memory and conversation
if "memory" not in st.session_state:
    st.session_state.memory = ConversationBufferMemory()
if "conversation" not in st.session_state:
    st.session_state.conversation = ConversationChain(
        llm=llm,
        memory=st.session_state.memory,
        verbose=False
    )

# βœ… PDF generation function
def save_trip_plan_as_pdf(text, filename="trip_plan.pdf"):
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)
    safe_text = text.encode('latin-1', 'ignore').decode('latin-1')
    pdf.multi_cell(0, 10, safe_text)
    pdf.output(filename)

# βœ… Streamlit UI
st.set_page_config(page_title="Intelligent Travel Planner", layout="wide")
st.title("🧳 Intelligent Travel Planner Agent")

# 🎯 User inputs
col1, col2 = st.columns(2)
with col1:
    from_location = st.text_input("🏠 Your Current Location", placeholder="e.g., Mumbai")
    destination = st.text_input("🌍 Destination", placeholder="e.g., Manali")
    start_date = st.date_input("πŸ“… Start Date")
with col2:
    end_date = st.date_input("πŸ“… End Date")
    budget = st.text_input("πŸ’° Budget (in INR)", placeholder="e.g., 5000")
    preferences = st.text_input("🎯 Preferences", placeholder="e.g., Adventure, Culture, Beaches")

generate_button = st.button("✈️ Generate Trip Plan")

# βœ… Generate and display AI trip plan
if generate_button:
    if from_location and destination and budget and preferences and start_date and end_date:
        user_prompt = (
            f"Create a detailed day-wise travel itinerary with for a trip to {destination} in India. "
            f"The trip should start on {start_date.strftime('%B %d, %Y')} and end on {end_date.strftime('%B %d, %Y')}, "
            f"with a total budget of β‚Ή{budget} INR. The traveler prefers {preferences.lower()} experiences. \n\n"
            f"Provide a **day-wise breakdown** of the trip including:\n"
            f"- Top tourist attractions\n"
            f"- Recommended local food\n"
            f"- Suggested experiences (markets, nature, etc.)\n"
            f"- Approximate daily expenses\n\n"
            f"Make sure the total plan is budget-friendly and culturally immersive. End with final tips."
        )

        with st.spinner("🧠 Generating trip plan..."):
            ai_response = st.session_state.conversation.run(user_prompt)

        st.subheader("πŸ“‹ Your AI-Generated Trip Plan")
        st.write(ai_response)

        save_trip_plan_as_pdf(ai_response)
        with open("trip_plan.pdf", "rb") as f:
            st.download_button("πŸ“„ Download as PDF", f, file_name="trip_plan.pdf")

    else:
        st.warning("🚨 Please fill out all fields above.")

# 🧠 Follow-up Chat Section
st.markdown("---")
st.subheader("πŸ’¬ Ask Follow-Up Questions")
follow_up = st.text_input("Ask a follow-up about your trip plan")

if follow_up:
    with st.spinner("πŸ’‘ Thinking..."):
        response = st.session_state.conversation.run(follow_up)
        st.markdown(f"**AI:** {response}")