import streamlit as st
from repochat.utils import init_session_state
from repochat.git import git_form
from repochat.db import vector_db, load_to_db
from repochat.models import hf_embeddings, groq_llm
from repochat.chain import response_chain
st.set_page_config(
page_title="Codebot",
page_icon="π»",
layout="wide",
initial_sidebar_state="expanded",
)
init_session_state()
# ββ Header ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
st.markdown(
"""
π» Codebot
Chat with any GitHub repository using RAG + Llama 3
""",
unsafe_allow_html=True,
)
# ββ Sidebar: GitHub form βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
try:
result = git_form(st.session_state["repo_path"])
if result:
db_name, submitted = result
st.session_state["db_name"] = db_name
if submitted and not st.session_state["db_loaded"]:
with st.spinner("π Loading repository files into vector databaseβ¦"):
st.session_state["chroma_db"] = vector_db(
hf_embeddings(),
load_to_db(st.session_state["repo_path"]),
)
with st.spinner("π€ Initialising LLM chainβ¦"):
st.session_state["qa"] = response_chain(
db=st.session_state["chroma_db"],
llm=groq_llm(),
)
st.session_state["db_loaded"] = True
st.success(f"β
Ready! Ask anything about **{db_name.replace('_', '/')}**")
except TypeError:
pass
# ββ Chat area βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if not st.session_state["db_loaded"]:
st.info("π Paste a public GitHub repository URL in the sidebar to get started.")
else:
# Render chat history
for message in st.session_state["messages"]:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# New user input
if prompt := st.chat_input("Ask about the codebaseβ¦"):
st.session_state["messages"].append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
with st.spinner("Thinkingβ¦"):
result = st.session_state["qa"].invoke({"question": prompt})
answer = result.get("answer", result.get("result", "Sorry, I couldn't generate a response."))
st.markdown(answer)
st.session_state["messages"].append({"role": "assistant", "content": answer})
# Clear chat button
if st.session_state["messages"]:
if st.button("ποΈ Clear chat", use_container_width=False):
st.session_state["messages"] = []
st.rerun()