Update app.py
Browse files
app.py
CHANGED
|
@@ -2,42 +2,73 @@ import streamlit as st
|
|
| 2 |
import requests
|
| 3 |
import time
|
| 4 |
|
| 5 |
-
time.sleep(3)
|
| 6 |
-
|
| 7 |
BACKEND_URL = "http://127.0.0.1:8000"
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
r = requests.post(
|
| 15 |
-
f"{BACKEND_URL}/load",
|
| 16 |
-
params={"repo_url": repo_url}
|
| 17 |
-
)
|
| 18 |
-
if r.status_code == 200:
|
| 19 |
-
st.success("Repository indexed successfully!")
|
| 20 |
-
else:
|
| 21 |
-
st.error("Indexing failed")
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
r = requests.get(
|
| 29 |
-
f"{BACKEND_URL}/ask",
|
| 30 |
-
params={"question": question}
|
| 31 |
-
)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
else:
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
st.write(data["answer"])
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
import time
|
| 4 |
|
|
|
|
|
|
|
| 5 |
BACKEND_URL = "http://127.0.0.1:8000"
|
| 6 |
+
HEALTH_URL = f"{BACKEND_URL}/health"
|
| 7 |
|
| 8 |
+
def wait_for_backend(timeout=30, interval=1):
|
| 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 |
+
st.title("🧠 Codebase RAG Assistant (Demo)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
st.info("Waiting for backend... (this may take a few seconds after startup)")
|
| 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 |
+
if not repo_url:
|
| 37 |
+
st.warning("Enter GitHub repo URL first")
|
| 38 |
else:
|
| 39 |
+
try:
|
| 40 |
+
r = requests.post(
|
| 41 |
+
f"{BACKEND_URL}/load",
|
| 42 |
+
params={"repo_url": repo_url},
|
| 43 |
+
timeout=120 # indexing can take time
|
| 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 not question:
|
| 56 |
+
st.warning("Type a question first")
|
| 57 |
+
else:
|
| 58 |
+
try:
|
| 59 |
+
r = requests.get(
|
| 60 |
+
f"{BACKEND_URL}/ask",
|
| 61 |
+
params={"question": question},
|
| 62 |
+
timeout=60
|
| 63 |
+
)
|
| 64 |
+
if r.status_code == 200:
|
| 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}")
|