File size: 3,382 Bytes
462062c
 
 
 
9f0dfcc
462062c
 
 
80f2420
462062c
9f0dfcc
462062c
 
 
9f0dfcc
 
 
462062c
9f0dfcc
 
 
 
 
 
 
 
 
462062c
 
9f0dfcc
462062c
9f0dfcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
462062c
 
 
9f0dfcc
 
 
 
 
462062c
 
 
 
9f0dfcc
 
462062c
 
 
 
 
9f0dfcc
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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(
    """
    <div style='text-align:center; padding: 1rem 0 0.5rem;'>
        <h1 style='font-size:2.2rem; margin-bottom:0;'>πŸ’» Codebot</h1>
        <p style='color:gray; font-size:1rem; margin-top:0.25rem;'>
            Chat with any GitHub repository using RAG + Llama 3
        </p>
    </div>
    """,
    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()