Spaces:
Running
Running
| import os | |
| import sys | |
| import streamlit as st | |
| # Page Config - MUST be first Streamlit command | |
| st.set_page_config(page_title="Sagar's Personal Assistant", page_icon="π") | |
| # Add the src directory to sys.path to allow imports | |
| sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "src")) | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # Check API key | |
| api_key = os.getenv("OPENROUTER_API_KEY") | |
| if not api_key: | |
| st.error("β οΈ **OPENROUTER_API_KEY is missing!**") | |
| st.info("π Go to your HF Space **Settings β Secrets** and add `OPENROUTER_API_KEY`") | |
| st.stop() | |
| # Import RAG | |
| try: | |
| from src.rag import get_answer | |
| except Exception as e: | |
| st.error(f"β Failed to import RAG module: {e}") | |
| st.stop() | |
| # Title | |
| st.title("π Sagar's Personal Assistant") | |
| st.write("Hey there! π I'm Sagar's personal assistant! Feel free to ask me anything about Sagar. I'm here to help! π") | |
| # Initialize Chat History | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # Display Chat History | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"]): | |
| st.markdown(message["content"]) | |
| # User Input | |
| if prompt := st.chat_input("What would you like to know?"): | |
| # Add user message to history | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| with st.chat_message("user"): | |
| st.markdown(prompt) | |
| # Generate Response | |
| with st.chat_message("assistant"): | |
| # Create a single placeholder for status updates | |
| status_placeholder = st.empty() | |
| try: | |
| response = get_answer(prompt, status_placeholder) | |
| if response: | |
| st.markdown(response) | |
| st.session_state.messages.append({"role": "assistant", "content": response}) | |
| else: | |
| st.error("β Received empty response from AI.") | |
| except Exception as e: | |
| status_placeholder.empty() | |
| st.error(f"β Error: {str(e)}") | |
| status_placeholder.empty() | |
| st.error(f"β Error: {str(e)}") | |