aman1762 commited on
Commit
5454ddf
·
verified ·
1 Parent(s): 39f1b00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -62
app.py CHANGED
@@ -1,74 +1,39 @@
1
  import streamlit as st
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}")
 
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"])