karthi311 commited on
Commit
1ee439e
Β·
verified Β·
1 Parent(s): df22110

Update app.py

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