Spaces:
Sleeping
Sleeping
Islam Mamedov commited on
Commit ·
cb2410e
1
Parent(s): 1c5f7c3
Self-building index for Spaces deploy
Browse files
app.py
CHANGED
|
@@ -1,18 +1,20 @@
|
|
| 1 |
"""Streamlit UI for the FastAPI Codebase Q&A RAG system.
|
| 2 |
|
| 3 |
Run locally:
|
| 4 |
-
|
| 5 |
-
streamlit run app.py
|
| 6 |
|
| 7 |
-
On Hugging Face Spaces, set GROQ_API_KEY as a Space secret.
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
|
| 10 |
import os
|
| 11 |
import sys
|
| 12 |
-
from dotenv import load_dotenv
|
| 13 |
-
load_dotenv(override=True)
|
| 14 |
from pathlib import Path
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
sys.path.insert(0, str(Path(__file__).parent / "src"))
|
| 17 |
|
| 18 |
import streamlit as st
|
|
@@ -31,6 +33,26 @@ EXAMPLES = [
|
|
| 31 |
|
| 32 |
st.set_page_config(page_title="FastAPI Codebase Q&A", page_icon="⚡",
|
| 33 |
layout="wide")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
st.title("⚡ FastAPI Codebase Q&A")
|
| 35 |
st.caption("Retrieval-augmented answers over FastAPI's source code, docs, "
|
| 36 |
"and GitHub issues — every claim cited, honest refusals when "
|
|
@@ -54,8 +76,8 @@ with st.sidebar:
|
|
| 54 |
"recall@5 **0.91** · MRR **0.71**\n\n"
|
| 55 |
"faithful **0.89** · correct **0.91** · refusal **7/7**"
|
| 56 |
)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
|
| 61 |
@st.cache_data(show_spinner=False)
|
|
|
|
| 1 |
"""Streamlit UI for the FastAPI Codebase Q&A RAG system.
|
| 2 |
|
| 3 |
Run locally:
|
| 4 |
+
streamlit run app.py # reads GROQ_API_KEY from .env
|
|
|
|
| 5 |
|
| 6 |
+
On Hugging Face Spaces, set GROQ_API_KEY as a Space secret. The Space has
|
| 7 |
+
no prebuilt index, so ensure_index() builds it from data/chunks.jsonl on
|
| 8 |
+
first boot (~3 min, cached afterwards).
|
| 9 |
"""
|
| 10 |
|
| 11 |
import os
|
| 12 |
import sys
|
|
|
|
|
|
|
| 13 |
from pathlib import Path
|
| 14 |
|
| 15 |
+
from dotenv import load_dotenv
|
| 16 |
+
|
| 17 |
+
load_dotenv(override=True)
|
| 18 |
sys.path.insert(0, str(Path(__file__).parent / "src"))
|
| 19 |
|
| 20 |
import streamlit as st
|
|
|
|
| 33 |
|
| 34 |
st.set_page_config(page_title="FastAPI Codebase Q&A", page_icon="⚡",
|
| 35 |
layout="wide")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@st.cache_resource(show_spinner="First boot: building the search index "
|
| 39 |
+
"(~3 min, one time only)...")
|
| 40 |
+
def ensure_index() -> bool:
|
| 41 |
+
"""Build the Chroma index from chunks.jsonl if it doesn't exist yet."""
|
| 42 |
+
import chromadb
|
| 43 |
+
client = chromadb.PersistentClient(path="data/chroma")
|
| 44 |
+
try:
|
| 45 |
+
if client.get_collection("chunks").count() > 0:
|
| 46 |
+
return True
|
| 47 |
+
except Exception:
|
| 48 |
+
pass
|
| 49 |
+
import index # src/index.py
|
| 50 |
+
index.main()
|
| 51 |
+
return True
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
ensure_index()
|
| 55 |
+
|
| 56 |
st.title("⚡ FastAPI Codebase Q&A")
|
| 57 |
st.caption("Retrieval-augmented answers over FastAPI's source code, docs, "
|
| 58 |
"and GitHub issues — every claim cited, honest refusals when "
|
|
|
|
| 76 |
"recall@5 **0.91** · MRR **0.71**\n\n"
|
| 77 |
"faithful **0.89** · correct **0.91** · refusal **7/7**"
|
| 78 |
)
|
| 79 |
+
st.markdown("[Source & write-up]"
|
| 80 |
+
"(https://github.com/Pace200413/codebase-rag)")
|
| 81 |
|
| 82 |
|
| 83 |
@st.cache_data(show_spinner=False)
|