Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ if not api_key:
|
|
| 20 |
# Initialize OpenAI client with older API syntax
|
| 21 |
openai.api_key = api_key
|
| 22 |
|
| 23 |
-
# Custom CSS for a
|
| 24 |
custom_css = """
|
| 25 |
html, body, .gradio-container {
|
| 26 |
height: 100vh !important;
|
|
@@ -28,74 +28,124 @@ html, body, .gradio-container {
|
|
| 28 |
max-width: 100vw !important;
|
| 29 |
margin: 0 !important;
|
| 30 |
padding: 0 !important;
|
| 31 |
-
font-family: 'Inter', sans-serif;
|
| 32 |
-
background: #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
.compact-box {
|
| 36 |
background: #fff;
|
| 37 |
-
border-radius:
|
| 38 |
-
box-shadow: 0
|
| 39 |
-
padding:
|
| 40 |
-
margin-bottom:
|
| 41 |
width: 100%;
|
| 42 |
-
max-width:
|
| 43 |
margin-left: auto;
|
| 44 |
margin-right: auto;
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
.section-title {
|
| 48 |
-
font-size: 1.
|
| 49 |
-
font-weight:
|
| 50 |
-
margin-bottom:
|
| 51 |
-
color: #
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
-
.upload-btn, .send-btn, .audio-btn {
|
| 55 |
-
background: linear-gradient(135deg, #
|
| 56 |
color: white;
|
| 57 |
border: none;
|
| 58 |
-
padding:
|
| 59 |
-
border-radius:
|
| 60 |
cursor: pointer;
|
| 61 |
font-weight: 600;
|
| 62 |
-
font-size:
|
| 63 |
-
margin-top:
|
| 64 |
-
margin-bottom:
|
| 65 |
transition: all 0.2s;
|
|
|
|
| 66 |
}
|
| 67 |
-
.upload-btn:hover, .send-btn:hover, .audio-btn:hover {
|
| 68 |
-
background: linear-gradient(135deg, #
|
|
|
|
| 69 |
}
|
| 70 |
|
| 71 |
.gradio-chatbot {
|
| 72 |
-
border-radius:
|
| 73 |
-
border: 1.5px solid #
|
| 74 |
-
background: #
|
| 75 |
-
padding:
|
| 76 |
min-height: 350px !important;
|
| 77 |
max-height: 400px !important;
|
| 78 |
overflow-y: auto !important;
|
|
|
|
| 79 |
}
|
| 80 |
|
| 81 |
.gradio-audio {
|
| 82 |
-
margin-top:
|
| 83 |
-
margin-bottom:
|
| 84 |
}
|
| 85 |
|
| 86 |
.textbox {
|
| 87 |
-
border-radius:
|
| 88 |
-
border: 1.5px solid #
|
| 89 |
-
padding:
|
| 90 |
-
font-size:
|
| 91 |
-
margin-bottom:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
}
|
| 93 |
|
| 94 |
.status-text {
|
| 95 |
-
color: #
|
| 96 |
-
font-size:
|
| 97 |
-
margin-top:
|
| 98 |
font-weight: 500;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
}
|
| 100 |
"""
|
| 101 |
|
|
@@ -356,26 +406,24 @@ def process_audio_base64(audio_base64, history):
|
|
| 356 |
# Create Gradio interface with improved layout
|
| 357 |
with gr.Blocks(css=custom_css, title="Document Q&A System") as demo:
|
| 358 |
gr.Markdown("""
|
| 359 |
-
#
|
| 360 |
-
<div style='text-align:center; color:#
|
| 361 |
""")
|
| 362 |
|
| 363 |
-
with gr.Row():
|
| 364 |
with gr.Column():
|
| 365 |
with gr.Group(elem_classes="compact-box"):
|
| 366 |
-
gr.Markdown("<div class='section-title'>1. Upload
|
| 367 |
file_input = gr.File(label="Upload Document", file_types=[".pdf", ".txt", ".doc", ".docx"], file_count="single", type="binary", elem_classes="upload-btn")
|
| 368 |
file_output = gr.Textbox(label="Upload Status", interactive=False, elem_classes="textbox")
|
| 369 |
-
reset_btn = gr.Button("Reset Chat & Upload New Document", elem_classes="
|
| 370 |
-
|
| 371 |
def reset_all():
|
| 372 |
rag.thread_id = None
|
| 373 |
return "", []
|
| 374 |
-
|
| 375 |
file_input.change(process_file, file_input, file_output)
|
| 376 |
-
reset_btn.click(reset_all, None, [file_output,
|
| 377 |
|
| 378 |
-
with gr.Row():
|
| 379 |
with gr.Column():
|
| 380 |
with gr.Group(elem_classes="compact-box"):
|
| 381 |
gr.Markdown("<div class='section-title'>2. Chat with your Document</div>")
|
|
@@ -387,8 +435,7 @@ with gr.Blocks(css=custom_css, title="Document Q&A System") as demo:
|
|
| 387 |
question.submit(process_question, [question, chatbot], [question, chatbot])
|
| 388 |
send_btn.click(process_question, [question, chatbot], [question, chatbot])
|
| 389 |
|
| 390 |
-
|
| 391 |
-
with gr.Row(visible=False) as audio_row:
|
| 392 |
with gr.Column():
|
| 393 |
with gr.Group(elem_classes="compact-box"):
|
| 394 |
gr.Markdown("<div class='section-title'>Speak your Question</div>")
|
|
|
|
| 20 |
# Initialize OpenAI client with older API syntax
|
| 21 |
openai.api_key = api_key
|
| 22 |
|
| 23 |
+
# Custom CSS for a beautiful, modern look
|
| 24 |
custom_css = """
|
| 25 |
html, body, .gradio-container {
|
| 26 |
height: 100vh !important;
|
|
|
|
| 28 |
max-width: 100vw !important;
|
| 29 |
margin: 0 !important;
|
| 30 |
padding: 0 !important;
|
| 31 |
+
font-family: 'Inter', 'Segoe UI', Arial, sans-serif;
|
| 32 |
+
background: #f4f7fb;
|
| 33 |
+
color: #222;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
.centered-main {
|
| 37 |
+
display: flex;
|
| 38 |
+
flex-direction: column;
|
| 39 |
+
align-items: center;
|
| 40 |
+
justify-content: flex-start;
|
| 41 |
+
min-height: 100vh;
|
| 42 |
+
width: 100vw;
|
| 43 |
+
padding-top: 32px;
|
| 44 |
}
|
| 45 |
|
| 46 |
.compact-box {
|
| 47 |
background: #fff;
|
| 48 |
+
border-radius: 18px;
|
| 49 |
+
box-shadow: 0 4px 24px rgba(0, 60, 180, 0.07), 0 1.5px 4px rgba(0,0,0,0.04);
|
| 50 |
+
padding: 32px 32px 20px 32px;
|
| 51 |
+
margin-bottom: 32px;
|
| 52 |
width: 100%;
|
| 53 |
+
max-width: 600px;
|
| 54 |
margin-left: auto;
|
| 55 |
margin-right: auto;
|
| 56 |
+
border: 1.5px solid #e3e8f0;
|
| 57 |
}
|
| 58 |
|
| 59 |
.section-title {
|
| 60 |
+
font-size: 1.25rem;
|
| 61 |
+
font-weight: 700;
|
| 62 |
+
margin-bottom: 18px;
|
| 63 |
+
color: #1a237e;
|
| 64 |
+
letter-spacing: 0.01em;
|
| 65 |
}
|
| 66 |
|
| 67 |
+
.upload-btn, .send-btn, .audio-btn, .reset-btn {
|
| 68 |
+
background: linear-gradient(135deg, #1976D2 0%, #00bcd4 100%);
|
| 69 |
color: white;
|
| 70 |
border: none;
|
| 71 |
+
padding: 12px 28px;
|
| 72 |
+
border-radius: 24px;
|
| 73 |
cursor: pointer;
|
| 74 |
font-weight: 600;
|
| 75 |
+
font-size: 16px;
|
| 76 |
+
margin-top: 10px;
|
| 77 |
+
margin-bottom: 10px;
|
| 78 |
transition: all 0.2s;
|
| 79 |
+
box-shadow: 0 2px 8px rgba(25, 118, 210, 0.08);
|
| 80 |
}
|
| 81 |
+
.upload-btn:hover, .send-btn:hover, .audio-btn:hover, .reset-btn:hover {
|
| 82 |
+
background: linear-gradient(135deg, #00bcd4 0%, #1976D2 100%);
|
| 83 |
+
box-shadow: 0 4px 16px rgba(0, 188, 212, 0.13);
|
| 84 |
}
|
| 85 |
|
| 86 |
.gradio-chatbot {
|
| 87 |
+
border-radius: 14px !important;
|
| 88 |
+
border: 1.5px solid #e3e8f0 !important;
|
| 89 |
+
background: #f8fafc !important;
|
| 90 |
+
padding: 12px !important;
|
| 91 |
min-height: 350px !important;
|
| 92 |
max-height: 400px !important;
|
| 93 |
overflow-y: auto !important;
|
| 94 |
+
margin-bottom: 10px;
|
| 95 |
}
|
| 96 |
|
| 97 |
.gradio-audio {
|
| 98 |
+
margin-top: 12px;
|
| 99 |
+
margin-bottom: 12px;
|
| 100 |
}
|
| 101 |
|
| 102 |
.textbox {
|
| 103 |
+
border-radius: 12px !important;
|
| 104 |
+
border: 1.5px solid #e3e8f0 !important;
|
| 105 |
+
padding: 12px !important;
|
| 106 |
+
font-size: 16px !important;
|
| 107 |
+
margin-bottom: 10px;
|
| 108 |
+
background: #f8fafc !important;
|
| 109 |
+
color: #222 !important;
|
| 110 |
+
}
|
| 111 |
+
.textbox:focus {
|
| 112 |
+
border-color: #1976D2 !important;
|
| 113 |
+
box-shadow: 0 0 0 2px rgba(25, 118, 210, 0.13) !important;
|
| 114 |
}
|
| 115 |
|
| 116 |
.status-text {
|
| 117 |
+
color: #1976D2;
|
| 118 |
+
font-size: 15px;
|
| 119 |
+
margin-top: 10px;
|
| 120 |
font-weight: 500;
|
| 121 |
+
background: #e3f2fd;
|
| 122 |
+
border-radius: 8px;
|
| 123 |
+
padding: 8px 12px;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
/* File upload area */
|
| 127 |
+
input[type="file"]::-webkit-file-upload-button {
|
| 128 |
+
background: #1976D2;
|
| 129 |
+
color: #fff;
|
| 130 |
+
border: none;
|
| 131 |
+
border-radius: 8px;
|
| 132 |
+
padding: 8px 18px;
|
| 133 |
+
font-weight: 600;
|
| 134 |
+
cursor: pointer;
|
| 135 |
+
}
|
| 136 |
+
input[type="file"]::-webkit-file-upload-button:hover {
|
| 137 |
+
background: #00bcd4;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
/* Only one main scroll */
|
| 141 |
+
body, .gradio-container, #root, #app {
|
| 142 |
+
overflow: auto !important;
|
| 143 |
+
height: 100vh !important;
|
| 144 |
+
}
|
| 145 |
+
#component-0, #component-1, #component-2, .chatbot, .chat-container {
|
| 146 |
+
overflow: visible !important;
|
| 147 |
+
height: auto !important;
|
| 148 |
+
max-height: none !important;
|
| 149 |
}
|
| 150 |
"""
|
| 151 |
|
|
|
|
| 406 |
# Create Gradio interface with improved layout
|
| 407 |
with gr.Blocks(css=custom_css, title="Document Q&A System") as demo:
|
| 408 |
gr.Markdown("""
|
| 409 |
+
# <span style='color:#1976D2;'>Document Q&A System</span>
|
| 410 |
+
<div style='text-align:center; color:#1976D2; margin-bottom:18px;'>Upload a document, ask questions, or use your voice!</div>
|
| 411 |
""")
|
| 412 |
|
| 413 |
+
with gr.Row(elem_classes="centered-main"):
|
| 414 |
with gr.Column():
|
| 415 |
with gr.Group(elem_classes="compact-box"):
|
| 416 |
+
gr.Markdown("<div class='section-title'>1. Upload Document</div>")
|
| 417 |
file_input = gr.File(label="Upload Document", file_types=[".pdf", ".txt", ".doc", ".docx"], file_count="single", type="binary", elem_classes="upload-btn")
|
| 418 |
file_output = gr.Textbox(label="Upload Status", interactive=False, elem_classes="textbox")
|
| 419 |
+
reset_btn = gr.Button("Reset Chat & Upload New Document", elem_classes="reset-btn")
|
|
|
|
| 420 |
def reset_all():
|
| 421 |
rag.thread_id = None
|
| 422 |
return "", []
|
|
|
|
| 423 |
file_input.change(process_file, file_input, file_output)
|
| 424 |
+
reset_btn.click(reset_all, None, [file_output, chatbot])
|
| 425 |
|
| 426 |
+
with gr.Row(elem_classes="centered-main"):
|
| 427 |
with gr.Column():
|
| 428 |
with gr.Group(elem_classes="compact-box"):
|
| 429 |
gr.Markdown("<div class='section-title'>2. Chat with your Document</div>")
|
|
|
|
| 435 |
question.submit(process_question, [question, chatbot], [question, chatbot])
|
| 436 |
send_btn.click(process_question, [question, chatbot], [question, chatbot])
|
| 437 |
|
| 438 |
+
with gr.Row(elem_classes="centered-main", visible=False) as audio_row:
|
|
|
|
| 439 |
with gr.Column():
|
| 440 |
with gr.Group(elem_classes="compact-box"):
|
| 441 |
gr.Markdown("<div class='section-title'>Speak your Question</div>")
|