rcs3 commited on
Commit
9f0dfcc
Β·
verified Β·
1 Parent(s): 80f2420

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -34
app.py CHANGED
@@ -1,62 +1,81 @@
1
- import time
2
  import streamlit as st
3
-
4
  from repochat.utils import init_session_state
5
  from repochat.git import git_form
6
  from repochat.db import vector_db, load_to_db
7
- from repochat.models import hf_embeddings, code_llama
8
  from repochat.chain import response_chain
9
 
10
- init_session_state()
11
-
12
  st.set_page_config(
13
  page_title="Codebot",
14
  page_icon="πŸ’»",
 
15
  initial_sidebar_state="expanded",
16
  )
17
 
 
 
 
18
  st.markdown(
19
- "<h1 style='text-align: center;'>Codebot</h1>",
20
- unsafe_allow_html=True
 
 
 
 
 
 
 
21
  )
22
 
 
23
  try:
24
- st.session_state["db_name"], st.session_state['git_form'] = git_form(st.session_state['repo_path'])
25
-
26
- if st.session_state['git_form']:
27
- with st.spinner('Loading the contents to database. This may take some time...'):
28
- st.session_state["chroma_db"] = vector_db(
29
- hf_embeddings(),
30
- load_to_db(st.session_state['repo_path'])
31
- )
32
- with st.spinner('Loading model to memory'):
33
- st.session_state["qa"] = response_chain(
34
- db=st.session_state["chroma_db"],
35
- llm=code_llama()
36
- )
37
-
38
- st.session_state["db_loaded"] = True
 
 
 
 
39
  except TypeError:
40
  pass
41
 
42
- if st.session_state["db_loaded"]:
 
 
 
 
43
  for message in st.session_state["messages"]:
44
  with st.chat_message(message["role"]):
45
  st.markdown(message["content"])
46
 
47
- if prompt := st.chat_input("Enter your query"):
 
48
  st.session_state["messages"].append({"role": "user", "content": prompt})
49
  with st.chat_message("user"):
50
  st.markdown(prompt)
51
 
52
  with st.chat_message("assistant"):
53
- message_placeholder = st.empty()
54
- full_response = ""
55
- with st.spinner("Generating response..."):
56
- result = st.session_state["qa"](prompt)
57
- for chunk in result['answer'].split():
58
- full_response += chunk + " "
59
- time.sleep(0.05)
60
- message_placeholder.markdown(full_response + "β–Œ")
61
- message_placeholder.markdown(full_response)
62
- st.session_state["messages"].append({"role": "assistant", "content": full_response})
 
 
 
 
1
  import streamlit as st
 
2
  from repochat.utils import init_session_state
3
  from repochat.git import git_form
4
  from repochat.db import vector_db, load_to_db
5
+ from repochat.models import hf_embeddings, groq_llm
6
  from repochat.chain import response_chain
7
 
 
 
8
  st.set_page_config(
9
  page_title="Codebot",
10
  page_icon="πŸ’»",
11
+ layout="wide",
12
  initial_sidebar_state="expanded",
13
  )
14
 
15
+ init_session_state()
16
+
17
+ # ── Header ────────────────────────────────────────────────────────────────────
18
  st.markdown(
19
+ """
20
+ <div style='text-align:center; padding: 1rem 0 0.5rem;'>
21
+ <h1 style='font-size:2.2rem; margin-bottom:0;'>πŸ’» Codebot</h1>
22
+ <p style='color:gray; font-size:1rem; margin-top:0.25rem;'>
23
+ Chat with any GitHub repository using RAG + Llama 3
24
+ </p>
25
+ </div>
26
+ """,
27
+ unsafe_allow_html=True,
28
  )
29
 
30
+ # ── Sidebar: GitHub form ───────────────────────────────────────────────────────
31
  try:
32
+ result = git_form(st.session_state["repo_path"])
33
+ if result:
34
+ db_name, submitted = result
35
+ st.session_state["db_name"] = db_name
36
+
37
+ if submitted and not st.session_state["db_loaded"]:
38
+ with st.spinner("πŸ“‚ Loading repository files into vector database…"):
39
+ st.session_state["chroma_db"] = vector_db(
40
+ hf_embeddings(),
41
+ load_to_db(st.session_state["repo_path"]),
42
+ )
43
+ with st.spinner("πŸ€– Initialising LLM chain…"):
44
+ st.session_state["qa"] = response_chain(
45
+ db=st.session_state["chroma_db"],
46
+ llm=groq_llm(),
47
+ )
48
+ st.session_state["db_loaded"] = True
49
+ st.success(f"βœ… Ready! Ask anything about **{db_name.replace('_', '/')}**")
50
+
51
  except TypeError:
52
  pass
53
 
54
+ # ── Chat area ─────────────────────────────────────────────────────────────────
55
+ if not st.session_state["db_loaded"]:
56
+ st.info("πŸ‘ˆ Paste a public GitHub repository URL in the sidebar to get started.")
57
+ else:
58
+ # Render chat history
59
  for message in st.session_state["messages"]:
60
  with st.chat_message(message["role"]):
61
  st.markdown(message["content"])
62
 
63
+ # New user input
64
+ if prompt := st.chat_input("Ask about the codebase…"):
65
  st.session_state["messages"].append({"role": "user", "content": prompt})
66
  with st.chat_message("user"):
67
  st.markdown(prompt)
68
 
69
  with st.chat_message("assistant"):
70
+ with st.spinner("Thinking…"):
71
+ result = st.session_state["qa"].invoke({"question": prompt})
72
+ answer = result.get("answer", result.get("result", "Sorry, I couldn't generate a response."))
73
+
74
+ st.markdown(answer)
75
+ st.session_state["messages"].append({"role": "assistant", "content": answer})
76
+
77
+ # Clear chat button
78
+ if st.session_state["messages"]:
79
+ if st.button("πŸ—‘οΈ Clear chat", use_container_width=False):
80
+ st.session_state["messages"] = []
81
+ st.rerun()