import gradio as gr
from core.chat_interface import ChatInterface
from core.document_manager import DocumentManager
from core.rag_system import RAGSystem
import uuid
from threading import Lock
def create_gradio_ui():
rag_system = RAGSystem()
rag_system.initialize()
doc_manager = DocumentManager(rag_system)
chat_interface = ChatInterface(rag_system)
rag_lock = Lock()
def format_file_list():
files = doc_manager.get_markdown_files()
if not files:
return "📭 No documents available in the knowledge base"
return "\n".join([f"{f}" for f in files])
def upload_handler(files, progress=gr.Progress()):
if not files:
return None, format_file_list()
with rag_lock:
added, skipped = doc_manager.add_documents(
files,
progress_callback=lambda p, desc: progress(p, desc=desc)
)
gr.Info(f"✅ Added: {added} | Skipped: {skipped}")
return None, format_file_list()
def clear_handler():
try:
with rag_lock:
doc_manager.clear_all()
gr.Info("🗑️ Removed all documents")
except Exception as exc:
gr.Error(f"Unable to clear documents: {exc}")
return format_file_list()
def chat_handler(msg, hist, thread_id):
thread_id = thread_id or str(uuid.uuid4())
with rag_lock:
for chunk in chat_interface.chat(msg, hist, thread_id):
yield chunk, thread_id
def clear_chat_handler(thread_id):
with rag_lock:
return chat_interface.clear_session(thread_id)
with gr.Blocks(title="Document Chat") as demo:
session_thread = gr.State(value=None)
gr.Markdown(
"# Document Chat\n"
"Turn your documents into a conversation. Upload a PDF or Markdown file, "
"then ask a question.\n\n"
"**Groq-powered answers** | **Hybrid document search**",
elem_id="app-header",
)
with gr.Tab("Documents", elem_id="doc-management-tab"):
gr.Markdown("## Your document library")
gr.Markdown("Upload PDF or Markdown files to get started. Duplicate filenames are skipped.")
files_input = gr.File(
label="Drop PDF or Markdown files here",
file_count="multiple",
file_types=[".pdf", ".md"],
type="filepath",
height=200,
show_label=False
)
add_btn = gr.Button("Add Documents", variant="primary", size="md")
gr.Markdown("### Ready to search")
file_list = gr.Textbox(
value=format_file_list(),
interactive=False,
lines = 7,
max_lines=10,
elem_id="file-list-box",
show_label=False
)
with gr.Row():
refresh_btn = gr.Button("Refresh", size="md")
clear_btn = gr.Button("Clear All", variant="stop", size="md")
add_btn.click(
upload_handler, [files_input], [files_input, file_list],
show_progress="corner", concurrency_id="rag", api_visibility="private",
)
refresh_btn.click(format_file_list, None, file_list, api_visibility="private")
clear_btn.click(clear_handler, None, file_list, concurrency_id="rag", api_visibility="private")
with gr.Tab("Chat"):
chatbot = gr.Chatbot(
height=560,
placeholder="What would you like to know?
Upload documents first, then ask for a summary, explanation, or comparison.",
show_label=False,
layout="bubble",
elem_id="document-chat",
)
chatbot.clear(
clear_chat_handler, [session_thread], [session_thread],
concurrency_id="rag", api_visibility="private",
)
gr.ChatInterface(
fn=chat_handler,
chatbot=chatbot,
textbox=gr.Textbox(
placeholder="Ask a question about your documents...",
show_label=False,
elem_id="question-input",
),
additional_inputs=[session_thread],
additional_outputs=[session_thread],
concurrency_limit=1,
api_name="chat",
)
gr.Markdown(
"Documents are shared with everyone who can access this app. "
"Do not upload private or sensitive files to a public Space.",
elem_id="privacy-note",
)
return demo.queue(default_concurrency_limit=1)