My_Self_Bot / app.py
BOB0920's picture
Update app.py
9c0ae5e verified
Raw
History Blame Contribute Delete
2.1 kB
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)}")