Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Add the repo root to path so we can import the /app modules
|
| 7 |
+
sys.path.append(os.path.dirname(__file__))
|
| 8 |
+
|
| 9 |
+
# Import your three RAG implementations
|
| 10 |
+
from app.rag_naive import NaiveRAG
|
| 11 |
+
from app.rag_optimized import OptimizedRAG
|
| 12 |
+
from app.no_compromise_rag import NoCompromiseRAG
|
| 13 |
+
|
| 14 |
+
# -------------------------------------------------------------------
|
| 15 |
+
# Initialize the three RAG systems once at startup.
|
| 16 |
+
# If memory becomes an issue, we can lazy‑load them inside each function.
|
| 17 |
+
# -------------------------------------------------------------------
|
| 18 |
+
print("Initializing Naive RAG...")
|
| 19 |
+
naive_rag = NaiveRAG() # loads embedding model + FAISS index
|
| 20 |
+
print("Initializing Optimized RAG...")
|
| 21 |
+
optimized_rag = OptimizedRAG() # loads the same + SQLite cache
|
| 22 |
+
print("Initializing No‑Compromise RAG...")
|
| 23 |
+
no_compromise_rag = NoCompromiseRAG()
|
| 24 |
+
print("All RAG systems ready.")
|
| 25 |
+
|
| 26 |
+
# -------------------------------------------------------------------
|
| 27 |
+
# Define the query functions for each mode
|
| 28 |
+
# -------------------------------------------------------------------
|
| 29 |
+
def query_naive(question):
|
| 30 |
+
start = time.perf_counter()
|
| 31 |
+
answer, chunks_used, cache_hit = naive_rag.query(question)
|
| 32 |
+
latency = (time.perf_counter() - start) * 1000
|
| 33 |
+
return answer, f"{latency:.1f} ms", chunks_used, "Yes" if cache_hit else "No"
|
| 34 |
+
|
| 35 |
+
def query_optimized(question):
|
| 36 |
+
start = time.perf_counter()
|
| 37 |
+
answer, chunks_used, cache_hit = optimized_rag.query(question)
|
| 38 |
+
latency = (time.perf_counter() - start) * 1000
|
| 39 |
+
return answer, f"{latency:.1f} ms", chunks_used, "Yes" if cache_hit else "No"
|
| 40 |
+
|
| 41 |
+
def query_no_compromise(question):
|
| 42 |
+
start = time.perf_counter()
|
| 43 |
+
answer, chunks_used, cache_hit = no_compromise_rag.query(question)
|
| 44 |
+
latency = (time.perf_counter() - start) * 1000
|
| 45 |
+
return answer, f"{latency:.1f} ms", chunks_used, "Yes" if cache_hit else "No"
|
| 46 |
+
|
| 47 |
+
# -------------------------------------------------------------------
|
| 48 |
+
# Build the Gradio interface
|
| 49 |
+
# -------------------------------------------------------------------
|
| 50 |
+
with gr.Blocks(title="RAG Latency Optimization", theme=gr.themes.Soft()) as demo:
|
| 51 |
+
gr.Markdown("""
|
| 52 |
+
# ⚡ RAG Latency Optimization
|
| 53 |
+
### Compare Naive, Optimized, and No‑Compromise RAG on CPU‑only hardware
|
| 54 |
+
**Proven 2.7× speedup (247ms → 92ms)** – now interactive!
|
| 55 |
+
""")
|
| 56 |
+
|
| 57 |
+
with gr.Tabs():
|
| 58 |
+
# ----- Naive RAG tab -----
|
| 59 |
+
with gr.TabItem("🐢 Naive RAG (Baseline)"):
|
| 60 |
+
with gr.Row():
|
| 61 |
+
question_naive = gr.Textbox(label="Your Question", lines=2)
|
| 62 |
+
submit_naive = gr.Button("Ask", variant="primary")
|
| 63 |
+
with gr.Row():
|
| 64 |
+
answer_naive = gr.Textbox(label="Answer", lines=4)
|
| 65 |
+
with gr.Row():
|
| 66 |
+
latency_naive = gr.Textbox(label="Latency", interactive=False)
|
| 67 |
+
chunks_naive = gr.Textbox(label="Chunks Used", interactive=False)
|
| 68 |
+
cache_naive = gr.Textbox(label="Cache Hit", interactive=False)
|
| 69 |
+
submit_naive.click(
|
| 70 |
+
query_naive,
|
| 71 |
+
inputs=question_naive,
|
| 72 |
+
outputs=[answer_naive, latency_naive, chunks_naive, cache_naive]
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# ----- Optimized RAG tab -----
|
| 76 |
+
with gr.TabItem("⚡ Optimized RAG (Production)"):
|
| 77 |
+
with gr.Row():
|
| 78 |
+
question_opt = gr.Textbox(label="Your Question", lines=2)
|
| 79 |
+
submit_opt = gr.Button("Ask", variant="primary")
|
| 80 |
+
with gr.Row():
|
| 81 |
+
answer_opt = gr.Textbox(label="Answer", lines=4)
|
| 82 |
+
with gr.Row():
|
| 83 |
+
latency_opt = gr.Textbox(label="Latency", interactive=False)
|
| 84 |
+
chunks_opt = gr.Textbox(label="Chunks Used", interactive=False)
|
| 85 |
+
cache_opt = gr.Textbox(label="Cache Hit", interactive=False)
|
| 86 |
+
submit_opt.click(
|
| 87 |
+
query_optimized,
|
| 88 |
+
inputs=question_opt,
|
| 89 |
+
outputs=[answer_opt, latency_opt, chunks_opt, cache_opt]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# ----- No‑Compromise RAG tab -----
|
| 93 |
+
with gr.TabItem("🚀 No‑Compromise RAG (Max Speed)"):
|
| 94 |
+
with gr.Row():
|
| 95 |
+
question_nc = gr.Textbox(label="Your Question", lines=2)
|
| 96 |
+
submit_nc = gr.Button("Ask", variant="primary")
|
| 97 |
+
with gr.Row():
|
| 98 |
+
answer_nc = gr.Textbox(label="Answer", lines=4)
|
| 99 |
+
with gr.Row():
|
| 100 |
+
latency_nc = gr.Textbox(label="Latency", interactive=False)
|
| 101 |
+
chunks_nc = gr.Textbox(label="Chunks Used", interactive=False)
|
| 102 |
+
cache_nc = gr.Textbox(label="Cache Hit", interactive=False)
|
| 103 |
+
submit_nc.click(
|
| 104 |
+
query_no_compromise,
|
| 105 |
+
inputs=question_nc,
|
| 106 |
+
outputs=[answer_nc, latency_nc, chunks_nc, cache_nc]
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
gr.Markdown("""
|
| 110 |
+
---
|
| 111 |
+
**Architecture**: CPU‑only | **Embeddings**: `all-MiniLM-L6-v2` | **Vector Store**: FAISS
|
| 112 |
+
**Caching**: SQLite (Optimized) + LRU memory | **Generation**: Simulated (real LLM can be plugged in)
|
| 113 |
+
""")
|
| 114 |
+
|
| 115 |
+
# Launch the app
|
| 116 |
+
if __name__ == "__main__":
|
| 117 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|