KrishPawargi commited on
Commit
23a2cfa
Β·
verified Β·
1 Parent(s): 840461a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from fpdf import FPDF
4
+ import os
5
+ from dotenv import load_dotenv
6
+ from datetime import datetime
7
+ import zipfile
8
+
9
+ # Langchain Imports
10
+ from langchain.memory import ConversationBufferMemory
11
+ from langchain.chains import ConversationChain
12
+ from langchain_community.llms import HuggingFaceHub
13
+
14
+ # πŸ”“ Extract .streamlit folder if zipped
15
+ if not os.path.exists(".streamlit"):
16
+ with zipfile.ZipFile(".streamlit.zip", 'r') as zip_ref:
17
+ zip_ref.extractall(".")
18
+
19
+ # βœ… Load .env variables
20
+ load_dotenv()
21
+ HF_API_TOKEN = os.getenv("HF_API_TOKEN")
22
+
23
+ # βœ… Initialize LangChain LLM with flan-t5-base
24
+ llm = HuggingFaceHub(
25
+ repo_id="google/flan-t5-base",
26
+ model_kwargs={"temperature": 0.7, "max_new_tokens": 512},
27
+ huggingfacehub_api_token=HF_API_TOKEN
28
+ )
29
+
30
+ # βœ… Setup LangChain memory and conversation
31
+ if "memory" not in st.session_state:
32
+ st.session_state.memory = ConversationBufferMemory()
33
+ if "conversation" not in st.session_state:
34
+ st.session_state.conversation = ConversationChain(
35
+ llm=llm,
36
+ memory=st.session_state.memory,
37
+ verbose=False
38
+ )
39
+
40
+ # βœ… PDF generation function
41
+ def save_trip_plan_as_pdf(text, filename="trip_plan.pdf"):
42
+ pdf = FPDF()
43
+ pdf.add_page()
44
+ pdf.set_font("Arial", size=12)
45
+ safe_text = text.encode('latin-1', 'ignore').decode('latin-1')
46
+ pdf.multi_cell(0, 10, safe_text)
47
+ pdf.output(filename)
48
+
49
+ # βœ… Streamlit UI
50
+ st.set_page_config(page_title="Intelligent Travel Planner", layout="wide")
51
+ st.title("🧳 Intelligent Travel Planner Agent")
52
+
53
+ # 🎯 User inputs
54
+ col1, col2 = st.columns(2)
55
+ with col1:
56
+ from_location = st.text_input("🏠 Your Current Location", placeholder="e.g., Mumbai")
57
+ destination = st.text_input("🌍 Destination", placeholder="e.g., Manali")
58
+ start_date = st.date_input("πŸ“… Start Date")
59
+ with col2:
60
+ end_date = st.date_input("πŸ“… End Date")
61
+ budget = st.text_input("πŸ’° Budget (in INR)", placeholder="e.g., 5000")
62
+ preferences = st.text_input("🎯 Preferences", placeholder="e.g., Adventure, Culture, Beaches")
63
+
64
+ generate_button = st.button("✈️ Generate Trip Plan")
65
+
66
+ # βœ… Generate and display AI trip plan
67
+ if generate_button:
68
+ if from_location and destination and budget and preferences and start_date and end_date:
69
+ user_prompt = (
70
+ f"Create a detailed day-wise travel itinerary with bullets and headings for a trip from {from_location} to {destination} in India. "
71
+ f"The trip should start on {start_date.strftime('%B %d, %Y')} and end on {end_date.strftime('%B %d, %Y')}, "
72
+ f"with a total budget of β‚Ή{budget} INR. The traveler prefers {preferences.lower()} experiences. \n\n"
73
+ f"Provide a **day-wise breakdown** of the trip including:\n"
74
+ f"- Top tourist attractions\n"
75
+ f"- Recommended local food\n"
76
+ f"- Suggested experiences (markets, nature, etc.)\n"
77
+ f"- Approximate daily expenses\n\n"
78
+ f"Make sure the total plan is budget-friendly and culturally immersive. End with final tips."
79
+ )
80
+
81
+ with st.spinner("🧠 Generating trip plan..."):
82
+ ai_response = st.session_state.conversation.run(user_prompt)
83
+
84
+ st.subheader("πŸ“‹ Your AI-Generated Trip Plan")
85
+ st.write(ai_response)
86
+
87
+ save_trip_plan_as_pdf(ai_response)
88
+ with open("trip_plan.pdf", "rb") as f:
89
+ st.download_button("πŸ“„ Download as PDF", f, file_name="trip_plan.pdf")
90
+
91
+ else:
92
+ st.warning("🚨 Please fill out all fields above.")
93
+
94
+ # 🧠 Follow-up Chat Section
95
+ st.markdown("---")
96
+ st.subheader("πŸ’¬ Ask Follow-Up Questions")
97
+ follow_up = st.text_input("Ask a follow-up about your trip plan")
98
+
99
+ if follow_up:
100
+ with st.spinner("πŸ’‘ Thinking..."):
101
+ response = st.session_state.conversation.run(follow_up)
102
+ st.markdown(f"**AI:** {response}")