Spaces:
Sleeping
Sleeping
Update DocuChat.py
Browse files- DocuChat.py +2 -22
DocuChat.py
CHANGED
@@ -6,10 +6,6 @@ from langchain.document_loaders import PyPDFLoader
|
|
6 |
from langchain.text_splitter import CharacterTextSplitter
|
7 |
from langchain.embeddings import OpenAIEmbeddings
|
8 |
from langchain.vectorstores import Chroma
|
9 |
-
from langchain.memory import ConversationBufferWindowMemory
|
10 |
-
from langchain.chains import ConversationChain
|
11 |
-
|
12 |
-
|
13 |
import panel as pn
|
14 |
|
15 |
# Set global sizing mode
|
@@ -19,21 +15,11 @@ pn.config.sizing_mode = 'stretch_width'
|
|
19 |
pn.extension()
|
20 |
|
21 |
class LangchainConversation:
|
22 |
-
def __init__(self
|
23 |
self.file_input = pn.widgets.FileInput(height=45)
|
24 |
self.openaikey = pn.widgets.PasswordInput(value="", placeholder="Enter your OpenAI API Key here...", height=45)
|
25 |
self.chatbox = pn.widgets.ChatBox(height=300, primary_name="User")
|
26 |
self.chatbox.param.watch(self._chat, 'value')
|
27 |
-
|
28 |
-
# Initialize memory with k interactions
|
29 |
-
self.memory = ConversationBufferWindowMemory(k=k)
|
30 |
-
|
31 |
-
# Initialize the conversation chain
|
32 |
-
self.chain = ConversationChain(
|
33 |
-
llm=LangchainOpenAI(temperature=0.5),
|
34 |
-
memory=self.memory,
|
35 |
-
verbose=False
|
36 |
-
)
|
37 |
|
38 |
def _chat(self, event):
|
39 |
user_message = event.new[-1]
|
@@ -45,13 +31,7 @@ class LangchainConversation:
|
|
45 |
self.file_input.save("/.cache/temp.pdf")
|
46 |
prompt_text = self.remove_empty_lines(input)
|
47 |
if prompt_text:
|
48 |
-
|
49 |
-
self.memory.save_context({"input": prompt_text}, {"output": ""})
|
50 |
-
|
51 |
-
# Use conversation chain for prediction
|
52 |
-
result = self.chain.predict(input=prompt_text)
|
53 |
-
|
54 |
-
# Append the result to chatbox
|
55 |
self.chatbox.append({"AI": result})
|
56 |
|
57 |
@staticmethod
|
|
|
6 |
from langchain.text_splitter import CharacterTextSplitter
|
7 |
from langchain.embeddings import OpenAIEmbeddings
|
8 |
from langchain.vectorstores import Chroma
|
|
|
|
|
|
|
|
|
9 |
import panel as pn
|
10 |
|
11 |
# Set global sizing mode
|
|
|
15 |
pn.extension()
|
16 |
|
17 |
class LangchainConversation:
|
18 |
+
def __init__(self):
|
19 |
self.file_input = pn.widgets.FileInput(height=45)
|
20 |
self.openaikey = pn.widgets.PasswordInput(value="", placeholder="Enter your OpenAI API Key here...", height=45)
|
21 |
self.chatbox = pn.widgets.ChatBox(height=300, primary_name="User")
|
22 |
self.chatbox.param.watch(self._chat, 'value')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def _chat(self, event):
|
25 |
user_message = event.new[-1]
|
|
|
31 |
self.file_input.save("/.cache/temp.pdf")
|
32 |
prompt_text = self.remove_empty_lines(input)
|
33 |
if prompt_text:
|
34 |
+
result = self.qa(file="/.cache/temp.pdf", query=prompt_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
self.chatbox.append({"AI": result})
|
36 |
|
37 |
@staticmethod
|