Spaces:
Build error
Build error
Shubham Pawar
Claude Sonnet 5
fix: make ingestion and querying robust; add acceptance-test corpus
c8f7f2e Download src/frontend/app.py from AIenthusSP/RAG-Documind-AI: direct link, hf CLI and curl.
- Browser
- Download file 17.3 kB
-
https://huggingface.co/spaces/AIenthusSP/RAG-Documind-AI/resolve/main/src/frontend/app.py
- Command line
-
hf download hf://spaces/AIenthusSP/RAG-Documind-AI/src/frontend/app.py
-
curl -L -o app.py https://huggingface.co/spaces/AIenthusSP/RAG-Documind-AI/resolve/main/src/frontend/app.py
17.3 kB
| """ | |
| Streamlit Frontend Application | |
| DocuMind AI - RAG-Powered Documentation Assistant | |
| Chat interface for querying documents. | |
| """ | |
| import streamlit as st | |
| import requests | |
| from typing import Optional | |
| import os | |
| # Page configuration | |
| st.set_page_config( | |
| page_title="DocuMind AI", | |
| page_icon="π§ ", | |
| layout="wide", | |
| initial_sidebar_state="expanded", | |
| ) | |
| # API configuration - use localhost for HF Spaces (both run in same container) | |
| API_BASE_URL = os.getenv("API_BASE_URL", "http://localhost:8000") | |
| # ============== Custom CSS ============== | |
| # NOTE: the app is pinned to the dark theme (.streamlit/config.toml). Any custom | |
| # element needs BOTH background and text colour set explicitly - a bare element | |
| # inherits the theme's near-white text, which was invisible on the old | |
| # near-white source cards ("shows white only"). Source snippets now use native | |
| # st.container components (see render_sources) instead of injected HTML. | |
| st.markdown(""" | |
| <style> | |
| .main { padding: 1rem 2rem; } | |
| /* Header banner (the only injected-HTML element left) */ | |
| .header-container { | |
| background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); | |
| padding: 2rem; | |
| border-radius: 15px; | |
| margin-bottom: 2rem; | |
| color: white; | |
| text-align: center; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # ============== Session State ============== | |
| # Authentication state | |
| if "authenticated" not in st.session_state: | |
| st.session_state.authenticated = False | |
| if "user_api_key" not in st.session_state: | |
| st.session_state.user_api_key = "" | |
| if "user_id" not in st.session_state: | |
| st.session_state.user_id = "" # Hash of API key for namespacing | |
| if "user_provider" not in st.session_state: | |
| st.session_state.user_provider = "gemini" # Default provider | |
| # Provider configurations | |
| PROVIDERS = { | |
| "gemini": { | |
| "name": "Gemini", | |
| "icon": "π·", | |
| "tier": "Free", | |
| "url": "https://aistudio.google.com/", | |
| "prefix": "AIza", | |
| "model": "gemini-2.5-flash", | |
| }, | |
| "openai": { | |
| "name": "OpenAI", | |
| "icon": "π’", | |
| "tier": "Paid", | |
| "url": "https://platform.openai.com/api-keys", | |
| "prefix": "sk-", | |
| "model": "gpt-4o-mini", | |
| }, | |
| "anthropic": { | |
| "name": "Claude", | |
| "icon": "π ", | |
| "tier": "Paid", | |
| "url": "https://console.anthropic.com/", | |
| "prefix": "sk-ant", | |
| "model": "claude-3-haiku-20240307", | |
| }, | |
| "groq": { | |
| "name": "Groq", | |
| "icon": "β‘", | |
| "tier": "Free", | |
| "url": "https://console.groq.com/keys", | |
| "prefix": "gsk_", | |
| "model": "llama-3.3-70b-versatile", | |
| }, | |
| } | |
| # Chat history per collection (key: collection_name, value: list of messages) | |
| if "chats" not in st.session_state: | |
| st.session_state.chats = {} | |
| if "collection_name" not in st.session_state: | |
| st.session_state.collection_name = "default" | |
| # Track when collections were last updated (for context refresh notification) | |
| if "collection_updated" not in st.session_state: | |
| st.session_state.collection_updated = {} | |
| # User's collections list | |
| if "user_collections" not in st.session_state: | |
| st.session_state.user_collections = ["default"] | |
| def get_current_messages(): | |
| """Get messages for the current collection.""" | |
| collection = st.session_state.collection_name | |
| if collection not in st.session_state.chats: | |
| st.session_state.chats[collection] = [] | |
| return st.session_state.chats[collection] | |
| def add_message(role: str, content: str, sources: list = None): | |
| """Add a message to the current collection's chat.""" | |
| collection = st.session_state.collection_name | |
| if collection not in st.session_state.chats: | |
| st.session_state.chats[collection] = [] | |
| message = {"role": role, "content": content} | |
| if sources: | |
| message["sources"] = sources | |
| st.session_state.chats[collection].append(message) | |
| def render_sources(sources: list): | |
| """Render retrieved source snippets inside an expander. | |
| Uses Streamlit-native containers rather than raw HTML: indented HTML passed | |
| to st.markdown gets parsed as a code block, and a bare <div> inherits the | |
| dark theme's near-white text colour onto a near-white card (invisible). | |
| """ | |
| if not sources: | |
| return | |
| with st.expander(f"π View Sources ({len(sources)})", expanded=False): | |
| for i, source in enumerate(sources, 1): | |
| score = source.get("score") | |
| label = f"**Source {i}**" | |
| if isinstance(score, (int, float)): | |
| label += f" Β· relevance {score:.2f}" | |
| meta = source.get("metadata") or {} | |
| name = meta.get("file_name") or meta.get("source") | |
| if name: | |
| label += f" Β· `{name}`" | |
| with st.container(border=True): | |
| st.markdown(label) | |
| st.caption((source.get("content", "") or "").strip()[:300] + "β¦") | |
| def clear_current_chat(): | |
| """Clear chat for current collection.""" | |
| collection = st.session_state.collection_name | |
| st.session_state.chats[collection] = [] | |
| def mark_collection_updated(collection_name: str): | |
| """Mark a collection as recently updated with new documents.""" | |
| import time | |
| st.session_state.collection_updated[collection_name] = time.time() | |
| # ============== Helper Functions ============== | |
| def get_namespaced_collection(collection: str) -> str: | |
| """Get collection name prefixed with user ID for isolation.""" | |
| return f"{st.session_state.user_id}_{collection}" | |
| def query_api(question: str, collection: str, top_k: int, api_key: str = None, provider: str = "gemini") -> Optional[dict]: | |
| """Query the RAG API.""" | |
| try: | |
| payload = { | |
| "question": question, | |
| "collection_name": collection, | |
| "top_k": top_k, | |
| "include_sources": True, | |
| "api_key": api_key, | |
| "provider": provider, | |
| } | |
| response = requests.post( | |
| f"{API_BASE_URL}/api/query", | |
| json=payload, | |
| timeout=60, | |
| ) | |
| response.raise_for_status() | |
| return response.json() | |
| except requests.exceptions.ConnectionError: | |
| st.error("β Cannot connect to API. Make sure the backend is running.") | |
| return None | |
| except Exception as e: | |
| st.error(f"β Error: {str(e)}") | |
| return None | |
| def ingest_files(files, collection: str) -> Optional[dict]: | |
| """Upload files to the API for ingestion.""" | |
| try: | |
| # Map extensions to MIME types (browsers often report wrong/empty types) | |
| mime_types = { | |
| ".pdf": "application/pdf", | |
| ".md": "text/markdown", | |
| ".markdown": "text/markdown", | |
| ".txt": "text/plain", | |
| } | |
| files_data = [] | |
| for f in files: | |
| ext = "." + f.name.split(".")[-1].lower() if "." in f.name else "" | |
| content_type = mime_types.get(ext, f.type or "application/octet-stream") | |
| files_data.append(("files", (f.name, f.getvalue(), content_type))) | |
| response = requests.post( | |
| f"{API_BASE_URL}/api/ingest", | |
| files=files_data, | |
| data={"collection_name": collection}, | |
| timeout=120, | |
| ) | |
| response.raise_for_status() | |
| return response.json() | |
| except requests.exceptions.ConnectionError: | |
| st.error("β Cannot connect to API. Make sure the backend is running.") | |
| return None | |
| except Exception as e: | |
| st.error(f"β Upload failed: {str(e)}") | |
| return None | |
| def check_api_health() -> bool: | |
| """Check if the API is available.""" | |
| try: | |
| response = requests.get(f"{API_BASE_URL}/health", timeout=5) | |
| return response.status_code == 200 | |
| except Exception: | |
| return False | |
| def validate_api_key(api_key: str, provider: str) -> bool: | |
| """Basic format check based on provider.""" | |
| if provider not in PROVIDERS: | |
| return False | |
| prefix = PROVIDERS[provider]["prefix"] | |
| return api_key.startswith(prefix) and len(api_key) > 20 | |
| def get_user_id(api_key: str) -> str: | |
| """Generate user ID from API key hash (first 12 chars of SHA256).""" | |
| import hashlib | |
| return hashlib.sha256(api_key.encode()).hexdigest()[:12] | |
| def show_login_page(): | |
| """Display the API key entry page with provider cards.""" | |
| st.markdown(""" | |
| <div style=" | |
| display: flex; | |
| flex-direction: column; | |
| align-items: center; | |
| justify-content: center; | |
| min-height: 30vh; | |
| text-align: center; | |
| "> | |
| <h1 style=" | |
| background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); | |
| -webkit-background-clip: text; | |
| -webkit-text-fill-color: transparent; | |
| font-size: 3rem; | |
| margin-bottom: 0.5rem; | |
| ">π§ DocuMind AI</h1> | |
| <p style="color: #666; font-size: 1.2rem; margin-bottom: 1rem;"> | |
| RAG-Powered Document Intelligence | |
| </p> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| # Provider selection cards | |
| st.markdown("### Choose Your AI Provider") | |
| cols = st.columns(4) | |
| for idx, (provider_key, provider_info) in enumerate(PROVIDERS.items()): | |
| with cols[idx]: | |
| is_selected = st.session_state.user_provider == provider_key | |
| btn_type = "primary" if is_selected else "secondary" | |
| if st.button( | |
| f"{provider_info['icon']} {provider_info['name']}\n[{provider_info['tier']}]", | |
| key=f"provider_{provider_key}", | |
| use_container_width=True, | |
| type=btn_type, | |
| ): | |
| st.session_state.user_provider = provider_key | |
| st.rerun() | |
| st.markdown( | |
| f"<a href='{provider_info['url']}' target='_blank' style='font-size: 0.8rem;'>Get Key β</a>", | |
| unsafe_allow_html=True | |
| ) | |
| st.markdown("---") | |
| # API Key input | |
| selected = PROVIDERS[st.session_state.user_provider] | |
| st.markdown(f"### π Enter Your {selected['name']} API Key") | |
| api_key = st.text_input( | |
| "API Key", | |
| type="password", | |
| placeholder=f"{selected['prefix']}...", | |
| label_visibility="collapsed" | |
| ) | |
| if st.button("π Get Started", type="primary", use_container_width=True): | |
| if not api_key: | |
| st.error("Please enter an API key") | |
| else: | |
| if validate_api_key(api_key, st.session_state.user_provider): | |
| st.session_state.authenticated = True | |
| st.session_state.user_api_key = api_key | |
| st.session_state.user_id = get_user_id(api_key) | |
| st.success("β Ready to go!") | |
| st.rerun() | |
| else: | |
| st.error(f"β Invalid key format. {selected['name']} keys start with '{selected['prefix']}'") | |
| st.markdown("---") | |
| st.caption("Your API key stays in your browser session only. Never stored on servers.") | |
| # ============== Authentication Gate ============== | |
| if not st.session_state.authenticated: | |
| show_login_page() | |
| st.stop() | |
| # ============== Sidebar ============== | |
| with st.sidebar: | |
| st.markdown("## βοΈ Settings") | |
| # User info and logout | |
| st.caption(f"π Session: `{st.session_state.user_id}`") | |
| if st.button("πͺ Logout", use_container_width=True): | |
| st.session_state.authenticated = False | |
| st.session_state.user_api_key = "" | |
| st.session_state.user_id = "" | |
| st.session_state.chats = {} | |
| st.session_state.user_collections = ["default"] | |
| st.rerun() | |
| st.divider() | |
| # API Status | |
| api_healthy = check_api_health() | |
| if api_healthy: | |
| st.success("β API Connected") | |
| else: | |
| st.error("β API Offline") | |
| st.divider() | |
| # Collections list | |
| st.markdown("### π Collections") | |
| for col_name in st.session_state.user_collections: | |
| col1, col2 = st.columns([4, 1]) | |
| with col1: | |
| is_active = col_name == st.session_state.collection_name | |
| btn_type = "primary" if is_active else "secondary" | |
| if st.button( | |
| f"{'π' if is_active else 'π'} {col_name}", | |
| key=f"col_{col_name}", | |
| use_container_width=True, | |
| type=btn_type | |
| ): | |
| st.session_state.collection_name = col_name | |
| st.rerun() | |
| with col2: | |
| if len(st.session_state.user_collections) > 1: # Keep at least one | |
| if st.button("ποΈ", key=f"del_{col_name}"): | |
| # Delete collection from backend | |
| try: | |
| namespaced = f"{st.session_state.user_id}_{col_name}" | |
| requests.delete(f"{API_BASE_URL}/api/ingest/{namespaced}", timeout=10) | |
| except Exception: | |
| pass | |
| # Remove from local state | |
| st.session_state.user_collections.remove(col_name) | |
| if col_name in st.session_state.chats: | |
| del st.session_state.chats[col_name] | |
| if st.session_state.collection_name == col_name: | |
| st.session_state.collection_name = st.session_state.user_collections[0] | |
| st.rerun() | |
| # New collection input | |
| new_col = st.text_input("New collection", placeholder="Enter name...") | |
| if st.button("β Create", use_container_width=True): | |
| if new_col and new_col not in st.session_state.user_collections: | |
| st.session_state.user_collections.append(new_col) | |
| st.session_state.collection_name = new_col | |
| st.rerun() | |
| st.divider() | |
| # Retrieval settings | |
| st.markdown("### π Retrieval") | |
| top_k = st.slider( | |
| "Number of sources", | |
| min_value=1, | |
| max_value=10, | |
| value=5, | |
| help="Number of document chunks to retrieve" | |
| ) | |
| st.divider() | |
| # File upload | |
| st.markdown("### π€ Upload Documents") | |
| uploaded_files = st.file_uploader( | |
| "Drop files here", | |
| type=["pdf", "md", "txt"], | |
| accept_multiple_files=True, | |
| help="Supported: PDF, Markdown, Text" | |
| ) | |
| if uploaded_files: | |
| if st.button("π₯ Ingest Files", type="primary", use_container_width=True): | |
| with st.spinner("Processing files..."): | |
| result = ingest_files(uploaded_files, get_namespaced_collection(st.session_state.collection_name)) | |
| if result and result.get("success"): | |
| st.success( | |
| f"β Ingested {result['documents_processed']} docs, " | |
| f"{result['chunks_created']} chunks" | |
| ) | |
| # Mark collection as updated for context notification | |
| mark_collection_updated(st.session_state.collection_name) | |
| st.info("π‘ Your chat context has been updated with new documents.") | |
| st.balloons() | |
| st.divider() | |
| # Clear chat | |
| if st.button("ποΈ Clear Chat", use_container_width=True): | |
| clear_current_chat() | |
| st.rerun() | |
| # ============== Main Content ============== | |
| # Header | |
| st.markdown(""" | |
| <div class="header-container"> | |
| <h1>π§ DocuMind AI</h1> | |
| <p>RAG-Powered Documentation Assistant</p> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| # Chat container | |
| chat_container = st.container() | |
| # Display chat messages | |
| with chat_container: | |
| for message in get_current_messages(): | |
| with st.chat_message(message["role"]): | |
| st.markdown(message["content"]) | |
| # Show sources for assistant messages | |
| if message["role"] == "assistant" and message.get("sources"): | |
| render_sources(message["sources"]) | |
| # Chat input | |
| if prompt := st.chat_input("Ask a question about your documents..."): | |
| # Add user message | |
| add_message("user", prompt) | |
| with st.chat_message("user"): | |
| st.markdown(prompt) | |
| # Get response | |
| with st.chat_message("assistant"): | |
| with st.spinner("Thinking..."): | |
| result = query_api( | |
| question=prompt, | |
| collection=get_namespaced_collection(st.session_state.collection_name), | |
| top_k=top_k, | |
| api_key=st.session_state.user_api_key, | |
| provider=st.session_state.user_provider, | |
| ) | |
| if result: | |
| answer = result.get("answer", "Sorry, I couldn't generate a response.") | |
| sources = result.get("sources", []) | |
| st.markdown(answer) | |
| # Store message with sources | |
| add_message("assistant", answer, sources) | |
| # Show sources | |
| render_sources(sources) | |
| else: | |
| error_msg = "Failed to get a response. Please check the API connection." | |
| st.error(error_msg) | |
| add_message("assistant", error_msg) | |
| # Footer | |
| st.markdown("---") | |
| st.caption( | |
| "Built with β€οΈ using LangChain, ChromaDB, and Google Gemini | " | |
| f"Collection: **{st.session_state.collection_name}**" | |
| ) | |