SuriRaja commited on
Commit
a7b77e7
·
verified ·
1 Parent(s): 9df19db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -23
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  import logging
3
  import os
@@ -7,7 +8,6 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
7
  import faiss
8
  from simple_salesforce import Salesforce
9
  from dotenv import load_dotenv
10
- import json
11
  import zipfile
12
  from pathlib import Path
13
 
@@ -181,17 +181,20 @@ def answer_query(query):
181
  logger.error(f"Error in answer_query: {str(e)}")
182
  return f"Error: {str(e)}", "", "", ""
183
 
184
- # --- Gradio Chatbot UI ---
185
- def process_question(q):
186
  if not q.strip():
187
- return "Please enter a question.", "", ""
188
 
189
  answer, confidence, source, record_id = answer_query(q)
190
- return answer, confidence, source, record_id
 
 
 
191
 
192
- # --- Chatbot UI Design (Chat bubble style) ---
193
  with gr.Blocks(title="Company Documents Q&A Chatbot", theme=gr.themes.Soft()) as demo:
194
- gr.Markdown("## 📚 Company Documents Q&A Chatbot")
195
 
196
  with gr.Row():
197
  with gr.Column(scale=3):
@@ -199,27 +202,25 @@ with gr.Blocks(title="Company Documents Q&A Chatbot", theme=gr.themes.Soft()) as
199
  label="Ask a Question",
200
  placeholder="What are the conditions for permanent employment status?",
201
  lines=1,
202
- interactive=True
 
 
203
  )
204
  with gr.Column(scale=1):
205
- submit_btn = gr.Button("Submit", variant="primary")
206
 
207
  with gr.Row():
208
  with gr.Column():
209
- # Chatbot styled for a more modern chat look with bubbles
210
- output_area = gr.HTML(
211
- label="Chat",
212
  elem_id="chatbox",
213
- value="""
214
- <div style="padding: 10px; background-color: #f5f5f5; border-radius: 10px;">
215
- <div style="padding: 5px 10px; background-color: #dfe1e6; border-radius: 10px; margin-bottom: 10px;">
216
- <b>User:</b> <span id="user-message"> </span>
217
- </div>
218
- <div style="padding: 5px 10px; background-color: #007bff; color: white; border-radius: 10px;">
219
- <b>Bot:</b> <span id="bot-message"> </span>
220
- </div>
221
- </div>""")
222
-
223
- submit_btn.click(fn=process_question, inputs=question, outputs=[output_area])
224
 
225
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
 
1
+
2
  import gradio as gr
3
  import logging
4
  import os
 
8
  import faiss
9
  from simple_salesforce import Salesforce
10
  from dotenv import load_dotenv
 
11
  import zipfile
12
  from pathlib import Path
13
 
 
181
  logger.error(f"Error in answer_query: {str(e)}")
182
  return f"Error: {str(e)}", "", "", ""
183
 
184
+ # --- Gradio Chatbot UI Design ---
185
+ def process_question(q, chat_history):
186
  if not q.strip():
187
+ return chat_history + [("User", "Please enter a question.")], "", ""
188
 
189
  answer, confidence, source, record_id = answer_query(q)
190
+ chat_history.append(("User", q))
191
+ chat_history.append(("Bot", answer))
192
+
193
+ return chat_history, confidence, source, record_id
194
 
195
+ # --- Chatbot UI with dynamic styling using elem_id ---
196
  with gr.Blocks(title="Company Documents Q&A Chatbot", theme=gr.themes.Soft()) as demo:
197
+ gr.Markdown("## 📚 **Company Policies Q&A Chatbot**")
198
 
199
  with gr.Row():
200
  with gr.Column(scale=3):
 
202
  label="Ask a Question",
203
  placeholder="What are the conditions for permanent employment status?",
204
  lines=1,
205
+ interactive=True,
206
+ elem_id="user-question",
207
+ visible=True
208
  )
209
  with gr.Column(scale=1):
210
+ submit_btn = gr.Button("Submit", variant="primary", elem_id="submit-btn")
211
 
212
  with gr.Row():
213
  with gr.Column():
214
+ chat_history = gr.Chatbot(
215
+ label="Chat History",
 
216
  elem_id="chatbox",
217
+ height=400, # Set a fixed height
218
+ show_label=False # Hide the label to make the chat more clean
219
+ )
220
+ conf_out = gr.Markdown(label="Confidence", elem_id="confidence")
221
+ source_out = gr.Markdown(label="Source Link", elem_id="source-link")
222
+ record_out = gr.Markdown(label="Salesforce Record ID", elem_id="salesforce-id")
223
+
224
+ submit_btn.click(fn=process_question, inputs=[question, chat_history], outputs=[chat_history, conf_out, source_out, record_out])
 
 
 
225
 
226
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)