Nischal Subedi
commited on
Commit
·
2b34827
1
Parent(s):
f8a1cae
UI update
Browse files
app.py
CHANGED
@@ -244,9 +244,10 @@ Answer:"""
|
|
244 |
|
245 |
|
246 |
|
|
|
|
|
247 |
def gradio_interface(self):
|
248 |
def query_interface_wrapper(api_key: str, query: str, state: str) -> str:
|
249 |
-
# Basic client-side validation for immediate feedback (redundant but good UX)
|
250 |
if not api_key or not api_key.strip() or not api_key.startswith("sk-"):
|
251 |
return "<div class='error-message'><span class='error-icon'>⚠️</span>Please provide a valid OpenAI API key (starting with 'sk-'). <a href='https://platform.openai.com/api-keys' target='_blank'>OpenAI</a>.</div>"
|
252 |
if not state or state is None:
|
@@ -254,11 +255,9 @@ Answer:"""
|
|
254 |
if not query or not query.strip():
|
255 |
return "<div class='error-message'><span class='error-icon'>⚠️</span>Please enter your question in the text box.</div>"
|
256 |
|
257 |
-
# Call the core processing logic
|
258 |
result = self.process_query(query=query, state=state, openai_api_key=api_key)
|
259 |
answer = result.get("answer", "<div class='error-message'><span class='error-icon'>⚠️</span>An unexpected error occurred.</div>")
|
260 |
|
261 |
-
# Check if the answer already contains an error message
|
262 |
if "<div class='error-message'>" in answer:
|
263 |
return answer
|
264 |
else:
|
@@ -292,34 +291,30 @@ Answer:"""
|
|
292 |
|
293 |
custom_css = """
|
294 |
/* Import modern fonts */
|
295 |
-
@import url('https://fonts.googleapis.com/css2?family=
|
296 |
|
297 |
/* Define custom properties for light and dark themes */
|
298 |
:root {
|
299 |
-
--background-primary: #
|
300 |
-
--background-secondary: #
|
301 |
-
--text-primary: #
|
302 |
-
--text-secondary: #
|
303 |
-
--
|
304 |
-
--
|
305 |
-
--accent-hover: #234E52;
|
306 |
-
--border-color: #E2E8F0;
|
307 |
--shadow-sm: 0 2px 4px rgba(0,0,0,0.05);
|
308 |
--shadow-md: 0 4px 12px rgba(0,0,0,0.1);
|
309 |
-
--error-bg: #
|
310 |
-
--error-text: #
|
311 |
}
|
312 |
|
313 |
-
/* Dark theme variables */
|
314 |
@media (prefers-color-scheme: dark) {
|
315 |
:root {
|
316 |
-
--background-primary: #
|
317 |
-
--background-secondary: #
|
318 |
-
--text-primary: #
|
319 |
--text-secondary: #A0AEC0;
|
320 |
-
--
|
321 |
-
--
|
322 |
-
--error-bg: #7F1D1D;
|
323 |
}
|
324 |
}
|
325 |
|
@@ -327,7 +322,7 @@ Answer:"""
|
|
327 |
html, body, .gradio-container {
|
328 |
background-color: var(--background-primary) !important;
|
329 |
color: var(--text-primary) !important;
|
330 |
-
font-family: '
|
331 |
margin: 0 !important;
|
332 |
padding: 0 !important;
|
333 |
}
|
@@ -337,7 +332,6 @@ Answer:"""
|
|
337 |
margin: 2rem auto !important;
|
338 |
padding: 2rem !important;
|
339 |
border-radius: 12px !important;
|
340 |
-
background-color: var(--background-primary) !important;
|
341 |
box-shadow: var(--shadow-md) !important;
|
342 |
}
|
343 |
|
@@ -348,57 +342,39 @@ Answer:"""
|
|
348 |
border-radius: 12px !important;
|
349 |
padding: 2rem !important;
|
350 |
margin-bottom: 2rem !important;
|
351 |
-
position: relative;
|
352 |
text-align: center !important;
|
353 |
box-shadow: var(--shadow-sm) !important;
|
354 |
-
|
355 |
-
}
|
356 |
-
|
357 |
-
.app-header-wrapper::before {
|
358 |
-
content: '';
|
359 |
-
position: absolute;
|
360 |
-
top: 0;
|
361 |
-
left: 0;
|
362 |
-
width: 100%;
|
363 |
-
height: 100%;
|
364 |
-
background: radial-gradient(circle at top left, rgba(44, 122, 123, 0.2), transparent 70%);
|
365 |
-
z-index: 0;
|
366 |
-
opacity: 0.8;
|
367 |
}
|
368 |
|
369 |
.app-header-logo {
|
370 |
font-size: 5rem !important;
|
371 |
color: var(--accent-primary) !important;
|
372 |
margin-bottom: 1rem !important;
|
373 |
-
|
374 |
-
z-index: 1;
|
375 |
-
animation: bounce 2s infinite ease-in-out;
|
376 |
}
|
377 |
|
378 |
-
@keyframes
|
379 |
-
0
|
380 |
-
50% { transform:
|
|
|
381 |
}
|
382 |
|
383 |
.app-header-title {
|
384 |
-
font-family: '
|
385 |
-
font-size: 2.
|
386 |
font-weight: 700 !important;
|
387 |
-
color: var(--
|
388 |
margin: 0 !important;
|
389 |
line-height: 1.2 !important;
|
390 |
-
text-shadow: 1px 1px
|
391 |
-
position: relative;
|
392 |
-
z-index: 1;
|
393 |
}
|
394 |
|
395 |
.app-header-tagline {
|
396 |
-
font-size: 1.
|
397 |
color: var(--text-secondary) !important;
|
398 |
margin-top: 0.5rem !important;
|
399 |
font-weight: 400 !important;
|
400 |
-
position: relative;
|
401 |
-
z-index: 1;
|
402 |
}
|
403 |
|
404 |
/* Main Dashboard Layout */
|
@@ -431,7 +407,7 @@ Answer:"""
|
|
431 |
}
|
432 |
|
433 |
.section-title {
|
434 |
-
font-family: '
|
435 |
font-size: 1.6rem !important;
|
436 |
font-weight: 600 !important;
|
437 |
color: var(--text-primary) !important;
|
@@ -462,7 +438,7 @@ Answer:"""
|
|
462 |
.gradio-textbox textarea:focus,
|
463 |
.gradio-textbox input:focus {
|
464 |
border-color: var(--accent-primary) !important;
|
465 |
-
box-shadow: 0 0 0 3px rgba(
|
466 |
outline: none !important;
|
467 |
}
|
468 |
|
@@ -485,7 +461,7 @@ Answer:"""
|
|
485 |
.gradio-radio input[type="radio"]:checked + label {
|
486 |
background-color: var(--accent-primary) !important;
|
487 |
color: white !important;
|
488 |
-
border-color: var(--accent-
|
489 |
}
|
490 |
|
491 |
.gradio-button {
|
@@ -503,7 +479,7 @@ Answer:"""
|
|
503 |
}
|
504 |
|
505 |
.gr-button-primary:hover {
|
506 |
-
background-color:
|
507 |
transform: translateY(-2px) !important;
|
508 |
}
|
509 |
|
@@ -621,9 +597,8 @@ Answer:"""
|
|
621 |
}
|
622 |
"""
|
623 |
|
624 |
-
# Use a professional Gradio theme with custom hues
|
625 |
custom_theme = gr.themes.Soft(
|
626 |
-
primary_hue="
|
627 |
secondary_hue="gray",
|
628 |
neutral_hue="stone"
|
629 |
)
|
@@ -640,7 +615,6 @@ Answer:"""
|
|
640 |
)
|
641 |
|
642 |
with gr.Column(elem_classes="main-dashboard-container"):
|
643 |
-
# How This Assistant Works Box
|
644 |
with gr.Group(elem_classes="dashboard-card-section"):
|
645 |
gr.Markdown("<h3 class='section-title'>How This Assistant Works</h3>", elem_classes="full-width-center section-title-gradient-bar")
|
646 |
with gr.Column(elem_classes="dashboard-card-content-area"):
|
@@ -650,7 +624,6 @@ Answer:"""
|
|
650 |
"""
|
651 |
)
|
652 |
|
653 |
-
# OpenAI API Key Input Card
|
654 |
with gr.Group(elem_classes="dashboard-card-section"):
|
655 |
gr.Markdown("<h3 class='section-title'>OpenAI API Key</h3>", elem_classes="full-width-center section-title-gradient-bar")
|
656 |
with gr.Column(elem_classes="dashboard-card-content-area"):
|
@@ -666,7 +639,6 @@ Answer:"""
|
|
666 |
elem_classes="gr-prose"
|
667 |
)
|
668 |
|
669 |
-
# Query Input and State Selection Card
|
670 |
with gr.Group(elem_classes="dashboard-card-section"):
|
671 |
gr.Markdown("<h3 class='section-title'>Ask Your Question</h3>", elem_classes="full-width-center section-title-gradient-bar")
|
672 |
with gr.Column(elem_classes="dashboard-card-content-area"):
|
@@ -691,7 +663,6 @@ Answer:"""
|
|
691 |
clear_button = gr.Button("Clear", variant="secondary", elem_classes=["gr-button-secondary"])
|
692 |
submit_button = gr.Button("Submit Query", variant="primary", elem_classes=["gr-button-primary"])
|
693 |
|
694 |
-
# Output Display Card
|
695 |
with gr.Group(elem_classes="dashboard-card-section"):
|
696 |
gr.Markdown("<h3 class='section-title'>Legal Assistant's Response</h3>", elem_classes="full-width-center section-title-gradient-bar")
|
697 |
with gr.Column(elem_classes="dashboard-card-content-area"):
|
@@ -700,7 +671,6 @@ Answer:"""
|
|
700 |
elem_classes="output-content-wrapper"
|
701 |
)
|
702 |
|
703 |
-
# Example Questions Section
|
704 |
with gr.Group(elem_classes="dashboard-card-section examples-section"):
|
705 |
gr.Markdown("<h3 class='section-title'>Example Questions</h3>", elem_classes="full-width-center section-title-gradient-bar")
|
706 |
with gr.Column(elem_classes="dashboard-card-content-area"):
|
@@ -714,7 +684,6 @@ Answer:"""
|
|
714 |
else:
|
715 |
gr.Markdown("<div class='placeholder'>Sample questions could not be loaded. Please ensure the vector database is populated.</div>")
|
716 |
|
717 |
-
# Footer Section
|
718 |
with gr.Group(elem_classes="app-footer-wrapper"):
|
719 |
gr.Markdown(
|
720 |
f"""
|
|
|
244 |
|
245 |
|
246 |
|
247 |
+
import gradio as gr
|
248 |
+
|
249 |
def gradio_interface(self):
|
250 |
def query_interface_wrapper(api_key: str, query: str, state: str) -> str:
|
|
|
251 |
if not api_key or not api_key.strip() or not api_key.startswith("sk-"):
|
252 |
return "<div class='error-message'><span class='error-icon'>⚠️</span>Please provide a valid OpenAI API key (starting with 'sk-'). <a href='https://platform.openai.com/api-keys' target='_blank'>OpenAI</a>.</div>"
|
253 |
if not state or state is None:
|
|
|
255 |
if not query or not query.strip():
|
256 |
return "<div class='error-message'><span class='error-icon'>⚠️</span>Please enter your question in the text box.</div>"
|
257 |
|
|
|
258 |
result = self.process_query(query=query, state=state, openai_api_key=api_key)
|
259 |
answer = result.get("answer", "<div class='error-message'><span class='error-icon'>⚠️</span>An unexpected error occurred.</div>")
|
260 |
|
|
|
261 |
if "<div class='error-message'>" in answer:
|
262 |
return answer
|
263 |
else:
|
|
|
291 |
|
292 |
custom_css = """
|
293 |
/* Import modern fonts */
|
294 |
+
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&family=Open+Sans:wght@600;700;800&display=swap');
|
295 |
|
296 |
/* Define custom properties for light and dark themes */
|
297 |
:root {
|
298 |
+
--background-primary: #F7F7F7;
|
299 |
+
--background-secondary: #EDF2F7;
|
300 |
+
--text-primary: #1A1A1A;
|
301 |
+
--text-secondary: #4A5568;
|
302 |
+
--accent-primary: #38A169;
|
303 |
+
--border-color: #CBD5E0;
|
|
|
|
|
304 |
--shadow-sm: 0 2px 4px rgba(0,0,0,0.05);
|
305 |
--shadow-md: 0 4px 12px rgba(0,0,0,0.1);
|
306 |
+
--error-bg: #FEF2F2;
|
307 |
+
--error-text: #E53E3E;
|
308 |
}
|
309 |
|
|
|
310 |
@media (prefers-color-scheme: dark) {
|
311 |
:root {
|
312 |
+
--background-primary: #2D3748;
|
313 |
+
--background-secondary: #4A5568;
|
314 |
+
--text-primary: #FFFFFF;
|
315 |
--text-secondary: #A0AEC0;
|
316 |
+
--border-color: #718096;
|
317 |
+
--error-bg: #63171B;
|
|
|
318 |
}
|
319 |
}
|
320 |
|
|
|
322 |
html, body, .gradio-container {
|
323 |
background-color: var(--background-primary) !important;
|
324 |
color: var(--text-primary) !important;
|
325 |
+
font-family: 'Roboto', sans-serif !important;
|
326 |
margin: 0 !important;
|
327 |
padding: 0 !important;
|
328 |
}
|
|
|
332 |
margin: 2rem auto !important;
|
333 |
padding: 2rem !important;
|
334 |
border-radius: 12px !important;
|
|
|
335 |
box-shadow: var(--shadow-md) !important;
|
336 |
}
|
337 |
|
|
|
342 |
border-radius: 12px !important;
|
343 |
padding: 2rem !important;
|
344 |
margin-bottom: 2rem !important;
|
|
|
345 |
text-align: center !important;
|
346 |
box-shadow: var(--shadow-sm) !important;
|
347 |
+
position: relative;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
348 |
}
|
349 |
|
350 |
.app-header-logo {
|
351 |
font-size: 5rem !important;
|
352 |
color: var(--accent-primary) !important;
|
353 |
margin-bottom: 1rem !important;
|
354 |
+
animation: pulse 2s infinite ease-in-out;
|
|
|
|
|
355 |
}
|
356 |
|
357 |
+
@keyframes pulse {
|
358 |
+
0% { transform: scale(1); }
|
359 |
+
50% { transform: scale(1.1); }
|
360 |
+
100% { transform: scale(1); }
|
361 |
}
|
362 |
|
363 |
.app-header-title {
|
364 |
+
font-family: 'Open Sans', sans-serif !important;
|
365 |
+
font-size: 2.5rem !important;
|
366 |
font-weight: 700 !important;
|
367 |
+
color: var(--text-primary) !important;
|
368 |
margin: 0 !important;
|
369 |
line-height: 1.2 !important;
|
370 |
+
text-shadow: 1px 1px 3px rgba(0,0,0,0.1) !important;
|
|
|
|
|
371 |
}
|
372 |
|
373 |
.app-header-tagline {
|
374 |
+
font-size: 1.1rem !important;
|
375 |
color: var(--text-secondary) !important;
|
376 |
margin-top: 0.5rem !important;
|
377 |
font-weight: 400 !important;
|
|
|
|
|
378 |
}
|
379 |
|
380 |
/* Main Dashboard Layout */
|
|
|
407 |
}
|
408 |
|
409 |
.section-title {
|
410 |
+
font-family: 'Open Sans', sans-serif !important;
|
411 |
font-size: 1.6rem !important;
|
412 |
font-weight: 600 !important;
|
413 |
color: var(--text-primary) !important;
|
|
|
438 |
.gradio-textbox textarea:focus,
|
439 |
.gradio-textbox input:focus {
|
440 |
border-color: var(--accent-primary) !important;
|
441 |
+
box-shadow: 0 0 0 3px rgba(56, 161, 105, 0.2) !important;
|
442 |
outline: none !important;
|
443 |
}
|
444 |
|
|
|
461 |
.gradio-radio input[type="radio"]:checked + label {
|
462 |
background-color: var(--accent-primary) !important;
|
463 |
color: white !important;
|
464 |
+
border-color: var(--accent-primary) !important;
|
465 |
}
|
466 |
|
467 |
.gradio-button {
|
|
|
479 |
}
|
480 |
|
481 |
.gr-button-primary:hover {
|
482 |
+
background-color: #2F855A !important; /* Darker green */
|
483 |
transform: translateY(-2px) !important;
|
484 |
}
|
485 |
|
|
|
597 |
}
|
598 |
"""
|
599 |
|
|
|
600 |
custom_theme = gr.themes.Soft(
|
601 |
+
primary_hue="green",
|
602 |
secondary_hue="gray",
|
603 |
neutral_hue="stone"
|
604 |
)
|
|
|
615 |
)
|
616 |
|
617 |
with gr.Column(elem_classes="main-dashboard-container"):
|
|
|
618 |
with gr.Group(elem_classes="dashboard-card-section"):
|
619 |
gr.Markdown("<h3 class='section-title'>How This Assistant Works</h3>", elem_classes="full-width-center section-title-gradient-bar")
|
620 |
with gr.Column(elem_classes="dashboard-card-content-area"):
|
|
|
624 |
"""
|
625 |
)
|
626 |
|
|
|
627 |
with gr.Group(elem_classes="dashboard-card-section"):
|
628 |
gr.Markdown("<h3 class='section-title'>OpenAI API Key</h3>", elem_classes="full-width-center section-title-gradient-bar")
|
629 |
with gr.Column(elem_classes="dashboard-card-content-area"):
|
|
|
639 |
elem_classes="gr-prose"
|
640 |
)
|
641 |
|
|
|
642 |
with gr.Group(elem_classes="dashboard-card-section"):
|
643 |
gr.Markdown("<h3 class='section-title'>Ask Your Question</h3>", elem_classes="full-width-center section-title-gradient-bar")
|
644 |
with gr.Column(elem_classes="dashboard-card-content-area"):
|
|
|
663 |
clear_button = gr.Button("Clear", variant="secondary", elem_classes=["gr-button-secondary"])
|
664 |
submit_button = gr.Button("Submit Query", variant="primary", elem_classes=["gr-button-primary"])
|
665 |
|
|
|
666 |
with gr.Group(elem_classes="dashboard-card-section"):
|
667 |
gr.Markdown("<h3 class='section-title'>Legal Assistant's Response</h3>", elem_classes="full-width-center section-title-gradient-bar")
|
668 |
with gr.Column(elem_classes="dashboard-card-content-area"):
|
|
|
671 |
elem_classes="output-content-wrapper"
|
672 |
)
|
673 |
|
|
|
674 |
with gr.Group(elem_classes="dashboard-card-section examples-section"):
|
675 |
gr.Markdown("<h3 class='section-title'>Example Questions</h3>", elem_classes="full-width-center section-title-gradient-bar")
|
676 |
with gr.Column(elem_classes="dashboard-card-content-area"):
|
|
|
684 |
else:
|
685 |
gr.Markdown("<div class='placeholder'>Sample questions could not be loaded. Please ensure the vector database is populated.</div>")
|
686 |
|
|
|
687 |
with gr.Group(elem_classes="app-footer-wrapper"):
|
688 |
gr.Markdown(
|
689 |
f"""
|