import os import streamlit as st import google.generativeai as genai import datetime import time import base64 # -------------------- CONFIG -------------------- from api_key import api_key genai.configure(api_key=api_key) SYSTEM_PROMPT_DETAILED = """ You are Sami-CLearn Hub Assistant your name is Sami and the website you are assistant with its name C learn-hub — a friendly assistant that helps students choose the best learning track ( AI, Data Science, Web Development, Mobile Development, Cybersecurity, etc ). Your job: - Answer questions in a clear, helpful and friendly tone. - Provide roadmaps, steps, tools, technologies. - Explain code simply. When giving code, use Markdown fenced code blocks. - Help with career paths, learning methods, and problem-solving. - If the user asks about Sami-CLearn Hub website features, explain them. Keep answers helpful, structured, and correct. """ SYSTEM_PROMPT_SHORT = """ You are Sami-CLearn Hub Assistant — a helpful assistant that guides students in choosing tech tracks and learning paths gives short and clear answer for the specific thing that user ask for. """ SYSTEM_PROMPT = SYSTEM_PROMPT_DETAILED model = genai.GenerativeModel( model_name="models/gemini-2.5-flash", system_instruction=SYSTEM_PROMPT ) # -------------------- STREAMLIT UI -------------------- st.set_page_config(page_title="Sami-CLearn Hub Assistant", page_icon='robot.png', layout="wide", initial_sidebar_state="expanded") # -------------------- IMPORT GOOGLE FONT -------------------- st.markdown(""" """, unsafe_allow_html=True) # -------------------- IMAGE LOADER -------------------- def load_base64_image(image_path): with open(image_path, "rb") as img: return base64.b64encode(img.read()).decode() setting_icon = load_base64_image("setting.png") clear_icon = load_base64_image("clear.png") export_icon = load_base64_image("export.png") # -------------------- CUSTOM CSS -------------------- st.markdown( f""" """, unsafe_allow_html=True ) # -------------------- TITLE -------------------- col1, col2 = st.columns([0.12, 0.88]) with col1: if os.path.exists("robot.png"): st.image("robot.png", width=72) else: st.markdown("
🤖
", unsafe_allow_html=True) with col2: st.markdown("
Sami-CLearn Hub Assistant
", unsafe_allow_html=True) st.markdown("

Ask about site help, track recommendations, or courses.

", unsafe_allow_html=True) # -------------------- CHAT HISTORY -------------------- if "messages" not in st.session_state: st.session_state.messages = [] # -------------------- SIDEBAR -------------------- with st.sidebar: st.markdown( f"""

Settings

""", unsafe_allow_html=True ) use_short_prompt = st.checkbox("Use short system prompt?", value=False) if st.button("Clear Conversation", key="clear_btn"): st.session_state.messages = [] if st.button("Export Chat", key="export_btn"): if st.session_state.messages: chat_text = "" for m in st.session_state.messages: chat_text += f"{m['role'].upper()}:\n{m['content']}\n\n" filename = f"Sami_chat_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.txt" st.download_button(label="Download Chat", data=chat_text, file_name=filename, mime="text/plain") else: st.warning("No messages to export!") # -------------------- DISPLAY CHAT HISTORY -------------------- for msg in st.session_state.messages: alignment = "user" if msg["role"] == "user" else "assistant" with st.chat_message(alignment): st.markdown(msg["content"], unsafe_allow_html=True) # -------------------- CHAT INPUT -------------------- user_input = st.chat_input("Type your question...") if user_input: st.session_state.messages.append({"role": "user", "content": user_input}) with st.chat_message("user"): st.markdown(user_input, unsafe_allow_html=True) model.system_instruction = SYSTEM_PROMPT_SHORT if use_short_prompt else SYSTEM_PROMPT_DETAILED messages_for_llm = [] for m in st.session_state.messages: messages_for_llm.append({"role": "user" if m["role"]=="user" else "model", "parts": m["content"]}) response = model.generate_content(messages_for_llm) bot_response = response.text with st.chat_message("assistant"): placeholder = st.empty() displayed_text = "" for char in bot_response: displayed_text += char placeholder.markdown(displayed_text, unsafe_allow_html=True) time.sleep(0.01) st.session_state.messages.append({"role": "assistant", "content": bot_response})