Spaces:
Build error
Build error
File size: 9,093 Bytes
0c2cd8c |
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
import streamlit as st
import base64
from groq import Groq
# Function to encode the image file
def get_base64_of_bin_file(bin_file):
with open(bin_file, 'rb') as f:
data = f.read()
return base64.b64encode(data).decode() # Return after file is read
# Load the background image
background_image_path = 'assets/map.webp'
background_image = get_base64_of_bin_file(background_image_path)
# Set up common styles for both pages
def set_styles(is_chatbot_page):
if is_chatbot_page:
st.markdown(
"""
<style>
.stApp {
background: none;
height: 100vh;
color: black;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 2rem;
}
.chatbot-title {
font-size: 36px;
font-weight: bold;
margin-bottom: 20px;
}
.button-container {
display: flex;
justify-content: center;
width: 100%;
}
.button {
background-color: #2196F3; /* Blue background */
color: white; /* White text */
border: 2px solid #2196F3; /* Blue border */
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 18px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
width: auto; /* Auto width based on content */
max-width: 400px; /* Max width for button */
transition: background-color 0.3s, border-color 0.3s;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
white-space: nowrap; /* Prevent text from wrapping */
overflow: hidden; /* Hide overflow */
}
.button:hover {
background-color: #1976D2; /* Darker blue */
border-color: #1976D2; /* Darker blue border */
}
</style>
""",
unsafe_allow_html=True
)
else:
st.markdown(
f"""
<style>
.stApp {{
background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url("data:image/webp;base64,{background_image}");
background-size: cover;
background-position: center;
background-attachment: fixed;
height: 100vh;
padding: 2rem;
color: white;
}}
.title {{
font-size: 48px;
font-weight: bold;
text-shadow: 2px 2px 5px black;
margin-top: 30px;
text-align: center;
}}
.button {{
background-color: #4CAF50; /* Green background */
color: white; /* White text */
border: 2px solid #4CAF50; /* Green border */
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 18px;
margin-top: 50px;
cursor: pointer;
border-radius: 4px;
width: auto; /* Auto width based on content */
max-width: 400px; /* Max width for button */
transition: background-color 0.3s, border-color 0.3s;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
white-space: nowrap; /* Prevent text from wrapping */
overflow: hidden; /* Hide overflow */
}}
.button:hover {{
background-color: #45a049; /* Darker green */
border-color: #45a049; /* Darker green border */
}}
.contribution {{
font-size: 20px;
color: white;
text-shadow: 1px 1px 3px black;
margin-top: 50px;
text-align: center;
}}
.contributors {{
display: flex;
justify-content: center;
align-items: center;
gap: 20px;
flex-wrap: nowrap; /* Keep items in one line */
overflow-x: auto; /* Allow horizontal scrolling if needed */
margin-top: 20px;
padding: 10px;
}}
.contributor {{
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
flex-shrink: 0; /* Prevent shrinking */
width: 150px; /* Set a fixed width for each contributor */
}}
.contributor img {{
border-radius: 50%;
margin-bottom: 5px;
}}
.contributor-number {{
font-size: 18px;
font-weight: bold;
color: white;
margin-bottom: 5px;
}}
</style>
""",
unsafe_allow_html=True
)
# Initialize Groq client
def initialize_groq_client(api_key):
return Groq(api_key=api_key)
# Session state to manage page navigation
if "page" not in st.session_state:
st.session_state.page = "home"
if "conversation_history" not in st.session_state:
st.session_state.conversation_history = []
# Define the home page
def home_page():
set_styles(is_chatbot_page=False)
st.markdown('<div class="title">π Geography Knowledge Chatbot π</div>', unsafe_allow_html=True)
st.markdown(
'<div class="summary">'
'Welcome to the Geography Knowledge Chatbot! This app uses Groq AI to answer your geography-related questions. '
'Explore the bot and get insights into geographical facts.'
'</div>', unsafe_allow_html=True
)
# Define member names and image URLs
members = [
{"name": "Saad Asghar Ali - ID: 978", "image": 'assets/saad.jpg'},
{"name": "Hamesh Raj - ID: 2592", "image": 'assets/raj.jpg'},
{"name": "Mir khalil ur Rehman - 1145", "image": "https://via.placeholder.com/100"}
]
st.markdown('<div class="contribution">Meet the Team:</div>', unsafe_allow_html=True)
# Create a row with columns
cols = st.columns(len(members))
for col, member in zip(cols, members):
with col:
st.image(member["image"], width=100)
st.write(member["name"])
# Suggestion with arrow sign above the button
st.markdown(
"""
<div style="text-align: center; margin-bottom: 20px;">
<div style="font-size: 18px; color: #2196F3;">Click below to go to the Chatbot Page</div>
<span style="font-size: 24px; color: #2196F3;">π</span>
</div>
""",
unsafe_allow_html=True
)
# Button to navigate to the chatbot page
if st.button("Go to Chatbot Page", key="chatbot_button", use_container_width=True):
st.session_state.page = "chatbot"
# Define the chatbot page
def chatbot_page():
set_styles(is_chatbot_page=True)
# Get API key from environment variables
api_key = st.secrets["GROQ_API_KEY"] # Ensure this is set in .streamlit/secrets.toml
client = initialize_groq_client(api_key)
st.markdown('<div class="chatbot-title">π£οΈ Geography Chatbot π£οΈ</div>', unsafe_allow_html=True)
# Display conversation history
if st.session_state.conversation_history:
for entry in st.session_state.conversation_history:
st.write(f"**{entry['role'].capitalize()}:** {entry['content']}")
# Input field
user_input = st.text_input("Ask a geography question:", "")
# Output area
if st.button("Submit"):
if user_input:
try:
response = client.chat.completions.create(
messages=[
{"role": "user", "content": user_input}
],
model="gemma-7b-it",
max_tokens=150, # Increase the number of tokens for a more detailed response
temperature=0.7, # Adjust temperature for better coherence
)
answer = response.choices[0].message.content
st.session_state.conversation_history.append({"role": "user", "content": user_input})
st.session_state.conversation_history.append({"role": "bot", "content": answer})
st.write(f"**Bot:** {answer}")
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.warning("Please enter a question before submitting.")
# Page navigation logic
if st.session_state.page == "home":
home_page()
elif st.session_state.page == "chatbot":
chatbot_page()
|