Spaces:
Sleeping
Sleeping
File size: 4,978 Bytes
b3b0ccd 3366ea9 b3b0ccd 3366ea9 b3b0ccd 3366ea9 b3b0ccd 3366ea9 b3b0ccd 3366ea9 b3b0ccd 3366ea9 b3b0ccd 3366ea9 b3b0ccd | 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 | 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="<strong>What would you like to know?</strong><br>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)
|