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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import base64
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# Function to encode the image file
|
| 6 |
+
def get_base64_of_bin_file(bin_file):
|
| 7 |
+
with open(bin_file, 'rb') as f:
|
| 8 |
+
data = f.read()
|
| 9 |
+
return base64.b64encode(data).decode() # Return after file is read
|
| 10 |
+
|
| 11 |
+
# Load the background image
|
| 12 |
+
background_image_path = 'assets/map.webp'
|
| 13 |
+
background_image = get_base64_of_bin_file(background_image_path)
|
| 14 |
+
|
| 15 |
+
# Set up common styles for both pages
|
| 16 |
+
def set_styles(is_chatbot_page):
|
| 17 |
+
if is_chatbot_page:
|
| 18 |
+
st.markdown(
|
| 19 |
+
"""
|
| 20 |
+
<style>
|
| 21 |
+
.stApp {
|
| 22 |
+
background: none;
|
| 23 |
+
height: 100vh;
|
| 24 |
+
color: black;
|
| 25 |
+
display: flex;
|
| 26 |
+
flex-direction: column;
|
| 27 |
+
align-items: center;
|
| 28 |
+
justify-content: center;
|
| 29 |
+
padding: 2rem;
|
| 30 |
+
}
|
| 31 |
+
.chatbot-title {
|
| 32 |
+
font-size: 36px;
|
| 33 |
+
font-weight: bold;
|
| 34 |
+
margin-bottom: 20px;
|
| 35 |
+
}
|
| 36 |
+
.button-container {
|
| 37 |
+
display: flex;
|
| 38 |
+
justify-content: center;
|
| 39 |
+
width: 100%;
|
| 40 |
+
}
|
| 41 |
+
.button {
|
| 42 |
+
background-color: #2196F3; /* Blue background */
|
| 43 |
+
color: white; /* White text */
|
| 44 |
+
border: 2px solid #2196F3; /* Blue border */
|
| 45 |
+
padding: 15px 32px;
|
| 46 |
+
text-align: center;
|
| 47 |
+
text-decoration: none;
|
| 48 |
+
display: inline-block;
|
| 49 |
+
font-size: 18px;
|
| 50 |
+
margin: 4px 2px;
|
| 51 |
+
cursor: pointer;
|
| 52 |
+
border-radius: 4px;
|
| 53 |
+
width: auto; /* Auto width based on content */
|
| 54 |
+
max-width: 400px; /* Max width for button */
|
| 55 |
+
transition: background-color 0.3s, border-color 0.3s;
|
| 56 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
| 57 |
+
white-space: nowrap; /* Prevent text from wrapping */
|
| 58 |
+
overflow: hidden; /* Hide overflow */
|
| 59 |
+
}
|
| 60 |
+
.button:hover {
|
| 61 |
+
background-color: #1976D2; /* Darker blue */
|
| 62 |
+
border-color: #1976D2; /* Darker blue border */
|
| 63 |
+
}
|
| 64 |
+
</style>
|
| 65 |
+
""",
|
| 66 |
+
unsafe_allow_html=True
|
| 67 |
+
)
|
| 68 |
+
else:
|
| 69 |
+
st.markdown(
|
| 70 |
+
f"""
|
| 71 |
+
<style>
|
| 72 |
+
.stApp {{
|
| 73 |
+
background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url("data:image/webp;base64,{background_image}");
|
| 74 |
+
background-size: cover;
|
| 75 |
+
background-position: center;
|
| 76 |
+
background-attachment: fixed;
|
| 77 |
+
height: 100vh;
|
| 78 |
+
padding: 2rem;
|
| 79 |
+
color: white;
|
| 80 |
+
}}
|
| 81 |
+
.title {{
|
| 82 |
+
font-size: 48px;
|
| 83 |
+
font-weight: bold;
|
| 84 |
+
text-shadow: 2px 2px 5px black;
|
| 85 |
+
margin-top: 30px;
|
| 86 |
+
text-align: center;
|
| 87 |
+
}}
|
| 88 |
+
.button {{
|
| 89 |
+
background-color: #4CAF50; /* Green background */
|
| 90 |
+
color: white; /* White text */
|
| 91 |
+
border: 2px solid #4CAF50; /* Green border */
|
| 92 |
+
padding: 15px 32px;
|
| 93 |
+
text-align: center;
|
| 94 |
+
text-decoration: none;
|
| 95 |
+
display: inline-block;
|
| 96 |
+
font-size: 18px;
|
| 97 |
+
margin-top: 50px;
|
| 98 |
+
cursor: pointer;
|
| 99 |
+
border-radius: 4px;
|
| 100 |
+
width: auto; /* Auto width based on content */
|
| 101 |
+
max-width: 400px; /* Max width for button */
|
| 102 |
+
transition: background-color 0.3s, border-color 0.3s;
|
| 103 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
| 104 |
+
white-space: nowrap; /* Prevent text from wrapping */
|
| 105 |
+
overflow: hidden; /* Hide overflow */
|
| 106 |
+
}}
|
| 107 |
+
.button:hover {{
|
| 108 |
+
background-color: #45a049; /* Darker green */
|
| 109 |
+
border-color: #45a049; /* Darker green border */
|
| 110 |
+
}}
|
| 111 |
+
.contribution {{
|
| 112 |
+
font-size: 20px;
|
| 113 |
+
color: white;
|
| 114 |
+
text-shadow: 1px 1px 3px black;
|
| 115 |
+
margin-top: 50px;
|
| 116 |
+
text-align: center;
|
| 117 |
+
}}
|
| 118 |
+
.contributors {{
|
| 119 |
+
display: flex;
|
| 120 |
+
justify-content: center;
|
| 121 |
+
align-items: center;
|
| 122 |
+
gap: 20px;
|
| 123 |
+
flex-wrap: nowrap; /* Keep items in one line */
|
| 124 |
+
overflow-x: auto; /* Allow horizontal scrolling if needed */
|
| 125 |
+
margin-top: 20px;
|
| 126 |
+
padding: 10px;
|
| 127 |
+
}}
|
| 128 |
+
.contributor {{
|
| 129 |
+
display: flex;
|
| 130 |
+
flex-direction: column;
|
| 131 |
+
align-items: center;
|
| 132 |
+
text-align: center;
|
| 133 |
+
flex-shrink: 0; /* Prevent shrinking */
|
| 134 |
+
width: 150px; /* Set a fixed width for each contributor */
|
| 135 |
+
}}
|
| 136 |
+
.contributor img {{
|
| 137 |
+
border-radius: 50%;
|
| 138 |
+
margin-bottom: 5px;
|
| 139 |
+
}}
|
| 140 |
+
.contributor-number {{
|
| 141 |
+
font-size: 18px;
|
| 142 |
+
font-weight: bold;
|
| 143 |
+
color: white;
|
| 144 |
+
margin-bottom: 5px;
|
| 145 |
+
}}
|
| 146 |
+
</style>
|
| 147 |
+
""",
|
| 148 |
+
unsafe_allow_html=True
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
# Initialize Groq client
|
| 152 |
+
def initialize_groq_client(api_key):
|
| 153 |
+
return Groq(api_key=api_key)
|
| 154 |
+
|
| 155 |
+
# Session state to manage page navigation
|
| 156 |
+
if "page" not in st.session_state:
|
| 157 |
+
st.session_state.page = "home"
|
| 158 |
+
if "conversation_history" not in st.session_state:
|
| 159 |
+
st.session_state.conversation_history = []
|
| 160 |
+
|
| 161 |
+
# Define the home page
|
| 162 |
+
def home_page():
|
| 163 |
+
set_styles(is_chatbot_page=False)
|
| 164 |
+
|
| 165 |
+
st.markdown('<div class="title">π Geography Knowledge Chatbot π</div>', unsafe_allow_html=True)
|
| 166 |
+
st.markdown(
|
| 167 |
+
'<div class="summary">'
|
| 168 |
+
'Welcome to the Geography Knowledge Chatbot! This app uses Groq AI to answer your geography-related questions. '
|
| 169 |
+
'Explore the bot and get insights into geographical facts.'
|
| 170 |
+
'</div>', unsafe_allow_html=True
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
# Define member names and image URLs
|
| 174 |
+
members = [
|
| 175 |
+
{"name": "Saad Asghar Ali - ID: 978", "image": 'assets/saad.jpg'},
|
| 176 |
+
{"name": "Hamesh Raj - ID: 2592", "image": 'assets/raj.jpg'},
|
| 177 |
+
{"name": "Mir khalil ur Rehman - 1145", "image": "https://via.placeholder.com/100"}
|
| 178 |
+
]
|
| 179 |
+
|
| 180 |
+
st.markdown('<div class="contribution">Meet the Team:</div>', unsafe_allow_html=True)
|
| 181 |
+
# Create a row with columns
|
| 182 |
+
cols = st.columns(len(members))
|
| 183 |
+
for col, member in zip(cols, members):
|
| 184 |
+
with col:
|
| 185 |
+
st.image(member["image"], width=100)
|
| 186 |
+
st.write(member["name"])
|
| 187 |
+
|
| 188 |
+
# Suggestion with arrow sign above the button
|
| 189 |
+
st.markdown(
|
| 190 |
+
"""
|
| 191 |
+
<div style="text-align: center; margin-bottom: 20px;">
|
| 192 |
+
<div style="font-size: 18px; color: #2196F3;">Click below to go to the Chatbot Page</div>
|
| 193 |
+
<span style="font-size: 24px; color: #2196F3;">π</span>
|
| 194 |
+
</div>
|
| 195 |
+
""",
|
| 196 |
+
unsafe_allow_html=True
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
# Button to navigate to the chatbot page
|
| 200 |
+
if st.button("Go to Chatbot Page", key="chatbot_button", use_container_width=True):
|
| 201 |
+
st.session_state.page = "chatbot"
|
| 202 |
+
|
| 203 |
+
# Define the chatbot page
|
| 204 |
+
def chatbot_page():
|
| 205 |
+
set_styles(is_chatbot_page=True)
|
| 206 |
+
|
| 207 |
+
# Get API key from environment variables
|
| 208 |
+
api_key = st.secrets["GROQ_API_KEY"] # Ensure this is set in .streamlit/secrets.toml
|
| 209 |
+
client = initialize_groq_client(api_key)
|
| 210 |
+
|
| 211 |
+
st.markdown('<div class="chatbot-title">π£οΈ Geography Chatbot π£οΈ</div>', unsafe_allow_html=True)
|
| 212 |
+
|
| 213 |
+
# Display conversation history
|
| 214 |
+
if st.session_state.conversation_history:
|
| 215 |
+
for entry in st.session_state.conversation_history:
|
| 216 |
+
st.write(f"**{entry['role'].capitalize()}:** {entry['content']}")
|
| 217 |
+
|
| 218 |
+
# Input field
|
| 219 |
+
user_input = st.text_input("Ask a geography question:", "")
|
| 220 |
+
|
| 221 |
+
# Output area
|
| 222 |
+
if st.button("Submit"):
|
| 223 |
+
if user_input:
|
| 224 |
+
try:
|
| 225 |
+
response = client.chat.completions.create(
|
| 226 |
+
messages=[
|
| 227 |
+
{"role": "user", "content": user_input}
|
| 228 |
+
],
|
| 229 |
+
model="gemma-7b-it",
|
| 230 |
+
max_tokens=150, # Increase the number of tokens for a more detailed response
|
| 231 |
+
temperature=0.7, # Adjust temperature for better coherence
|
| 232 |
+
)
|
| 233 |
+
answer = response.choices[0].message.content
|
| 234 |
+
st.session_state.conversation_history.append({"role": "user", "content": user_input})
|
| 235 |
+
st.session_state.conversation_history.append({"role": "bot", "content": answer})
|
| 236 |
+
st.write(f"**Bot:** {answer}")
|
| 237 |
+
except Exception as e:
|
| 238 |
+
st.error(f"An error occurred: {e}")
|
| 239 |
+
else:
|
| 240 |
+
st.warning("Please enter a question before submitting.")
|
| 241 |
+
|
| 242 |
+
# Page navigation logic
|
| 243 |
+
if st.session_state.page == "home":
|
| 244 |
+
home_page()
|
| 245 |
+
elif st.session_state.page == "chatbot":
|
| 246 |
+
chatbot_page()
|