Update app.py
Browse files
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,
|
| 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 |
-
"
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
)
|
| 22 |
|
|
|
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
except TypeError:
|
| 40 |
pass
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
for message in st.session_state["messages"]:
|
| 44 |
with st.chat_message(message["role"]):
|
| 45 |
st.markdown(message["content"])
|
| 46 |
|
| 47 |
-
|
|
|
|
| 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 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|