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
|
| 24 |
custom_css = """
|
| 25 |
html, body, .gradio-container {
|
| 26 |
height: 100vh !important;
|
|
@@ -29,131 +29,73 @@ html, body, .gradio-container {
|
|
| 29 |
margin: 0 !important;
|
| 30 |
padding: 0 !important;
|
| 31 |
font-family: 'Inter', sans-serif;
|
| 32 |
-
|
| 33 |
-
background: #fafbfc;
|
| 34 |
}
|
| 35 |
|
| 36 |
-
.
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
.chat-container, .file-upload, .audio-recorder {
|
| 44 |
-
box-sizing: border-box;
|
| 45 |
-
border-radius: 16px;
|
| 46 |
-
background: white;
|
| 47 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.07);
|
| 48 |
-
margin-bottom: 20px;
|
| 49 |
width: 100%;
|
|
|
|
|
|
|
|
|
|
| 50 |
}
|
| 51 |
|
| 52 |
-
.
|
| 53 |
-
|
| 54 |
-
padding: 24px;
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
-
.file-upload {
|
| 58 |
-
border: 2px dashed #e0e0e0;
|
| 59 |
-
padding: 24px;
|
| 60 |
-
text-align: center;
|
| 61 |
-
transition: all 0.3s ease;
|
| 62 |
-
}
|
| 63 |
-
.file-upload:hover {
|
| 64 |
-
border-color: #4CAF50;
|
| 65 |
-
background: #f0f7f0;
|
| 66 |
-
transform: translateY(-2px);
|
| 67 |
-
}
|
| 68 |
-
.audio-recorder {
|
| 69 |
-
padding: 24px;
|
| 70 |
-
text-align: center;
|
| 71 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
.record-button {
|
| 75 |
-
background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
|
| 76 |
-
color: white;
|
| 77 |
-
border: none;
|
| 78 |
-
padding: 14px 28px;
|
| 79 |
-
border-radius: 30px;
|
| 80 |
-
cursor: pointer;
|
| 81 |
-
transition: all 0.3s ease;
|
| 82 |
-
font-size: 16px;
|
| 83 |
font-weight: 600;
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
gap: 10px;
|
| 87 |
-
margin: 0 auto;
|
| 88 |
-
box-shadow: 0 4px 6px rgba(76, 175, 80, 0.2);
|
| 89 |
-
}
|
| 90 |
-
.record-button:hover {
|
| 91 |
-
background: linear-gradient(135deg, #45a049 0%, #3d8b40 100%);
|
| 92 |
-
transform: translateY(-2px);
|
| 93 |
-
box-shadow: 0 6px 8px rgba(76, 175, 80, 0.3);
|
| 94 |
-
}
|
| 95 |
-
.record-button.recording {
|
| 96 |
-
background: linear-gradient(135deg, #f44336 0%, #e53935 100%);
|
| 97 |
-
animation: pulse 1.5s infinite;
|
| 98 |
}
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
font-size: 14px;
|
| 102 |
-
margin-top: 12px;
|
| 103 |
-
font-weight: 500;
|
| 104 |
-
}
|
| 105 |
-
.send-button {
|
| 106 |
background: linear-gradient(135deg, #2196F3 0%, #1976D2 100%);
|
| 107 |
color: white;
|
| 108 |
border: none;
|
| 109 |
-
padding:
|
| 110 |
-
border-radius:
|
| 111 |
cursor: pointer;
|
| 112 |
-
transition: all 0.3s ease;
|
| 113 |
font-weight: 600;
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
| 115 |
}
|
| 116 |
-
.send-
|
| 117 |
background: linear-gradient(135deg, #1976D2 0%, #1565C0 100%);
|
| 118 |
-
transform: translateY(-2px);
|
| 119 |
-
box-shadow: 0 6px 8px rgba(33, 150, 243, 0.3);
|
| 120 |
}
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
border: 2px solid #e0e0e0 !important;
|
| 124 |
-
padding: 12px !important;
|
| 125 |
-
font-size: 16px !important;
|
| 126 |
-
transition: all 0.3s ease !important;
|
| 127 |
-
}
|
| 128 |
-
.textbox:focus {
|
| 129 |
-
border-color: #2196F3 !important;
|
| 130 |
-
box-shadow: 0 0 0 2px rgba(33, 150, 243, 0.2) !important;
|
| 131 |
-
}
|
| 132 |
-
.chatbot {
|
| 133 |
border-radius: 12px !important;
|
| 134 |
-
border:
|
| 135 |
-
padding: 16px !important;
|
| 136 |
-
width: 100% !important;
|
| 137 |
-
box-sizing: border-box !important;
|
| 138 |
background: #fff !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
}
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
}
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
| 150 |
}
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
}
|
| 158 |
"""
|
| 159 |
|
|
@@ -412,73 +354,37 @@ def process_audio_base64(audio_base64, history):
|
|
| 412 |
return "", history + [("Audio input", f"Error: {str(e)}")]
|
| 413 |
|
| 414 |
# Create Gradio interface with improved layout
|
| 415 |
-
with gr.Blocks(title="Document Q&A System"
|
| 416 |
gr.Markdown("""
|
| 417 |
-
#
|
| 418 |
-
Upload a document
|
| 419 |
""")
|
| 420 |
|
| 421 |
with gr.Row():
|
| 422 |
-
with gr.Column(
|
| 423 |
-
with gr.
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
file_count="single",
|
| 428 |
-
type="binary"
|
| 429 |
-
)
|
| 430 |
-
file_output = gr.Textbox(
|
| 431 |
-
label="Upload Status",
|
| 432 |
-
interactive=False,
|
| 433 |
-
elem_classes="textbox"
|
| 434 |
-
)
|
| 435 |
file_input.change(process_file, file_input, file_output)
|
| 436 |
|
| 437 |
with gr.Row():
|
| 438 |
-
with gr.Column(
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
elem_classes="
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
def chat_fn(message, history):
|
| 448 |
-
if not rag.thread_id:
|
| 449 |
-
return "Please upload a document first."
|
| 450 |
-
try:
|
| 451 |
-
response = rag.ask_question(message)
|
| 452 |
-
return response
|
| 453 |
-
except Exception as e:
|
| 454 |
-
return f"Error: {str(e)}"
|
| 455 |
-
|
| 456 |
-
def vote(data: gr.LikeData):
|
| 457 |
-
if data.liked:
|
| 458 |
-
print("You upvoted this response: " + data.value["value"])
|
| 459 |
-
else:
|
| 460 |
-
print("You downvoted this response: " + data.value["value"])
|
| 461 |
-
|
| 462 |
-
chatbot.like(vote, None, None)
|
| 463 |
-
|
| 464 |
-
gr.ChatInterface(
|
| 465 |
-
fn=chat_fn,
|
| 466 |
-
chatbot=chatbot,
|
| 467 |
-
examples=[
|
| 468 |
-
"What is this document about?",
|
| 469 |
-
"Can you summarize the main points?",
|
| 470 |
-
"What are the key findings?"
|
| 471 |
-
],
|
| 472 |
-
title="Document Q&A",
|
| 473 |
-
description="Ask questions about your uploaded document",
|
| 474 |
-
theme="soft"
|
| 475 |
-
)
|
| 476 |
|
| 477 |
with gr.Row():
|
| 478 |
-
with gr.Column(
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
|
|
|
|
|
|
| 482 |
|
| 483 |
# Add JavaScript for audio handling
|
| 484 |
demo.load(
|
|
|
|
| 20 |
# Initialize OpenAI client with older API syntax
|
| 21 |
openai.api_key = api_key
|
| 22 |
|
| 23 |
+
# Custom CSS for a modern, compact look
|
| 24 |
custom_css = """
|
| 25 |
html, body, .gradio-container {
|
| 26 |
height: 100vh !important;
|
|
|
|
| 29 |
margin: 0 !important;
|
| 30 |
padding: 0 !important;
|
| 31 |
font-family: 'Inter', sans-serif;
|
| 32 |
+
background: #f7f8fa;
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
+
.compact-box {
|
| 36 |
+
background: #fff;
|
| 37 |
+
border-radius: 14px;
|
| 38 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.07);
|
| 39 |
+
padding: 24px 24px 16px 24px;
|
| 40 |
+
margin-bottom: 18px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
width: 100%;
|
| 42 |
+
max-width: 700px;
|
| 43 |
+
margin-left: auto;
|
| 44 |
+
margin-right: auto;
|
| 45 |
}
|
| 46 |
|
| 47 |
+
.section-title {
|
| 48 |
+
font-size: 1.2rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
font-weight: 600;
|
| 50 |
+
margin-bottom: 10px;
|
| 51 |
+
color: #222;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
+
|
| 54 |
+
.upload-btn, .send-btn, .audio-btn {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
background: linear-gradient(135deg, #2196F3 0%, #1976D2 100%);
|
| 56 |
color: white;
|
| 57 |
border: none;
|
| 58 |
+
padding: 10px 22px;
|
| 59 |
+
border-radius: 22px;
|
| 60 |
cursor: pointer;
|
|
|
|
| 61 |
font-weight: 600;
|
| 62 |
+
font-size: 15px;
|
| 63 |
+
margin-top: 8px;
|
| 64 |
+
margin-bottom: 8px;
|
| 65 |
+
transition: all 0.2s;
|
| 66 |
}
|
| 67 |
+
.upload-btn:hover, .send-btn:hover, .audio-btn:hover {
|
| 68 |
background: linear-gradient(135deg, #1976D2 0%, #1565C0 100%);
|
|
|
|
|
|
|
| 69 |
}
|
| 70 |
+
|
| 71 |
+
.gradio-chatbot {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
border-radius: 12px !important;
|
| 73 |
+
border: 1.5px solid #e0e0e0 !important;
|
|
|
|
|
|
|
|
|
|
| 74 |
background: #fff !important;
|
| 75 |
+
padding: 10px !important;
|
| 76 |
+
min-height: 350px !important;
|
| 77 |
+
max-height: 400px !important;
|
| 78 |
+
overflow-y: auto !important;
|
| 79 |
}
|
| 80 |
+
|
| 81 |
+
.gradio-audio {
|
| 82 |
+
margin-top: 10px;
|
| 83 |
+
margin-bottom: 10px;
|
| 84 |
}
|
| 85 |
|
| 86 |
+
.textbox {
|
| 87 |
+
border-radius: 10px !important;
|
| 88 |
+
border: 1.5px solid #e0e0e0 !important;
|
| 89 |
+
padding: 10px !important;
|
| 90 |
+
font-size: 15px !important;
|
| 91 |
+
margin-bottom: 8px;
|
| 92 |
}
|
| 93 |
|
| 94 |
+
.status-text {
|
| 95 |
+
color: #666;
|
| 96 |
+
font-size: 14px;
|
| 97 |
+
margin-top: 8px;
|
| 98 |
+
font-weight: 500;
|
| 99 |
}
|
| 100 |
"""
|
| 101 |
|
|
|
|
| 354 |
return "", history + [("Audio input", f"Error: {str(e)}")]
|
| 355 |
|
| 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 |
+
# 📄 Document Q&A System
|
| 360 |
+
<div style='text-align:center; color:#555; margin-bottom:18px;'>Upload a document, ask questions, or use your voice!</div>
|
| 361 |
""")
|
| 362 |
|
| 363 |
with gr.Row():
|
| 364 |
+
with gr.Column():
|
| 365 |
+
with gr.Box(elem_classes="compact-box"):
|
| 366 |
+
gr.Markdown("<div class='section-title'>1. Upload Document</div>")
|
| 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 |
file_input.change(process_file, file_input, file_output)
|
| 370 |
|
| 371 |
with gr.Row():
|
| 372 |
+
with gr.Column():
|
| 373 |
+
with gr.Box(elem_classes="compact-box"):
|
| 374 |
+
gr.Markdown("<div class='section-title'>2. Chat with your Document</div>")
|
| 375 |
+
chatbot = gr.Chatbot(height=350, elem_classes="gradio-chatbot", label=None)
|
| 376 |
+
question = gr.Textbox(label="Type your question and press Enter", placeholder="Ask a question about your document...", elem_classes="textbox")
|
| 377 |
+
send_btn = gr.Button("Send", elem_classes="send-btn")
|
| 378 |
+
question.submit(process_question, [question, chatbot], [question, chatbot])
|
| 379 |
+
send_btn.click(process_question, [question, chatbot], [question, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
|
| 381 |
with gr.Row():
|
| 382 |
+
with gr.Column():
|
| 383 |
+
with gr.Box(elem_classes="compact-box"):
|
| 384 |
+
gr.Markdown("<div class='section-title'>3. Or use your Voice</div>")
|
| 385 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="Record or Upload Audio", elem_classes="gradio-audio")
|
| 386 |
+
audio_status = gr.Textbox(label="Audio Status", interactive=False, elem_classes="status-text")
|
| 387 |
+
audio_input.change(process_audio, [audio_input, chatbot], [audio_input, chatbot])
|
| 388 |
|
| 389 |
# Add JavaScript for audio handling
|
| 390 |
demo.load(
|