Spaces:
Sleeping
Sleeping
Update app.py
Browse files2 unmodified lines345678910111213141516171819202122232425262728293031323334353637383940414243444546474863 unmodified lines1121131141151161172 unmodified linesimport sysimport os# Add the repo root to path so we can import the /app modulessys.path.append(os.path.dirname(__file__))# Import your three RAG implementationsfrom app.rag_naive import NaiveRAGfrom app.rag_optimized import OptimizedRAGfrom app.no_compromise_rag import NoCompromiseRAG# -------------------------------------------------------------------# Initialize the three RAG systems once at startup.# If memory becomes an issue, we can lazy‑load them inside each function.# -------------------------------------------------------------------print("Initializing Naive RAG...")naive_rag = NaiveRAG() # loads embedding model + FAISS indexprint("Initializing Optimized RAG...")optimized_rag = OptimizedRAG() # loads the same + SQLite cacheprint("Initializing No‑Compromise RAG...")no_compromise_rag = NoCompromiseRAG()print("All RAG systems ready.")# -------------------------------------------------------------------# Define the query functions for each mode# -------------------------------------------------------------------def query_naive(question):start = time.perf_counter()answer, chunks_used, cache_hit = naive_rag.query(question)latency = (time.perf_counter() - start) * 1000return answer, f"{latency:.1f} ms", chunks_used, "Yes" if cache_hit else "No"def query_optimized(question):start = time.perf_counter()answer, chunks_used, cache_hit = optimized_rag.query(question)latency = (time.perf_counter() - start) * 1000return answer, f"{latency:.1f} ms", chunks_used, "Yes" if cache_hit else "No"def query_no_compromise(question):start = time.perf_counter()answer, chunks_used, cache_hit = no_compromise_rag.query(question)latency = (time.perf_counter() - start) * 1000return answer, f"{latency:.1f} ms", chunks_used, "Yes" if cache_hit else "No"# -------------------------------------------------------------------# Build the Gradio interface63 unmodified lines**Caching**: SQLite (Optimized) + LRU memory | **Generation**: Simulated (real LLM can be plugged in)""")# Launch the appif __name__ == "__main__":demo.launch(server_name="0.0.0.0", server_port=7860)2 unmodified lines3456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828363 unmodified lines1471481491501512 unmodified linesimport sysimport os# Add repo root to pathsys.path.append(os.path.dirname(__file__))# Global references to loaded systems_naive_rag = None_optimized_rag = None_no_compromise_rag = None_embedding_model = None # shared modeldef get_embedding_model():"""Load the embedding model once and reuse it across all RAG classes."""global _embedding_modelif _embedding_model is None:from sentence_transformers import SentenceTransformerprint("Loading embedding model...")_embedding_model = SentenceTransformer('all-MiniLM-L6-v2')return _embedding_modeldef get_naive():global _naive_ragif _naive_rag is None:from app.rag_naive import NaiveRAGprint("Initializing Naive RAG...")# Pass the shared embedding model if the class supports it# (you may need to modify your RAG classes to accept a model argument)_naive_rag = NaiveRAG()# If NaiveRAG has a set_embedding_model method, call it:# _naive_rag.set_embedding_model(get_embedding_model())return _naive_ragdef get_optimized():global _optimized_ragif _optimized_rag is None:from app.rag_optimized import OptimizedRAGprint("Initializing Optimized RAG...")_optimized_rag = OptimizedRAG()return _optimized_ragdef get_no_compromise():global _no_compromise_ragif _no_compromise_rag is None:from app.no_compromise_rag import NoCompromiseRAGprint("Initializing No-Compromise RAG...")_no_compromise_rag = NoCompromiseRAG()return _no_compromise_ragdef query_naive(question):try:rag = get_naive()start = time.perf_counter()answer, chunks_used, cache_hit = rag.query(question)latency = (time.perf_counter() - start) * 1000return answer, f"{latency:.1f} ms", str(chunks_used), "Yes" if cache_hit else "No"except Exception as e:return f"Error: {e}", "0 ms", "0", "No"def query_optimized(question):try:rag = get_optimized()start = time.perf_counter()answer, chunks_used, cache_hit = rag.query(question)latency = (time.perf_counter() - start) * 1000return answer, f"{latency:.1f} ms", str(chunks_used), "Yes" if cache_hit else "No"except Exception as e:return f"Error: {e}", "0 ms", "0", "No"def query_no_compromise(question):try:rag = get_no_compromise()start = time.perf_counter()answer, chunks_used, cache_hit = rag.query(question)latency = (time.perf_counter() - start) * 1000return answer, f"{latency:.1f} ms", str(chunks_used), "Yes" if cache_hit else "No"except Exception as e:return f"Error: {e}", "0 ms", "0", "No"# -------------------------------------------------------------------# Build the Gradio interface63 unmodified lines**Caching**: SQLite (Optimized) + LRU memory | **Generation**: Simulated (real LLM can be plugged in)""")if __name__ == "__main__":demo.launch(server_name="0.0.0.0", server_port=7860)
app.py+66-32