Spaces:
Running
Running
Update DocuChat.py
Browse files- DocuChat.py +23 -16
DocuChat.py
CHANGED
@@ -17,7 +17,6 @@ pn.extension()
|
|
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 |
-
ask_button = pn.widgets.Button(name="Ask", width=100)
|
21 |
|
22 |
def remove_empty_lines(text):
|
23 |
lines = re.split(r'\r\n|\r|\n', text)
|
@@ -35,18 +34,27 @@ def qa(file, query):
|
|
35 |
result = qa({"query": query})
|
36 |
return result['result']
|
37 |
|
38 |
-
def
|
39 |
-
|
40 |
-
if
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
|
|
50 |
|
51 |
layout = pn.Column(
|
52 |
pn.pane.Markdown("""
|
@@ -54,7 +62,6 @@ layout = pn.Column(
|
|
54 |
AI-Powered Query Engine for Document Insights (powered by LangChain & OpenAI)
|
55 |
...
|
56 |
"""),
|
57 |
-
pn.Row(file_input, openaikey),
|
58 |
-
|
59 |
-
|
60 |
-
).servable()
|
|
|
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 |
lines = re.split(r'\r\n|\r|\n', text)
|
|
|
34 |
result = qa({"query": query})
|
35 |
return result['result']
|
36 |
|
37 |
+
def qa_result(event):
|
38 |
+
print("Debugging: Event triggered") # Debugging Line 0
|
39 |
+
if len(event.new) > len(event.old):
|
40 |
+
os.environ["OPENAI_API_KEY"] = openaikey.value
|
41 |
+
if file_input.value is not None:
|
42 |
+
file_input.save("/.cache/temp.pdf")
|
43 |
+
prompt_text = remove_empty_lines(event.new[-1][chatbox.primary_name])
|
44 |
+
print("Debugging: Prompt text:", prompt_text) # Debugging Line 1
|
45 |
+
print("Debugging: Chat input from user:", event.new[-1][chatbox.primary_name]) # Debugging Line 1.1
|
46 |
+
if prompt_text:
|
47 |
+
print("Debugging: Calling Langchain") # Debugging Line 2
|
48 |
+
result = qa(file="/.cache/temp.pdf", query=prompt_text)
|
49 |
+
print("Debugging: Result from Langchain:", result) # Debugging Line 3
|
50 |
+
# Append the AI's response to the chatbox
|
51 |
+
new_value = chatbox.value.copy() # Copy the existing value
|
52 |
+
new_value.append({"AI": result}) # Append the AI's response
|
53 |
+
chatbox.value = new_value # Assign the new value
|
54 |
+
print("Debugging: Appended result to chatbox") # Debugging Line 4
|
55 |
|
56 |
+
|
57 |
+
chatbox.param.watch(qa_result, 'value')
|
58 |
|
59 |
layout = pn.Column(
|
60 |
pn.pane.Markdown("""
|
|
|
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
|
|