mckplus commited on
Commit
f20077c
1 Parent(s): 5f8cdae

Update DocuChat.py

Browse files
Files changed (1) hide show
  1. 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 ask_question():
39
- os.environ["OPENAI_API_KEY"] = openaikey.value
40
- if file_input.value is not None:
41
- file_input.save("/.cache/temp.pdf")
42
- prompt_text = remove_empty_lines(chatbox.value[-1][chatbox.primary_name])
43
- if prompt_text:
44
- result = qa(file="/.cache/temp.pdf", query=prompt_text)
45
- new_value = chatbox.value.copy()
46
- new_value.append({"AI": result})
47
- chatbox.value = new_value
 
 
 
 
 
 
 
 
48
 
49
- ask_button.on_click(ask_question)
 
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
- chatbox,
59
- ask_button
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