HonestGrad commited on
Commit
c745c24
·
verified ·
1 Parent(s): 32afde6

Update app.py

Browse files

Fix messages in chat

Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -55,17 +55,17 @@ def get_llm_answer(pdf_text, question, history):
55
  if they have serious concerns whenever appropriate.'''
56
 
57
  # Construct the message payload for the API.
58
- messages = [{"role": "system", "content": system_prompt}]
59
-
60
- # Add document context if available.
61
- if context:
62
- messages.append({"role": "user", "content": f"Use the following document to answer my question:\n\n{context}"})
63
 
64
  # Add the conversation history.
65
  if history:
66
- messages.extend(history)
67
-
68
- # Add the latest user question.
 
69
  messages.append({"role": "user", "content": question})
70
 
71
  try:
@@ -103,16 +103,22 @@ class PDFChatbot:
103
  return f"Status: Successfully processed {self.pdf_filename}. You can now ask questions."
104
 
105
  def chat(self, user_message, history):
106
- if not self.pdf_text:
107
  # Add an instruction to the chatbot window if no PDF is uploaded.
108
- history.append([user_message, "Please upload a PDF document first."])
109
- return "", history
110
-
 
 
 
111
  # Get the answer from the LLM.
112
- answer = get_llm_answer(self.pdf_text, user_message, history)
113
 
114
  # Append the user message and the assistant's answer to the history.
115
- history.append([user_message, answer])
 
 
 
116
 
117
  # Return an empty string to clear the input textbox and the updated history.
118
  return "", history
@@ -131,7 +137,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
131
  upload_status = gr.Textbox(label="Status", interactive=False, value="Status: Waiting for PDF...")
132
 
133
  with gr.Column(scale=2):
134
- chatbot = gr.Chatbot(label="Chat History", bubble_full_width=False, height=500)
135
  msg_textbox = gr.Textbox(label="Your Question:", interactive=True, placeholder="Type your question here...")
136
  # Clear button is useful for starting a new conversation.
137
  clear_btn = gr.ClearButton([msg_textbox, chatbot], value="Clear Chat")
 
55
  if they have serious concerns whenever appropriate.'''
56
 
57
  # Construct the message payload for the API.
58
+ messages = [{"role": "system", "content": system_prompt},
59
+ {"role": "user", "content": f"Use the following document to answer my question:\n\n{context}"},
60
+ {"role":"user", "content": f"Question: {question}"}
61
+ ]
 
62
 
63
  # Add the conversation history.
64
  if history:
65
+ for msg in history:
66
+ if msg["role"] in ["user", "assistant"]:
67
+ messages.append(msg)
68
+ # Add the new user question
69
  messages.append({"role": "user", "content": question})
70
 
71
  try:
 
103
  return f"Status: Successfully processed {self.pdf_filename}. You can now ask questions."
104
 
105
  def chat(self, user_message, history):
106
+ if self.pdf_text is None:
107
  # Add an instruction to the chatbot window if no PDF is uploaded.
108
+ #history.append([user_message, "Please upload a PDF document first."])
109
+ return "Please upload a PDF document first.", history
110
+
111
+ if history is None:
112
+ history = []
113
+ context_history = [msg for msg in history if msg["role"] in ["user", "assistant"]]
114
  # Get the answer from the LLM.
115
+ answer = get_llm_answer(self.pdf_text, user_message, context_history)
116
 
117
  # Append the user message and the assistant's answer to the history.
118
+ history = history + [
119
+ {"role": "user", "content": user_message },
120
+ {"role": "assistant", "content": answer }
121
+ ]
122
 
123
  # Return an empty string to clear the input textbox and the updated history.
124
  return "", history
 
137
  upload_status = gr.Textbox(label="Status", interactive=False, value="Status: Waiting for PDF...")
138
 
139
  with gr.Column(scale=2):
140
+ chatbot = gr.Chatbot(type="messages", label="Chat History", height=500)
141
  msg_textbox = gr.Textbox(label="Your Question:", interactive=True, placeholder="Type your question here...")
142
  # Clear button is useful for starting a new conversation.
143
  clear_btn = gr.ClearButton([msg_textbox, chatbot], value="Clear Chat")