Update app.py
Browse files
app.py
CHANGED
@@ -1,208 +1,209 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import requests
|
3 |
-
import os
|
4 |
-
|
5 |
-
from
|
6 |
-
from langchain_core.
|
7 |
-
from
|
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 |
-
# background-image: url("https://images.unsplash.com/photo-
|
70 |
-
background-image: url("https://images.unsplash.com/photo-
|
71 |
-
|
72 |
-
background-
|
73 |
-
background-
|
74 |
-
background-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
font-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
.
|
113 |
-
.
|
114 |
-
.
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
border:
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
color:
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
font-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
color:
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
#
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
st.markdown('<
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
#
|
182 |
-
#
|
183 |
-
#
|
184 |
-
#
|
185 |
-
# st.
|
186 |
-
# st.
|
187 |
-
# st.
|
188 |
-
#
|
189 |
-
#
|
190 |
-
|
191 |
-
|
192 |
-
#
|
193 |
-
# st.
|
194 |
-
|
195 |
-
|
196 |
-
#
|
197 |
-
#
|
198 |
-
#
|
199 |
-
#
|
200 |
-
#
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
st.
|
207 |
-
|
|
|
208 |
st.warning("Please fill in all fields.")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
os.system("pip install langchain-groq==0.1.3")
|
5 |
+
from langchain_groq import ChatGroq
|
6 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
7 |
+
from langchain_core.prompts import ChatPromptTemplate
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
load_dotenv()
|
11 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
12 |
+
|
13 |
+
# Validate API Key
|
14 |
+
if not GROQ_API_KEY:
|
15 |
+
raise ValueError("GROQ_API_KEY environment variable is not set")
|
16 |
+
|
17 |
+
# Function to fetch Wikipedia summary
|
18 |
+
def search_travel_info(destination):
|
19 |
+
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{destination}"
|
20 |
+
response = requests.get(url)
|
21 |
+
if response.status_code == 200:
|
22 |
+
data = response.json()
|
23 |
+
return data.get("extract", "No information found.")
|
24 |
+
return "No results found."
|
25 |
+
|
26 |
+
# Function to generate travel itinerary using Groq API
|
27 |
+
def generate_itinerary(start_location, budget, duration, destination, purpose, preferences):
|
28 |
+
search_results = search_travel_info(destination)
|
29 |
+
|
30 |
+
# Create prompt template
|
31 |
+
prompt_template = ChatPromptTemplate.from_messages([
|
32 |
+
SystemMessage(content="You are an expert travel guide. Provide a detailed travel itinerary."),
|
33 |
+
HumanMessage(content=f"""
|
34 |
+
Create a {duration}-day travel itinerary for a traveler going from {start_location} to {destination}.
|
35 |
+
|
36 |
+
### π·οΈ Traveler Information:
|
37 |
+
- Budget: {budget}
|
38 |
+
- Purpose of Travel: {purpose}
|
39 |
+
- Preferences: {preferences}
|
40 |
+
|
41 |
+
### π Day-wise Itinerary:
|
42 |
+
- π Activities (morning, afternoon, evening)
|
43 |
+
- π Attractions (landmarks & hidden gems)
|
44 |
+
- π½οΈ Food recommendations
|
45 |
+
- π¨ Accommodation options
|
46 |
+
- π Transportation details
|
47 |
+
|
48 |
+
### π Additional Travel Info:
|
49 |
+
{search_results}
|
50 |
+
""")
|
51 |
+
])
|
52 |
+
|
53 |
+
# Initialize Groq Chat Model (Llama3-8B)
|
54 |
+
llm = ChatGroq(temperature=0,model_name="llama-3.3-70b-versatile", api_key=GROQ_API_KEY)
|
55 |
+
|
56 |
+
# Generate response
|
57 |
+
response = llm.invoke(prompt_template.format())
|
58 |
+
|
59 |
+
return response.content if response else "Error: Unable to generate itinerary."
|
60 |
+
|
61 |
+
# Set page config (add this at the very top)
|
62 |
+
st.set_page_config(page_title="AI Travel Planner", page_icon="βοΈ")
|
63 |
+
|
64 |
+
# Apply global background image
|
65 |
+
st.markdown(
|
66 |
+
"""
|
67 |
+
<style>
|
68 |
+
.stApp {
|
69 |
+
# background-image: url("https://images.unsplash.com/photo-1436491865332-7a61a109cc05");
|
70 |
+
# background-image: url("https://images.unsplash.com/photo-1506748686214-e9df14d4d9d0");
|
71 |
+
background-image: url("https://images.unsplash.com/photo-1483728642387-6c3bdd6c93e5");
|
72 |
+
# background-image: url("https://images.unsplash.com/photo-1518563259479-d003c05a6507?q=80&w=1470&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D");
|
73 |
+
background-size: cover;
|
74 |
+
background-position: center;
|
75 |
+
background-attachment: fixed;
|
76 |
+
filter: brightness(90%);
|
77 |
+
}
|
78 |
+
.stApp::before {
|
79 |
+
content: "";
|
80 |
+
position: absolute;
|
81 |
+
top: 0;
|
82 |
+
left: 0;
|
83 |
+
right: 0;
|
84 |
+
bottom: 0;
|
85 |
+
background: rgba(0, 0, 0, 0.4); /* 40% opaque black */
|
86 |
+
z-index: -1;
|
87 |
+
}
|
88 |
+
.form-title {
|
89 |
+
color: white !important;
|
90 |
+
font-size: 2rem;
|
91 |
+
font-weight: bold;
|
92 |
+
text-align: center;
|
93 |
+
}
|
94 |
+
.form-subtitle {
|
95 |
+
color: white !important;
|
96 |
+
font-size: 1.2rem;
|
97 |
+
text-align: center;
|
98 |
+
margin-bottom: 1.5rem;
|
99 |
+
}
|
100 |
+
/* Frosted Glass Box */
|
101 |
+
.frosted-glass-box {
|
102 |
+
background: rgba(255, 255, 255, 0.2);
|
103 |
+
backdrop-filter: blur(10px);
|
104 |
+
-webkit-backdrop-filter: blur(10px);
|
105 |
+
border-radius: 15px;
|
106 |
+
padding: 2rem;
|
107 |
+
box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.2);
|
108 |
+
width: 80%;
|
109 |
+
margin: auto;
|
110 |
+
}
|
111 |
+
/* Input Styling */
|
112 |
+
.stTextInput input,
|
113 |
+
.stSelectbox select,
|
114 |
+
.stNumberInput input,
|
115 |
+
.stTextArea textarea {
|
116 |
+
color: #000000 !important;
|
117 |
+
background-color: rgba(255, 255, 255, 0.7) !important;
|
118 |
+
border-radius: 8px !important;
|
119 |
+
border: 1px solid rgba(0, 0, 0, 0.1) !important;
|
120 |
+
padding: 10px;
|
121 |
+
font-size: 1rem;
|
122 |
+
}
|
123 |
+
/* Button Styling */
|
124 |
+
.stButton button {
|
125 |
+
background-color: black !important;
|
126 |
+
color: white !important;
|
127 |
+
border-radius: 8px !important;
|
128 |
+
padding: 10px 20px !important;
|
129 |
+
font-size: 1rem;
|
130 |
+
font-weight: bold !important;
|
131 |
+
width: 100% !important;
|
132 |
+
margin-top: 1rem !important;
|
133 |
+
border: none !important;
|
134 |
+
}
|
135 |
+
.stButton button:hover {
|
136 |
+
background-color: white !important;
|
137 |
+
color: black !important;
|
138 |
+
border: 1.5px solid black !important;
|
139 |
+
}
|
140 |
+
/* Itinerary Box */
|
141 |
+
.itinerary-box {
|
142 |
+
background: rgba(255, 255, 255, 0.7);
|
143 |
+
border-radius: 15px;
|
144 |
+
padding: 1.5rem;
|
145 |
+
margin-top: 1.5rem;
|
146 |
+
width: 80%;
|
147 |
+
margin: auto;
|
148 |
+
}
|
149 |
+
</style>
|
150 |
+
""",
|
151 |
+
unsafe_allow_html=True
|
152 |
+
)
|
153 |
+
|
154 |
+
# Start of the frosted glass box
|
155 |
+
# st.markdown('<div class="frosted-glass-box">', unsafe_allow_html=True)
|
156 |
+
|
157 |
+
# Title and Subtitle
|
158 |
+
st.markdown('<h1 class="form-title">AI-Powered Travel Planner βοΈ</h1>', unsafe_allow_html=True)
|
159 |
+
st.markdown('<p class="form-subtitle">Plan your next trip with AI!</p>', unsafe_allow_html=True)
|
160 |
+
|
161 |
+
# Form Elements (Columns for Structure)
|
162 |
+
col1, col2 = st.columns(2)
|
163 |
+
with col1:
|
164 |
+
start_location = st.text_input("π Starting Location")
|
165 |
+
with col2:
|
166 |
+
destination = st.text_input("π Destination")
|
167 |
+
|
168 |
+
col3, col4 = st.columns(2)
|
169 |
+
with col3:
|
170 |
+
budget = st.selectbox("π° Select Budget", ["Low", "Moderate", "Luxury"])
|
171 |
+
with col4:
|
172 |
+
duration = st.number_input("π
Trip Duration (days)", min_value=1, max_value=30, value=3)
|
173 |
+
|
174 |
+
# Full-width Form Elements
|
175 |
+
purpose = st.text_area("π Purpose of Trip")
|
176 |
+
preferences = st.text_area("π― Your Preferences (e.g., adventure, food, history)")
|
177 |
+
|
178 |
+
# Close the frosted glass div
|
179 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
180 |
+
|
181 |
+
# # Button and Itinerary Box
|
182 |
+
# if st.button("π Generate Itinerary"):
|
183 |
+
# if start_location and destination and purpose and preferences:
|
184 |
+
# itinerary = f"Here is a sample itinerary for {start_location} to {destination}."
|
185 |
+
# st.markdown('<div class="itinerary-box">', unsafe_allow_html=True)
|
186 |
+
# st.subheader("π Your AI-Generated Itinerary:")
|
187 |
+
# st.write(itinerary)
|
188 |
+
# st.markdown('</div>', unsafe_allow_html=True)
|
189 |
+
# else:
|
190 |
+
# st.warning("β οΈ Please fill in all fields.")
|
191 |
+
|
192 |
+
# # Streamlit UI
|
193 |
+
# st.title("AI-Powered Travel Planner π§³")
|
194 |
+
# st.write("Plan your next trip with AI!")
|
195 |
+
|
196 |
+
# start_location = st.text_input("Starting Location")
|
197 |
+
# destination = st.text_input("Destination")
|
198 |
+
# budget = st.selectbox("Select Budget", ["Low", "Moderate", "Luxury"])
|
199 |
+
# duration = st.number_input("Trip Duration (days)", min_value=1, max_value=30, value=3)
|
200 |
+
# purpose = st.text_area("Purpose of Trip")
|
201 |
+
# preferences = st.text_area("Your Preferences (e.g., adventure, food, history)")
|
202 |
+
|
203 |
+
if st.button("Generate Itinerary"):
|
204 |
+
if start_location and destination and purpose and preferences:
|
205 |
+
itinerary = generate_itinerary(start_location, budget, duration, destination, purpose, preferences)
|
206 |
+
st.subheader("Your AI-Generated Itinerary:")
|
207 |
+
st.write(itinerary)
|
208 |
+
else:
|
209 |
st.warning("Please fill in all fields.")
|