Update app.py
Browse files
app.py
CHANGED
|
@@ -1,158 +1,71 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
from rag_engine import RAGEngine
|
| 4 |
|
| 5 |
-
# Initialize engine
|
| 6 |
engine = RAGEngine()
|
| 7 |
|
| 8 |
-
# ββ
|
| 9 |
-
|
| 10 |
-
def
|
| 11 |
-
if
|
| 12 |
-
return "
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
return
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def process_text(text):
|
| 29 |
-
if not text.strip():
|
| 30 |
-
return "β οΈ Please paste some text.", gr.update(interactive=False)
|
| 31 |
-
try:
|
| 32 |
-
msg = engine.load_text(text.strip())
|
| 33 |
-
return msg, gr.update(interactive=True)
|
| 34 |
-
except Exception as e:
|
| 35 |
-
return f"β Error: {e}", gr.update(interactive=False)
|
| 36 |
-
|
| 37 |
-
def chat(message, history):
|
| 38 |
-
if not message.strip():
|
| 39 |
return history, ""
|
| 40 |
-
|
| 41 |
history = history or []
|
| 42 |
-
history.append((
|
| 43 |
return history, ""
|
| 44 |
|
| 45 |
-
def
|
| 46 |
engine.reset()
|
| 47 |
-
return [], "", "
|
| 48 |
|
| 49 |
-
# ββ UI βββββββββββββββββββββββββββββββββββ
|
| 50 |
|
| 51 |
-
|
| 52 |
-
# π€ RAG Chatbot
|
| 53 |
-
Upload a **PDF**, paste a **website URL**, or drop in **raw text** β then ask anything.
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
theme=gr.themes.Soft(primary_hue="violet", secondary_hue="slate"),
|
| 61 |
-
css="""
|
| 62 |
-
#chatbot { min-height: 420px; }
|
| 63 |
-
.source-box { border: 1px solid #e2e8f0; border-radius: 12px; padding: 16px; background: #f8fafc; }
|
| 64 |
-
.status-bar { font-size: 13px; padding: 6px 12px; border-radius: 8px; }
|
| 65 |
-
footer { display: none !important; }
|
| 66 |
-
""",
|
| 67 |
-
) as demo:
|
| 68 |
-
gr.Markdown(DESCRIPTION)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
gr.
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
text_input = gr.Textbox(
|
| 92 |
-
label="Paste your text",
|
| 93 |
-
lines=8,
|
| 94 |
-
placeholder="Paste any text here...",
|
| 95 |
-
)
|
| 96 |
-
text_btn = gr.Button("Load Text", variant="primary")
|
| 97 |
-
|
| 98 |
-
status = gr.Textbox(
|
| 99 |
-
label="Status",
|
| 100 |
-
value="β³ Load a source to start chatting.",
|
| 101 |
-
interactive=False,
|
| 102 |
-
elem_classes="status-bar",
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
with gr.Accordion("βΉοΈ NLP Insights", open=False):
|
| 106 |
-
nlp_output = gr.JSON(label="Last query analysis")
|
| 107 |
-
|
| 108 |
-
# ββ Right column: chat ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 109 |
-
with gr.Column(scale=2):
|
| 110 |
-
gr.Markdown("### π¬ Chat")
|
| 111 |
-
chatbot = gr.Chatbot(
|
| 112 |
-
elem_id="chatbot",
|
| 113 |
-
bubble_full_width=False,
|
| 114 |
-
avatar_images=(None, "https://huggingface.co/front/assets/huggingface_logo-noborder.svg"),
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
with gr.Row():
|
| 118 |
-
msg_box = gr.Textbox(
|
| 119 |
-
placeholder="Ask a question about your loaded content...",
|
| 120 |
-
show_label=False,
|
| 121 |
-
scale=4,
|
| 122 |
-
interactive=False,
|
| 123 |
-
)
|
| 124 |
-
send_btn = gr.Button("Send β€", variant="primary", scale=1)
|
| 125 |
-
|
| 126 |
-
reset_btn = gr.Button("ποΈ Clear & Reset", variant="secondary")
|
| 127 |
-
|
| 128 |
-
# ββ Example questions ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 129 |
-
gr.Examples(
|
| 130 |
-
examples=[
|
| 131 |
-
"What is the main topic of this document?",
|
| 132 |
-
"Summarise the key points in 3 bullet points.",
|
| 133 |
-
"What are the most important entities mentioned?",
|
| 134 |
-
"Give me a short conclusion based on the content.",
|
| 135 |
-
],
|
| 136 |
-
inputs=msg_box,
|
| 137 |
-
label="π‘ Example questions",
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
# ββ Wire events ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 141 |
-
def chat_with_nlp(message, history):
|
| 142 |
-
if not message.strip():
|
| 143 |
-
return history, "", {}
|
| 144 |
-
answer, nlp_info = engine.answer_with_nlp(message)
|
| 145 |
-
history = history or []
|
| 146 |
-
history.append((message, answer))
|
| 147 |
-
return history, "", nlp_info
|
| 148 |
-
|
| 149 |
-
pdf_btn.click(process_pdf, pdf_input, [status, msg_box])
|
| 150 |
-
url_btn.click(process_url, url_input, [status, msg_box])
|
| 151 |
-
text_btn.click(process_text, text_input, [status, msg_box])
|
| 152 |
-
|
| 153 |
-
send_btn.click(chat_with_nlp, [msg_box, chatbot], [chatbot, msg_box, nlp_output])
|
| 154 |
-
msg_box.submit(chat_with_nlp, [msg_box, chatbot], [chatbot, msg_box, nlp_output])
|
| 155 |
-
reset_btn.click(reset_all, outputs=[chatbot, msg_box, status])
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from rag_engine import RAGEngine
|
| 3 |
|
|
|
|
| 4 |
engine = RAGEngine()
|
| 5 |
|
| 6 |
+
# ββ Functions βββββββββββββββββββββββββββββ
|
| 7 |
+
|
| 8 |
+
def load_pdf(file):
|
| 9 |
+
if not file:
|
| 10 |
+
return "Upload file", gr.update(interactive=False)
|
| 11 |
+
return engine.load_pdf(file.name), gr.update(interactive=True)
|
| 12 |
+
|
| 13 |
+
def load_url(url):
|
| 14 |
+
if not url:
|
| 15 |
+
return "Enter URL", gr.update(interactive=False)
|
| 16 |
+
return engine.load_url(url), gr.update(interactive=True)
|
| 17 |
+
|
| 18 |
+
def load_text(text):
|
| 19 |
+
if not text:
|
| 20 |
+
return "Enter text", gr.update(interactive=False)
|
| 21 |
+
return engine.load_text(text), gr.update(interactive=True)
|
| 22 |
+
|
| 23 |
+
def chat(msg, history):
|
| 24 |
+
if not msg:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return history, ""
|
| 26 |
+
ans = engine.answer(msg)
|
| 27 |
history = history or []
|
| 28 |
+
history.append((msg, ans))
|
| 29 |
return history, ""
|
| 30 |
|
| 31 |
+
def reset():
|
| 32 |
engine.reset()
|
| 33 |
+
return [], "", "Reset done"
|
| 34 |
|
| 35 |
+
# ββ UI βββββββββββββββββββββββββββββββββββ
|
| 36 |
|
| 37 |
+
with gr.Blocks(title="RAG Bot") as demo:
|
| 38 |
+
gr.Markdown("# π€ RAG Chatbot")
|
|
|
|
| 39 |
|
| 40 |
+
with gr.Row():
|
| 41 |
+
with gr.Column():
|
| 42 |
+
pdf = gr.File()
|
| 43 |
+
btn_pdf = gr.Button("Load PDF")
|
| 44 |
|
| 45 |
+
url = gr.Textbox()
|
| 46 |
+
btn_url = gr.Button("Load URL")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
text = gr.Textbox(lines=5)
|
| 49 |
+
btn_text = gr.Button("Load Text")
|
| 50 |
+
|
| 51 |
+
status = gr.Textbox(value="Load data", interactive=False)
|
| 52 |
+
|
| 53 |
+
with gr.Column():
|
| 54 |
+
chatbot = gr.Chatbot() # β
FIXED (no bubble_full_width)
|
| 55 |
+
|
| 56 |
+
msg = gr.Textbox(placeholder="Ask something...", interactive=False)
|
| 57 |
+
send = gr.Button("Send")
|
| 58 |
+
|
| 59 |
+
reset_btn = gr.Button("Reset")
|
| 60 |
+
|
| 61 |
+
btn_pdf.click(load_pdf, pdf, [status, msg])
|
| 62 |
+
btn_url.click(load_url, url, [status, msg])
|
| 63 |
+
btn_text.click(load_text, text, [status, msg])
|
| 64 |
+
|
| 65 |
+
send.click(chat, [msg, chatbot], [chatbot, msg])
|
| 66 |
+
msg.submit(chat, [msg, chatbot], [chatbot, msg])
|
| 67 |
+
|
| 68 |
+
reset_btn.click(reset, outputs=[chatbot, msg, status])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|