Update app.py
Browse files
app.py
CHANGED
@@ -87,14 +87,14 @@ except Exception as e:
|
|
87 |
# RAG process
|
88 |
def rag_process(query, k=2):
|
89 |
if not query.strip() or len(query) < 5:
|
90 |
-
return "Invalid query. Please select a question.",
|
91 |
|
92 |
start_time = time.perf_counter()
|
93 |
try:
|
94 |
query_embedding = embedder.encode([query], show_progress_bar=False)
|
95 |
embed_time = time.perf_counter() - start_time
|
96 |
except Exception as e:
|
97 |
-
return f"Error embedding query: {str(e)}",
|
98 |
|
99 |
start_time = time.perf_counter()
|
100 |
distances, indices = index.search(query_embedding.astype(np.float32), k)
|
@@ -141,7 +141,7 @@ def plot_metrics(metrics):
|
|
141 |
plt.close()
|
142 |
return 'rag_plot.png'
|
143 |
|
144 |
-
# Gradio interface with buttons and single output
|
145 |
def chat_interface(query):
|
146 |
try:
|
147 |
response, retrieved_faqs, metrics = rag_process(query)
|
@@ -167,6 +167,11 @@ body {
|
|
167 |
background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%);
|
168 |
color: #e0e0e0;
|
169 |
font-family: 'Arial', sans-serif;
|
|
|
|
|
|
|
|
|
|
|
170 |
}
|
171 |
.gr-box {
|
172 |
background: #3a3a3a;
|
@@ -180,8 +185,11 @@ body {
|
|
180 |
color: white;
|
181 |
border-radius: 5px;
|
182 |
padding: 10px 20px;
|
183 |
-
margin: 5px;
|
|
|
|
|
184 |
transition: background 0.3s ease;
|
|
|
185 |
}
|
186 |
.gr-button:hover {
|
187 |
background: #1c86ee;
|
@@ -192,22 +200,41 @@ body {
|
|
192 |
color: #e0e0e0;
|
193 |
border: 1px solid #4a4a4a;
|
194 |
border-radius: 5px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
}
|
196 |
#button-container {
|
197 |
display: flex;
|
198 |
-
flex-
|
199 |
gap: 10px;
|
200 |
-
|
201 |
-
|
202 |
-
background: #252525;
|
203 |
border-radius: 8px;
|
204 |
-
|
|
|
205 |
}
|
206 |
#output-container {
|
207 |
background: #303030;
|
208 |
-
padding:
|
209 |
border-radius: 8px;
|
210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
}
|
212 |
"""
|
213 |
|
@@ -215,28 +242,31 @@ body {
|
|
215 |
unique_questions = faq_data['question'].tolist()
|
216 |
|
217 |
with gr.Blocks(css=custom_css) as demo:
|
218 |
-
gr.
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
|
|
|
|
|
|
241 |
|
242 |
demo.launch()
|
|
|
87 |
# RAG process
|
88 |
def rag_process(query, k=2):
|
89 |
if not query.strip() or len(query) < 5:
|
90 |
+
return "Invalid query. Please select a question.", "", "", None
|
91 |
|
92 |
start_time = time.perf_counter()
|
93 |
try:
|
94 |
query_embedding = embedder.encode([query], show_progress_bar=False)
|
95 |
embed_time = time.perf_counter() - start_time
|
96 |
except Exception as e:
|
97 |
+
return f"Error embedding query: {str(e)}", "", "", None
|
98 |
|
99 |
start_time = time.perf_counter()
|
100 |
distances, indices = index.search(query_embedding.astype(np.float32), k)
|
|
|
141 |
plt.close()
|
142 |
return 'rag_plot.png'
|
143 |
|
144 |
+
# Gradio interface with stacked buttons and single output
|
145 |
def chat_interface(query):
|
146 |
try:
|
147 |
response, retrieved_faqs, metrics = rag_process(query)
|
|
|
167 |
background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%);
|
168 |
color: #e0e0e0;
|
169 |
font-family: 'Arial', sans-serif;
|
170 |
+
display: flex;
|
171 |
+
justify-content: center;
|
172 |
+
align-items: center;
|
173 |
+
min-height: 100vh;
|
174 |
+
margin: 0;
|
175 |
}
|
176 |
.gr-box {
|
177 |
background: #3a3a3a;
|
|
|
185 |
color: white;
|
186 |
border-radius: 5px;
|
187 |
padding: 10px 20px;
|
188 |
+
margin: 5px 0;
|
189 |
+
width: 100%;
|
190 |
+
text-align: center;
|
191 |
transition: background 0.3s ease;
|
192 |
+
font-size: 16px;
|
193 |
}
|
194 |
.gr-button:hover {
|
195 |
background: #1c86ee;
|
|
|
200 |
color: #e0e0e0;
|
201 |
border: 1px solid #4a4a4a;
|
202 |
border-radius: 5px;
|
203 |
+
margin-bottom: 10px;
|
204 |
+
font-size: 14px;
|
205 |
+
}
|
206 |
+
#app-container {
|
207 |
+
max-width: 800px;
|
208 |
+
width: 100%;
|
209 |
+
padding: 20px;
|
210 |
+
background: #252525;
|
211 |
+
border-radius: 12px;
|
212 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
|
213 |
}
|
214 |
#button-container {
|
215 |
display: flex;
|
216 |
+
flex-direction: column;
|
217 |
gap: 10px;
|
218 |
+
padding: 15px;
|
219 |
+
background: #303030;
|
|
|
220 |
border-radius: 8px;
|
221 |
+
align-items: center;
|
222 |
+
width: 100%;
|
223 |
}
|
224 |
#output-container {
|
225 |
background: #303030;
|
226 |
+
padding: 15px;
|
227 |
border-radius: 8px;
|
228 |
+
width: 100%;
|
229 |
+
}
|
230 |
+
.text-center {
|
231 |
+
text-align: center;
|
232 |
+
margin-bottom: 20px;
|
233 |
+
}
|
234 |
+
#app-row {
|
235 |
+
display: flex;
|
236 |
+
gap: 20px;
|
237 |
+
justify-content: space-between;
|
238 |
}
|
239 |
"""
|
240 |
|
|
|
242 |
unique_questions = faq_data['question'].tolist()
|
243 |
|
244 |
with gr.Blocks(css=custom_css) as demo:
|
245 |
+
with gr.Column(elem_id="app-container"):
|
246 |
+
gr.Markdown("# Customer Experience Bot Demo", elem_classes="text-center")
|
247 |
+
gr.Markdown("Select a question to see the bot's response, retrieved FAQs, and call center data cleanup stats.", elem_classes="text-center")
|
248 |
+
|
249 |
+
# Layout: outputs on left, buttons on right
|
250 |
+
with gr.Row(elem_id="app-row"):
|
251 |
+
# Single output panel (left 2/3)
|
252 |
+
with gr.Column(elem_id="output-container"):
|
253 |
+
response_output = gr.Textbox(label="Bot Response", elem_id="response-output")
|
254 |
+
faq_output = gr.Textbox(label="Retrieved FAQs", elem_id="faq-output")
|
255 |
+
cleanup_output = gr.Textbox(label="Data Cleanup Stats", elem_id="cleanup-output")
|
256 |
+
plot_output = gr.Image(label="RAG Pipeline Metrics", elem_id="plot-output")
|
257 |
+
|
258 |
+
# Stacked buttons (right 1/3)
|
259 |
+
with gr.Column(elem_id="button-container"):
|
260 |
+
for question in unique_questions:
|
261 |
+
gr.Button(question).click(
|
262 |
+
fn=chat_interface,
|
263 |
+
inputs=gr.State(value=question),
|
264 |
+
outputs=[
|
265 |
+
response_output,
|
266 |
+
faq_output,
|
267 |
+
cleanup_output,
|
268 |
+
plot_output
|
269 |
+
]
|
270 |
+
)
|
271 |
|
272 |
demo.launch()
|