File size: 2,067 Bytes
4d9dea7 9653244 7a75bf3 73a473a 7a75bf3 73a473a 7a75bf3 73a473a 7a75bf3 73a473a 7a75bf3 73a473a 9653244 73a473a 9653244 7a75bf3 73a473a 7a75bf3 4d9dea7 7a75bf3 4d9dea7 7a75bf3 9653244 7a75bf3 9653244 7a75bf3 9653244 7a75bf3 4d9dea7 9653244 | 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 | import gradio as gr
from rag_engine import RAGEngine
engine = RAGEngine()
# ββ Functions βββββββββββββββββββββββββ
def load_pdf(file):
if not file:
return "Upload file", gr.update(interactive=False)
return engine.load_pdf(file.name), gr.update(interactive=True)
def load_url(url):
if not url:
return "Enter URL", gr.update(interactive=False)
return engine.load_url(url), gr.update(interactive=True)
def load_text(text):
if not text:
return "Enter text", gr.update(interactive=False)
return engine.load_text(text), gr.update(interactive=True)
def chat(msg, history):
if not msg:
return history, ""
ans = engine.answer(msg)
history = history or []
history.append((msg, ans))
return history, ""
def reset():
engine.reset()
return [], "", "Reset done"
# ββ UI βββββββββββββββββββββββββββββββ
with gr.Blocks(title="RAG Chatbot") as demo:
gr.Markdown("# π€ RAG Chatbot")
with gr.Row():
with gr.Column():
pdf = gr.File()
btn_pdf = gr.Button("Load PDF")
url = gr.Textbox()
btn_url = gr.Button("Load URL")
text = gr.Textbox(lines=5)
btn_text = gr.Button("Load Text")
status = gr.Textbox(value="Load data", interactive=False)
with gr.Column():
chatbot = gr.Chatbot()
msg = gr.Textbox(
placeholder="Ask something...",
interactive=False
)
send = gr.Button("Send")
reset_btn = gr.Button("Reset")
btn_pdf.click(load_pdf, pdf, [status, msg])
btn_url.click(load_url, url, [status, msg])
btn_text.click(load_text, text, [status, msg])
send.click(chat, [msg, chatbot], [chatbot, msg])
msg.submit(chat, [msg, chatbot], [chatbot, msg])
reset_btn.click(reset, outputs=[chatbot, msg, status])
# β
IMPORTANT (Gradio 6 style)
demo.launch() |