Spaces:
Sleeping
Sleeping
Update client
Browse files
app.py
CHANGED
@@ -1,24 +1,15 @@
|
|
1 |
import os
|
2 |
-
|
3 |
import streamlit as st
|
4 |
from langchain.callbacks.base import BaseCallbackHandler
|
5 |
from langchain.chains import ConversationalRetrievalChain
|
6 |
from langchain.schema import ChatMessage
|
7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
from langchain_community.document_loaders import Docx2txtLoader, PyPDFLoader, TextLoader
|
9 |
-
from langchain_community.embeddings.openai import OpenAIEmbeddings
|
10 |
from langchain_community.vectorstores.chroma import Chroma
|
11 |
-
from langchain_openai import ChatOpenAI
|
12 |
-
|
13 |
|
14 |
st.set_page_config(page_title="InkChatGPT", page_icon="π")
|
15 |
|
16 |
-
with st.sidebar:
|
17 |
-
openai_api_key = st.text_input("OpenAI API Key", type="password")
|
18 |
-
|
19 |
-
if not openai_api_key:
|
20 |
-
st.info("Please add your OpenAI API key to continue.")
|
21 |
-
|
22 |
|
23 |
class StreamHandler(BaseCallbackHandler):
|
24 |
def __init__(self, container, initial_text=""):
|
@@ -30,7 +21,7 @@ class StreamHandler(BaseCallbackHandler):
|
|
30 |
self.container.markdown(self.text)
|
31 |
|
32 |
|
33 |
-
def load_and_process_file(file_data
|
34 |
"""
|
35 |
Load and process the uploaded file.
|
36 |
Returns a vector store containing the embedded chunks of the file.
|
@@ -49,7 +40,7 @@ def load_and_process_file(file_data, openai_api_key):
|
|
49 |
elif extension == ".txt":
|
50 |
loader = TextLoader(file_name)
|
51 |
else:
|
52 |
-
st.
|
53 |
return None
|
54 |
|
55 |
documents = loader.load()
|
@@ -59,14 +50,12 @@ def load_and_process_file(file_data, openai_api_key):
|
|
59 |
chunk_overlap=200,
|
60 |
)
|
61 |
chunks = text_splitter.split_documents(documents)
|
62 |
-
|
63 |
-
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
64 |
vector_store = Chroma.from_documents(chunks, embeddings)
|
65 |
-
|
66 |
return vector_store
|
67 |
|
68 |
|
69 |
-
def initialize_chat_model(vector_store
|
70 |
"""
|
71 |
Initialize the chat model with the given vector store.
|
72 |
Returns a ConversationalRetrievalChain instance.
|
@@ -74,7 +63,7 @@ def initialize_chat_model(vector_store, openai_api_key):
|
|
74 |
llm = ChatOpenAI(
|
75 |
model="gpt-3.5-turbo",
|
76 |
temperature=0,
|
77 |
-
openai_api_key=
|
78 |
)
|
79 |
retriever = vector_store.as_retriever()
|
80 |
return ConversationalRetrievalChain.from_llm(llm, retriever)
|
@@ -85,60 +74,41 @@ def main():
|
|
85 |
The main function that runs the Streamlit app.
|
86 |
"""
|
87 |
|
88 |
-
st.
|
89 |
-
st.
|
|
|
|
|
90 |
|
91 |
-
|
92 |
-
"
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
openai_api_key,
|
100 |
-
)
|
101 |
-
|
102 |
-
if vector_store:
|
103 |
-
crc = initialize_chat_model(
|
104 |
-
vector_store,
|
105 |
-
openai_api_key=openai_api_key,
|
106 |
-
)
|
107 |
-
st.session_state.crc = crc
|
108 |
-
st.success("File processed successfully!")
|
109 |
-
|
110 |
-
if "crc" in st.session_state:
|
111 |
-
st.session_state["messages"] = [
|
112 |
-
ChatMessage(role="assistant", content="How can I help you?")
|
113 |
-
]
|
114 |
-
|
115 |
-
if prompt := st.chat_input():
|
116 |
-
st.session_state.messages.append(
|
117 |
-
ChatMessage(
|
118 |
-
role="user",
|
119 |
-
content=prompt,
|
120 |
-
)
|
121 |
)
|
122 |
-
|
|
|
123 |
|
124 |
-
|
125 |
|
126 |
|
127 |
-
def handle_question(question
|
128 |
"""
|
129 |
Handles the user's question by generating a response and updating the chat history.
|
130 |
"""
|
131 |
crc = st.session_state.crc
|
|
|
132 |
if "history" not in st.session_state:
|
133 |
st.session_state["history"] = []
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
)
|
142 |
|
143 |
st.session_state["history"].append((question, response))
|
144 |
|
@@ -148,7 +118,7 @@ def handle_question(question, openai_api_key):
|
|
148 |
with st.chat_message("assistant"):
|
149 |
stream_handler = StreamHandler(st.empty())
|
150 |
llm = ChatOpenAI(
|
151 |
-
openai_api_key=
|
152 |
streaming=True,
|
153 |
callbacks=[stream_handler],
|
154 |
)
|
@@ -179,5 +149,40 @@ def clear_history():
|
|
179 |
del st.session_state["history"]
|
180 |
|
181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
if __name__ == "__main__":
|
|
|
183 |
main()
|
|
|
1 |
import os
|
|
|
2 |
import streamlit as st
|
3 |
from langchain.callbacks.base import BaseCallbackHandler
|
4 |
from langchain.chains import ConversationalRetrievalChain
|
5 |
from langchain.schema import ChatMessage
|
6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
from langchain_community.document_loaders import Docx2txtLoader, PyPDFLoader, TextLoader
|
|
|
8 |
from langchain_community.vectorstores.chroma import Chroma
|
9 |
+
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
|
|
10 |
|
11 |
st.set_page_config(page_title="InkChatGPT", page_icon="π")
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
class StreamHandler(BaseCallbackHandler):
|
15 |
def __init__(self, container, initial_text=""):
|
|
|
21 |
self.container.markdown(self.text)
|
22 |
|
23 |
|
24 |
+
def load_and_process_file(file_data):
|
25 |
"""
|
26 |
Load and process the uploaded file.
|
27 |
Returns a vector store containing the embedded chunks of the file.
|
|
|
40 |
elif extension == ".txt":
|
41 |
loader = TextLoader(file_name)
|
42 |
else:
|
43 |
+
st.error("This document format is not supported!")
|
44 |
return None
|
45 |
|
46 |
documents = loader.load()
|
|
|
50 |
chunk_overlap=200,
|
51 |
)
|
52 |
chunks = text_splitter.split_documents(documents)
|
53 |
+
embeddings = OpenAIEmbeddings(openai_api_key=st.session_state.api_key)
|
|
|
54 |
vector_store = Chroma.from_documents(chunks, embeddings)
|
|
|
55 |
return vector_store
|
56 |
|
57 |
|
58 |
+
def initialize_chat_model(vector_store):
|
59 |
"""
|
60 |
Initialize the chat model with the given vector store.
|
61 |
Returns a ConversationalRetrievalChain instance.
|
|
|
63 |
llm = ChatOpenAI(
|
64 |
model="gpt-3.5-turbo",
|
65 |
temperature=0,
|
66 |
+
openai_api_key=st.session_state.api_key,
|
67 |
)
|
68 |
retriever = vector_store.as_retriever()
|
69 |
return ConversationalRetrievalChain.from_llm(llm, retriever)
|
|
|
74 |
The main function that runs the Streamlit app.
|
75 |
"""
|
76 |
|
77 |
+
# if "messages" not in st.session_state:
|
78 |
+
st.session_state["messages"] = [
|
79 |
+
ChatMessage(role="assistant", content="How can I help you?")
|
80 |
+
]
|
81 |
|
82 |
+
if prompt := st.chat_input(
|
83 |
+
placeholder="Chat with your document",
|
84 |
+
disabled=(not st.session_state.api_key),
|
85 |
+
):
|
86 |
+
st.session_state.messages.append(
|
87 |
+
ChatMessage(
|
88 |
+
role="user",
|
89 |
+
content=prompt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
)
|
91 |
+
)
|
92 |
+
st.chat_message("user").write(prompt)
|
93 |
|
94 |
+
handle_question(prompt)
|
95 |
|
96 |
|
97 |
+
def handle_question(question):
|
98 |
"""
|
99 |
Handles the user's question by generating a response and updating the chat history.
|
100 |
"""
|
101 |
crc = st.session_state.crc
|
102 |
+
|
103 |
if "history" not in st.session_state:
|
104 |
st.session_state["history"] = []
|
105 |
|
106 |
+
response = crc.run(
|
107 |
+
{
|
108 |
+
"question": question,
|
109 |
+
"chat_history": st.session_state["history"],
|
110 |
+
}
|
111 |
+
)
|
|
|
112 |
|
113 |
st.session_state["history"].append((question, response))
|
114 |
|
|
|
118 |
with st.chat_message("assistant"):
|
119 |
stream_handler = StreamHandler(st.empty())
|
120 |
llm = ChatOpenAI(
|
121 |
+
openai_api_key=st.session_state.api_key,
|
122 |
streaming=True,
|
123 |
callbacks=[stream_handler],
|
124 |
)
|
|
|
149 |
del st.session_state["history"]
|
150 |
|
151 |
|
152 |
+
def build_sidebar():
|
153 |
+
with st.sidebar:
|
154 |
+
st.title("π InkChatGPT")
|
155 |
+
st.write("Upload a document and ask questions related to its content.")
|
156 |
+
|
157 |
+
openai_api_key = st.text_input(
|
158 |
+
"OpenAI API Key", type="password", placeholder="Enter your OpenAI API key"
|
159 |
+
)
|
160 |
+
st.session_state.api_key = openai_api_key
|
161 |
+
|
162 |
+
if not openai_api_key:
|
163 |
+
st.info("Please add your OpenAI API key to continue.")
|
164 |
+
|
165 |
+
uploaded_file = st.file_uploader(
|
166 |
+
"Select a file", type=["pdf", "docx", "txt"], key="file_uploader"
|
167 |
+
)
|
168 |
+
|
169 |
+
if uploaded_file and openai_api_key.startswith("sk-"):
|
170 |
+
add_file = st.button(
|
171 |
+
"Process File",
|
172 |
+
on_click=clear_history,
|
173 |
+
key="process_button",
|
174 |
+
)
|
175 |
+
|
176 |
+
if uploaded_file and add_file:
|
177 |
+
with st.spinner("π Thinking..."):
|
178 |
+
vector_store = load_and_process_file(uploaded_file)
|
179 |
+
|
180 |
+
if vector_store:
|
181 |
+
crc = initialize_chat_model(vector_store)
|
182 |
+
st.session_state.crc = crc
|
183 |
+
st.success("File processed successfully!")
|
184 |
+
|
185 |
+
|
186 |
if __name__ == "__main__":
|
187 |
+
build_sidebar()
|
188 |
main()
|