Wall06 commited on
Commit
4d9dea7
Β·
verified Β·
1 Parent(s): 51cdd04

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from rag_engine import RAGEngine
4
+
5
+ # Initialize engine
6
+ engine = RAGEngine()
7
+
8
+ # ── Helpers ──────────────────────────────────────────────────────────────────
9
+
10
+ def process_pdf(file):
11
+ if file is None:
12
+ return "⚠️ No file uploaded.", gr.update(interactive=False)
13
+ try:
14
+ msg = engine.load_pdf(file.name)
15
+ return msg, gr.update(interactive=True)
16
+ except Exception as e:
17
+ return f"❌ Error: {e}", gr.update(interactive=False)
18
+
19
+ def process_url(url):
20
+ if not url.strip():
21
+ return "⚠️ Please enter a URL.", gr.update(interactive=False)
22
+ try:
23
+ msg = engine.load_url(url.strip())
24
+ return msg, gr.update(interactive=True)
25
+ except Exception as e:
26
+ return f"❌ Error: {e}", gr.update(interactive=False)
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
+ answer = engine.answer(message)
41
+ history = history or []
42
+ history.append((message, answer))
43
+ return history, ""
44
+
45
+ def reset_all():
46
+ engine.reset()
47
+ return [], "", "", "", "", "⚠️ No knowledge source loaded.", gr.update(interactive=False), {}
48
+
49
+ # ── UI Layout ────────────────────────────────────────────────────────────────
50
+
51
+ with gr.Blocks(theme=gr.themes.Soft(), title="RAG Chatbot") as demo:
52
+ gr.Markdown("# πŸ€– RAG Chatbot")
53
+ gr.Markdown("### Chat with your PDFs, URLs, or raw text using AI.")
54
+
55
+ with gr.Row():
56
+ # Left Column: Inputs
57
+ with gr.Column(scale=1):
58
+ with gr.Tab("πŸ“„ Upload PDF"):
59
+ pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
60
+ pdf_btn = gr.Button("Process PDF", variant="primary")
61
+
62
+ with gr.Tab("🌐 Load URL"):
63
+ url_input = gr.Textbox(label="Website URL", placeholder="https://example.com")
64
+ url_btn = gr.Button("Process URL", variant="primary")
65
+
66
+ with gr.Tab("πŸ“ Paste Text"):
67
+ text_input = gr.Textbox(label="Raw Text", lines=10, placeholder="Paste your text here...")
68
+ text_btn = gr.Button("Process Text", variant="primary")
69
+
70
+ status = gr.Label(value="⚠️ No knowledge source loaded.")
71
+
72
+ with gr.Accordion("πŸ” NLP Insights", open=False):
73
+ nlp_output = gr.JSON(label="Intent & Entities")
74
+
75
+ # Right Column: Chat
76
+ with gr.Column(scale=2):
77
+ chatbot = gr.Chatbot(label="Conversation", height=500)
78
+ with gr.Row():
79
+ msg_box = gr.Textbox(
80
+ placeholder="Ask a question about the content...",
81
+ show_label=False,
82
+ scale=4,
83
+ interactive=False,
84
+ )
85
+ send_btn = gr.Button("Send ➀", variant="primary", scale=1)
86
+
87
+ reset_btn = gr.Button("πŸ—‘οΈ Clear & Reset", variant="secondary")
88
+
89
+ # ── Example questions ────────────────────────────────────────────────────
90
+ gr.Examples(
91
+ examples=[
92
+ "What is the main topic of this document?",
93
+ "Summarise the key points in 3 bullet points.",
94
+ "What are the most important entities mentioned?",
95
+ "Give me a short conclusion based on the content.",
96
+ ],
97
+ inputs=msg_box,
98
+ label="πŸ’‘ Example questions",
99
+ )
100
+
101
+ # ── Wire events ──────────────────────────────────────────────────────────
102
+ def chat_with_nlp(message, history):
103
+ if not message.strip():
104
+ return history, "", {}
105
+ answer, nlp_info = engine.answer_with_nlp(message)
106
+ history = history or []
107
+ history.append((message, answer))
108
+ return history, "", nlp_info
109
+
110
+ pdf_btn.click(process_pdf, pdf_input, [status, msg_box])
111
+ url_btn.click(process_url, url_input, [status, msg_box])
112
+ text_btn.click(process_text, text_input, [status, msg_box])
113
+
114
+ send_btn.click(chat_with_nlp, [msg_box, chatbot], [chatbot, msg_box, nlp_output])
115
+ msg_box.submit(chat_with_nlp, [msg_box, chatbot], [chatbot, msg_box, nlp_output])
116
+ reset_btn.click(reset_all, None, [chatbot, msg_box, pdf_input, url_input, text_input, status, msg_box, nlp_output])
117
+
118
+ if __name__ == "__main__":
119
+ demo.launch()