# app/app.py import sys, os # Ensure root and submodules are importable CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) ROOT_DIR = os.path.abspath(os.path.join(CURRENT_DIR, "..")) if ROOT_DIR not in sys.path: sys.path.insert(0, ROOT_DIR) from pathlib import Path import streamlit as st from apps.rag_pipeline import rag_pipeline from apps.core.config import UPLOAD_DIR, APP_NAME, DESCRIPTION # === Ensure upload directory exists === UPLOAD_DIR.mkdir(parents=True, exist_ok=True) # === Streamlit Page Setup === st.set_page_config( page_title=APP_NAME, page_icon="📘", layout="wide", ) # === Custom CSS (includes Option 1 fix for “200MB” text) === st.markdown(""" """, unsafe_allow_html=True) # === Header === st.markdown(f"
{DESCRIPTION}
", unsafe_allow_html=True) st.markdown("---") # === Sidebar Info === with st.sidebar: st.header("ℹ️ About This App") st.write(""" This RAG (Retrieval-Augmented Generation) demo lets you: 1️⃣ Upload and embed your document 2️⃣ Ask questions using an LLM 3️⃣ Get context-aware answers instantly """) st.markdown("---") st.caption("Built with ❤️ using Streamlit + LangChain") # === Step 1: Upload & Ingest === st.subheader("📤 Step 1: Upload & Ingest Document") uploaded_file = st.file_uploader( "📤 Upload a `.pdf`, `.txt`, or `.md` file (Max size: 50MB)", type=["pdf", "txt", "md"], help="Limit 50MB per file • Supported formats: PDF, TXT, MD", ) # ✅ Backend validation if uploaded_file: if uploaded_file.size > 50 * 1024 * 1024: st.error("❌ File too large. Please upload a document under 50 MB.") st.stop() dest_path = UPLOAD_DIR / uploaded_file.name dest_path.write_bytes(uploaded_file.read()) st.success(f"✅ File '{uploaded_file.name}' uploaded successfully!") if st.button("🚀 Ingest Document", use_container_width=True): with st.spinner("Embedding and storing document... ⏳"): try: rag_pipeline.ingest_file(dest_path) st.success(f"✅ '{uploaded_file.name}' ingested into vector DB!") except Exception as e: st.error(f"❌ Ingestion failed: {e}") # === Step 2: Ask Question === st.markdown("---") st.subheader("💬 Step 2: Ask a Question") query = st.text_area( "Ask something about your uploaded document:", placeholder="e.g. What is the main idea of this text?", height=80, ) if st.button("🧠 Get Answer", use_container_width=True): if not query.strip(): st.warning("⚠️ Please enter a question first.") else: with st.spinner("🤔 Thinking..."): try: answer = rag_pipeline.ask(query.strip()) st.markdown( f"""