Spaces:
Sleeping
Sleeping
Update DocuChat.py
Browse files- DocuChat.py +51 -52
DocuChat.py
CHANGED
@@ -8,60 +8,59 @@ from langchain.embeddings import OpenAIEmbeddings
|
|
8 |
from langchain.vectorstores import Chroma
|
9 |
import panel as pn
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
pn.
|
16 |
-
|
17 |
-
file_input = pn.widgets.FileInput(height=45)
|
18 |
-
openaikey = pn.widgets.PasswordInput(value="", placeholder="Enter your OpenAI API Key here...", height=45)
|
19 |
-
chatbox = pn.widgets.ChatBox(height=300, primary_name="User")
|
20 |
|
21 |
-
def remove_empty_lines(text):
|
22 |
-
|
23 |
-
|
24 |
|
25 |
-
def qa(file, query):
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
def qa_result(event):
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
print("Debugging: Appended result to chatbox") # Debugging Line 4
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
# DocuChat
|
62 |
-
AI-Powered Query Engine for Document Insights (powered by LangChain & OpenAI)
|
63 |
-
...
|
64 |
-
"""),
|
65 |
-
pn.Row(file_input, openaikey), chatbox
|
66 |
-
).servable()
|
67 |
-
print("Debugging: Application is ready") # Debugging Line 4
|
|
|
8 |
from langchain.vectorstores import Chroma
|
9 |
import panel as pn
|
10 |
|
11 |
+
class LangChainConversation:
|
12 |
+
def __init__(self):
|
13 |
+
self.file_input = pn.widgets.FileInput(height=45)
|
14 |
+
self.openaikey = pn.widgets.PasswordInput(value="", placeholder="Enter your OpenAI API Key here...", height=45)
|
15 |
+
self.chatbox = pn.widgets.ChatBox(height=300, primary_name="User")
|
16 |
+
self.chatbox.param.watch(self.qa_result, 'value')
|
|
|
|
|
|
|
17 |
|
18 |
+
def remove_empty_lines(self, text):
|
19 |
+
lines = re.split(r'\r\n|\r|\n', text)
|
20 |
+
return '\n'.join([line.strip() for line in lines if line.strip()])
|
21 |
|
22 |
+
def qa(self, file, query):
|
23 |
+
loader = PyPDFLoader(file)
|
24 |
+
documents = loader.load()
|
25 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
26 |
+
texts = text_splitter.split_documents(documents)
|
27 |
+
embeddings = OpenAIEmbeddings()
|
28 |
+
db = Chroma.from_documents(texts, embeddings)
|
29 |
+
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 3})
|
30 |
+
qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=retriever, return_source_documents=True)
|
31 |
+
result = qa({"query": query})
|
32 |
+
return result['result']
|
33 |
|
34 |
+
def qa_result(self, event):
|
35 |
+
print("Debugging: Event triggered")
|
36 |
+
if len(event.new) > len(event.old):
|
37 |
+
os.environ["OPENAI_API_KEY"] = self.openaikey.value
|
38 |
+
if self.file_input.value is not None:
|
39 |
+
self.file_input.save("/.cache/temp.pdf")
|
40 |
+
prompt_text = self.remove_empty_lines(event.new[-1][self.chatbox.primary_name])
|
41 |
+
print("Debugging: Prompt text:", prompt_text)
|
42 |
+
if prompt_text:
|
43 |
+
print("Debugging: Calling Langchain")
|
44 |
+
result = self.qa(file="/.cache/temp.pdf", query=prompt_text)
|
45 |
+
print("Debugging: Result from Langchain:", result)
|
46 |
+
# Append the AI's response to the chatbox
|
47 |
+
new_value = self.chatbox.value.copy()
|
48 |
+
new_value.append({"AI": result})
|
49 |
+
self.chatbox.value = new_value
|
50 |
+
print("Debugging: Appended result to chatbox")
|
|
|
51 |
|
52 |
+
def view(self):
|
53 |
+
layout = pn.Column(
|
54 |
+
pn.pane.Markdown("""
|
55 |
+
# DocuChat
|
56 |
+
AI-Powered Query Engine for Document Insights (powered by LangChain & OpenAI)
|
57 |
+
...
|
58 |
+
"""),
|
59 |
+
pn.Row(self.file_input, self.openaikey), self.chatbox
|
60 |
+
)
|
61 |
+
return layout.servable()
|
62 |
|
63 |
+
pn.config.sizing_mode = 'stretch_width'
|
64 |
+
pn.extension()
|
65 |
+
chat = LangChainConversation()
|
66 |
+
chat.view()
|
|
|
|
|
|
|
|
|
|
|
|
|
|