File size: 2,917 Bytes
f3405cb
 
 
 
 
 
 
72af804
f3405cb
18c7f7e
1457ac5
f3405cb
1a59d21
f3405cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a59d21
f3405cb
 
 
 
 
 
1a59d21
f3405cb
 
 
 
 
 
 
 
1a59d21
 
f3405cb
1a59d21
 
 
 
f3405cb
1a59d21
 
 
f3405cb
1a59d21
 
f3405cb
1a59d21
 
 
 
f3405cb
 
1a59d21
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
import streamlit as st
from src.main import ConversationalResponse
import os

# Constants
ROLE_USER = "user"
ROLE_ASSISTANT = "assistant"
MAX_MESSAGES = 5

st.set_page_config(page_title="Chat with Git", page_icon="🦜")
st.title("Chat with Git πŸ€–πŸ“š")
st.markdown("by [Rohan Kataria](https://www.linkedin.com/in/imrohan/) view more at [VEW.AI](https://vew.ai/)")
st.markdown("This app allows you to chat with Git code files. You can paste link to the Git repository and ask questions about it. In the background uses the Git Loader and ConversationalRetrieval chain from langchain, Streamlit for UI.")

@st.cache_resource(ttl="1h")
def load_agent(url, branch, file_filter):
    with st.spinner('Loading Git documents...'):
        agent = ConversationalResponse(url, branch, file_filter)
    st.success("Git Loaded Successfully")
    return agent

def main():

    git_link = st.sidebar.text_input("Enter your Git Link")
    branch = st.sidebar.text_input("Enter your Git Branch")
    file_filter = st.sidebar.text_input("Enter the Extension of Files to Load eg. py,sql,r (no spaces)")

    if "agent" not in st.session_state:
        st.session_state["agent"] = None
        st.session_state["user_message_count"] = 0

    if st.sidebar.button("Load Agent"):
        if git_link and branch and file_filter:
            try:
                st.session_state["agent"] = load_agent(git_link, branch, file_filter)
                st.session_state["messages"] = [{"role": ROLE_ASSISTANT, "content": "How can I help you?"}]
                st.session_state["user_message_count"] = 0
            except Exception as e:
                st.sidebar.error(f"Error loading Git repository: {str(e)}")
                return

    if st.session_state["agent"]:  # Chat will only appear if the agent is loaded
        for msg in st.session_state.messages:
            st.chat_message(msg["role"]).write(msg["content"])

        if st.session_state["user_message_count"] < MAX_MESSAGES:
            user_query = st.chat_input(placeholder="Ask me anything!")

            if user_query:
                st.session_state.messages.append({"role": ROLE_USER, "content": user_query})
                st.chat_message(ROLE_USER).write(user_query)
                st.session_state["user_message_count"] += 1

                # Generate the response
                with st.spinner("Generating response"):
                    response = st.session_state["agent"](user_query)

                # Display the response immediately
                st.chat_message(ROLE_ASSISTANT).write(response)

                # Add the response to the message history
                st.session_state.messages.append({"role": ROLE_ASSISTANT, "content": response})
        else:
            st.warning("Your message limit is over. Contact [Rohan Kataria](https://www.linkedin.com/in/imrohan/) to increase the limit.")

if __name__ == "__main__":
    main()