File size: 6,613 Bytes
e6cc6f7 8503db1 e6cc6f7 6f0a3cf e6cc6f7 8503db1 e6cc6f7 3d7eef8 e6cc6f7 3d7eef8 e6cc6f7 6f0a3cf e6cc6f7 6f0a3cf e6cc6f7 6f0a3cf e6cc6f7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
import logging
import gradio as gr
from utils.document_utils import initialize_logging
from retriever.chat_manager import chat_response
# Note: DocumentManager is now initialized in retrieve_documents.py
from globals import app_config
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
initialize_logging()
def load_sample_question(question):
return question
def clear_selection():
return [], "", [] # Reset doc_selector to empty list
def process_uploaded_file(file, current_selection):
"""Process uploaded file using DocumentManager and update UI."""
status, page_list, filename, _ = app_config.doc_manager.process_document(file.name if file else None)
# Update current selection to include new file if not already present
updated_selection = current_selection if current_selection else []
if filename and filename not in updated_selection:
updated_selection.append(filename)
return (
status,
page_list,
gr.update(choices=app_config.doc_manager.get_uploaded_documents(), value=updated_selection)
)
def update_doc_selector(selected_docs):
"""Keep selected documents in sync."""
return selected_docs
# UI Configuration
models = ["qwen-2.5-32b", "gemma2-9b-it"]
example_questions = [
"What is communication server?",
"Show me an example of a configuration file.",
"How to create Protected File Directories ?",
"What are the attributes Azureblobstorage?",
"What is Mediator help?",
"Why AzureBlobStorage port is used?"
]
all_questions = [
"Can you explain Communication Server architecture?",
"Why does the other instance of my multi-instance qmgr seem to hang after a failover? Queue manager will not start after failover.",
"Explain the concept of blockchain.",
"What is the capital of France?",
"Do Surface Porosity and Pore Size Influence Mechanical Properties and Cellular Response to PEEK?",
"How does a vaccine work?",
"Tell me the step-by-step instruction for front-door installation.",
"What are the risk factors for heart disease?",
]
with gr.Blocks(css="""
.chatbot .user {
position: relative;
background-color: #cfdcfd;
padding: 12px 16px;
border-radius: 20px;
border-bottom-right-radius: 6px;
display: inline-block;
max-width: 80%;
margin: 8px 0;
}
/* Tail effect */
.chatbot .user::after {
content: "";
position: absolute;
right: -10px;
bottom: 10px;
width: 0;
height: 0;
border: 10px solid transparent;
border-left-color: #cfdcfd;
border-right: 0;
border-top: 0;
margin-top: -5px;
}
.chatbot .bot { background-color: #f1f8e9; padding: 8px; border-radius: 10px; } /* Light green for bot responses */
""") as interface:
interface.title = "π€ IntelliDoc: AI Document Explorer"
gr.Markdown("""
# π€ IntelliDoc: AI Document Explorer
**AI Document Explorer** allows you to upload PDF documents and interact with them using AI-powered analysis and summarization. Ask questions, extract key insights, and gain a deeper understanding of your documents effortlessly.
""")
with gr.Row():
# Left Sidebar
with gr.Column(scale=2):
gr.Markdown("## Upload and Select Document")
upload_btn = gr.File(label="Upload PDF Document", file_types=[".pdf"])
doc_selector = gr.Dropdown(
choices=app_config.doc_manager.get_uploaded_documents(),
label="Documents",
multiselect=True,
value=[] # Initial value as empty list
)
model_selector = gr.Dropdown(choices=models, label="Models", interactive=True)
clear_btn = gr.Button("Clear Selection")
upload_status = gr.Textbox(label="Upload Status", interactive=False)
# Process uploaded file and update UI
upload_btn.change(
process_uploaded_file,
inputs=[upload_btn, doc_selector],
outputs=[
upload_status,
gr.State(), # page_list
doc_selector # Update choices and value together
]
)
clear_btn.click(
clear_selection,
outputs=[doc_selector, upload_status, gr.State()]
)
# Reinitialize LLM when the model changes
model_selector.change(
app_config.gen_llm.reinitialize_llm,
inputs=[model_selector],
outputs=[upload_status]
)
# Middle Section (Chat & LLM Response)
with gr.Column(scale=6):
gr.Markdown("## Chat with document(s)")
chat_history = gr.Chatbot(label="Chat History", height= 650, bubble_full_width= False, type="messages")
with gr.Row():
chat_input = gr.Textbox(label="Ask additional questions about the document...", show_label=False, placeholder="Ask additional questions about the document...", elem_id="chat-input", lines=3)
chat_btn = gr.Button("π Send", variant="primary", elem_id="send-button", scale=0)
chat_btn.click(chat_response, inputs=[chat_input, doc_selector, chat_history], outputs=chat_history).then(
lambda: "", # Return an empty string to clear the chat_input
outputs=chat_input
)
# Right Sidebar (Sample Questions & History)
with gr.Column(scale=2):
gr.Markdown("## Frequently asked questions:")
with gr.Column():
gr.Examples(
examples=example_questions,
inputs=chat_input,
label=""
)
'''question_dropdown = gr.Dropdown(
label="",
choices=all_questions,
interactive=True,
info="Choose a question from the dropdown to populate the query box."
)'''
#gr.Markdown("## Logs")
#history = gr.Textbox(label="Previous Queries", interactive=False)
if __name__ == "__main__":
interface.launch() |