app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="PDF Chat Assistant") as demo:
|
| 2 |
+
with gr.Row():
|
| 3 |
+
gr.Markdown("""
|
| 4 |
+
# 📚 PDF Chat Assistant
|
| 5 |
+
### Have natural conversations with your documents
|
| 6 |
+
""")
|
| 7 |
+
|
| 8 |
+
with gr.Row():
|
| 9 |
+
with gr.Column(scale=1, min_width=300):
|
| 10 |
+
gr.Markdown("### Document Upload")
|
| 11 |
+
pdf_input = gr.File(
|
| 12 |
+
file_types=[".pdf"],
|
| 13 |
+
file_count="multiple",
|
| 14 |
+
label="Upload PDFs",
|
| 15 |
+
height=100
|
| 16 |
+
)
|
| 17 |
+
upload_btn = gr.Button("Process Documents", variant="primary")
|
| 18 |
+
status_box = gr.Textbox(label="Status", interactive=False)
|
| 19 |
+
gr.Markdown("""
|
| 20 |
+
**Instructions:**
|
| 21 |
+
1. Upload PDF documents
|
| 22 |
+
2. Click Process Documents
|
| 23 |
+
3. Start chatting in the right panel
|
| 24 |
+
""")
|
| 25 |
+
|
| 26 |
+
with gr.Column(scale=2):
|
| 27 |
+
chatbot = gr.Chatbot(
|
| 28 |
+
height=600,
|
| 29 |
+
bubble_full_width=False,
|
| 30 |
+
avatar_images=(
|
| 31 |
+
"user.png",
|
| 32 |
+
"bot.png"
|
| 33 |
+
)
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
with gr.Row():
|
| 37 |
+
message = gr.Textbox(
|
| 38 |
+
placeholder="Type your question about the documents...",
|
| 39 |
+
show_label=False,
|
| 40 |
+
container=False,
|
| 41 |
+
scale=7,
|
| 42 |
+
autofocus=True
|
| 43 |
+
)
|
| 44 |
+
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
clear_chat = gr.Button("🧹 Clear Conversation")
|
| 48 |
+
examples = gr.Examples(
|
| 49 |
+
examples=[
|
| 50 |
+
"Summarize the key points from the documents",
|
| 51 |
+
"What are the main findings?",
|
| 52 |
+
"Explain this in simpler terms"
|
| 53 |
+
],
|
| 54 |
+
inputs=message,
|
| 55 |
+
label="Example Questions"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Event handlers
|
| 59 |
+
upload_btn.click(
|
| 60 |
+
fn=handle_pdf_upload,
|
| 61 |
+
inputs=pdf_input,
|
| 62 |
+
outputs=status_box
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
submit_btn.click(
|
| 66 |
+
fn=user_query,
|
| 67 |
+
inputs=[message, chatbot],
|
| 68 |
+
outputs=[message, chatbot]
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
message.submit(
|
| 72 |
+
fn=user_query,
|
| 73 |
+
inputs=[message, chatbot],
|
| 74 |
+
outputs=[message, chatbot]
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
clear_chat.click(
|
| 78 |
+
lambda: [],
|
| 79 |
+
None,
|
| 80 |
+
chatbot,
|
| 81 |
+
queue=False
|
| 82 |
+
)
|