|
|
import os |
|
|
import sys |
|
|
import streamlit as st |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
sys.path.append(str(Path(__file__).parent.parent)) |
|
|
|
|
|
import psutil |
|
|
from textwrap import dedent |
|
|
|
|
|
from src.rag_engine import SatelliteRAG |
|
|
from src.config import settings |
|
|
|
|
|
def check_system_health(): |
|
|
"""Check memory availability before processing.""" |
|
|
process = psutil.Process(os.getpid()) |
|
|
mem_info = process.memory_info() |
|
|
mem_mb = mem_info.rss / 1024 / 1024 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
total_mem = psutil.virtual_memory().total / 1024 / 1024 |
|
|
avail_mem = psutil.virtual_memory().available / 1024 / 1024 |
|
|
|
|
|
return { |
|
|
"used_mb": mem_mb, |
|
|
"available_mb": avail_mem, |
|
|
"warning": avail_mem < 250 |
|
|
} |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Space Satellite Assistant", page_icon="🛰️") |
|
|
st.title("🛰️ Space Satellite Assistant") |
|
|
|
|
|
|
|
|
with st.sidebar: |
|
|
st.markdown("### Configuration") |
|
|
if not settings.GROQ_API_KEY: |
|
|
st.error("⚠️ GROQ_API_KEY not found in .env!") |
|
|
else: |
|
|
st.success("System Online") |
|
|
|
|
|
st.markdown("---") |
|
|
st.markdown("**Engine:** SatelliteRAG v1.0") |
|
|
st.markdown("**Knowledge Base:** 637 Satellites") |
|
|
|
|
|
|
|
|
@st.cache_resource |
|
|
def get_engine(): |
|
|
try: |
|
|
with st.spinner("Initializing RAG Engine (Loading Models & DB)..."): |
|
|
return SatelliteRAG() |
|
|
except Exception as e: |
|
|
|
|
|
return f"ERROR: {str(e)}" |
|
|
|
|
|
|
|
|
engine_status = get_engine() |
|
|
|
|
|
with st.sidebar: |
|
|
st.markdown("---") |
|
|
st.markdown("### System Status") |
|
|
if isinstance(engine_status, str) and "ERROR" in engine_status: |
|
|
st.error(f"🔴 Engine: {engine_status}") |
|
|
elif engine_status is None: |
|
|
st.warning("🟠 Engine: Initializing...") |
|
|
else: |
|
|
st.success("🟢 Engine: Ready") |
|
|
|
|
|
|
|
|
if "messages" not in st.session_state: |
|
|
st.session_state.messages = [] |
|
|
|
|
|
|
|
|
for message in st.session_state.messages: |
|
|
with st.chat_message(message["role"]): |
|
|
st.markdown(message["content"]) |
|
|
|
|
|
|
|
|
if prompt := st.chat_input("Ask about any satellite (e.g., 'What is Gaofen 1?')..."): |
|
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
with st.chat_message("user"): |
|
|
st.markdown(prompt) |
|
|
|
|
|
with st.chat_message("assistant"): |
|
|
|
|
|
engine = engine_status |
|
|
|
|
|
if isinstance(engine, SatelliteRAG): |
|
|
with st.spinner("Analyzing satellite data..."): |
|
|
try: |
|
|
|
|
|
health = check_system_health() |
|
|
if health["warning"]: |
|
|
st.warning(f"⚠️ Low Memory Warning: Only {health['available_mb']:.0f}MB available. Query might be slow.") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chat_history = [] |
|
|
msgs = st.session_state.messages[:-1] |
|
|
for i in range(0, len(msgs) - 1, 2): |
|
|
if msgs[i]["role"] == "user" and msgs[i+1]["role"] == "assistant": |
|
|
chat_history.append((msgs[i]["content"], msgs[i+1]["content"])) |
|
|
|
|
|
response, docs = engine.query(prompt, chat_history=chat_history) |
|
|
|
|
|
st.markdown(response) |
|
|
|
|
|
|
|
|
with st.expander(f"View Source Context ({len(docs)} chunks)"): |
|
|
for i, doc in enumerate(docs, 1): |
|
|
st.markdown(f"**Source {i}:** {doc.metadata.get('name')}") |
|
|
st.text(doc.page_content[:300] + "...") |
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": response}) |
|
|
except Exception as e: |
|
|
st.error("⚠️ An error occurred during analysis.") |
|
|
st.markdown(f"**Error Details:** `{str(e)}`") |
|
|
st.info("💡 **Tip:** If this happens frequently, the system might be out of memory (OOM). Try waiting a few seconds and refreshing.") |
|
|
else: |
|
|
st.error("⚠️ RAG Engine is not ready. Please check the sidebar for status.") |
|
|
|