Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,39 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
HEALTH_URL = f"{BACKEND_URL}/health"
|
| 7 |
|
| 8 |
-
|
| 9 |
-
"""Wait until backend /health returns 200 or timeout (seconds)."""
|
| 10 |
-
t0 = time.time()
|
| 11 |
-
while True:
|
| 12 |
-
try:
|
| 13 |
-
r = requests.get(HEALTH_URL, timeout=1)
|
| 14 |
-
if r.status_code == 200:
|
| 15 |
-
return True
|
| 16 |
-
except Exception:
|
| 17 |
-
pass
|
| 18 |
-
if time.time() - t0 > timeout:
|
| 19 |
-
return False
|
| 20 |
-
time.sleep(interval)
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
st.
|
| 25 |
-
ready = wait_for_backend(timeout=45, interval=1)
|
| 26 |
-
|
| 27 |
-
if not ready:
|
| 28 |
-
st.error("Backend not available. Check space logs. Try restarting the Space.")
|
| 29 |
-
st.stop()
|
| 30 |
-
else:
|
| 31 |
-
st.success("Backend available ✅")
|
| 32 |
-
|
| 33 |
-
repo_url = st.text_input("GitHub Repository URL", placeholder="https://github.com/your/repo")
|
| 34 |
|
| 35 |
if st.button("Index Repository"):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if r.status_code == 200:
|
| 46 |
-
st.success("Repository indexed successfully!")
|
| 47 |
-
else:
|
| 48 |
-
st.error(f"Indexing failed: {r.status_code} {r.text}")
|
| 49 |
-
except requests.exceptions.RequestException as e:
|
| 50 |
-
st.error(f"Request failed: {e}")
|
| 51 |
|
| 52 |
question = st.text_input("Ask a question about the codebase")
|
| 53 |
|
| 54 |
if st.button("Ask"):
|
| 55 |
-
if
|
| 56 |
-
st.warning("
|
| 57 |
else:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
data = r.json()
|
| 66 |
-
st.markdown("### Answer")
|
| 67 |
-
st.write(data.get("answer", "No answer returned"))
|
| 68 |
-
st.markdown("### Sources")
|
| 69 |
-
for src in data.get("sources", []):
|
| 70 |
-
st.write(src)
|
| 71 |
-
else:
|
| 72 |
-
st.error(f"Backend error: {r.status_code} {r.text}")
|
| 73 |
-
except requests.exceptions.RequestException as e:
|
| 74 |
-
st.error(f"Request failed: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from ingest import load_repo, ingest_repo
|
| 4 |
+
from vectorstore import create_vectorstore
|
| 5 |
+
from rag_chain import build_rag_chain
|
| 6 |
|
| 7 |
+
st.set_page_config(page_title="Codebase RAG Assistant")
|
|
|
|
| 8 |
|
| 9 |
+
st.title("🧠 Codebase RAG Assistant")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
if "qa_chain" not in st.session_state:
|
| 12 |
+
st.session_state.qa_chain = None
|
| 13 |
|
| 14 |
+
repo_url = st.text_input("GitHub Repository URL")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
if st.button("Index Repository"):
|
| 17 |
+
with st.spinner("Indexing repository..."):
|
| 18 |
+
path = load_repo(repo_url)
|
| 19 |
+
docs = ingest_repo(path)
|
| 20 |
+
vectorstore = create_vectorstore(docs)
|
| 21 |
+
st.session_state.qa_chain = build_rag_chain(
|
| 22 |
+
vectorstore,
|
| 23 |
+
os.getenv("GROQ_API_KEY")
|
| 24 |
+
)
|
| 25 |
+
st.success("Repository indexed successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
question = st.text_input("Ask a question about the codebase")
|
| 28 |
|
| 29 |
if st.button("Ask"):
|
| 30 |
+
if st.session_state.qa_chain is None:
|
| 31 |
+
st.warning("Please index a repository first")
|
| 32 |
else:
|
| 33 |
+
with st.spinner("Thinking..."):
|
| 34 |
+
result = st.session_state.qa_chain(question)
|
| 35 |
+
st.markdown("### Answer")
|
| 36 |
+
st.write(result["result"])
|
| 37 |
+
st.markdown("### Sources")
|
| 38 |
+
for doc in result["source_documents"]:
|
| 39 |
+
st.write(doc.metadata["file"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|