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(
"""
""",
unsafe_allow_html=True
)
else:
st.markdown(
f"""
""",
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('
🌍 Geography Knowledge Chatbot 🌍
', unsafe_allow_html=True)
st.markdown(
''
'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.'
'
', 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('Meet the Team:
', 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(
"""
Click below to go to the Chatbot Page
👉
""",
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('🗣️ Geography Chatbot 🗣️
', 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()