Wall06 commited on
Commit
7a75bf3
Β·
verified Β·
1 Parent(s): 65562e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -141
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
- # ── 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 [], "", "πŸ”„ Knowledge base cleared. Load a new source to begin."
48
 
49
- # ── UI ────────────────────────────────────────────────────────────────────────
50
 
51
- DESCRIPTION = """
52
- # πŸ€– RAG Chatbot β€” Chat with Your Data
53
- Upload a **PDF**, paste a **website URL**, or drop in **raw text** β€” then ask anything.
54
 
55
- Built with πŸ€— HuggingFace Β· FAISS Β· spaCy Β· Sentence-Transformers
56
- """
 
 
57
 
58
- with gr.Blocks(
59
- title="RAG Chatbot",
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
- with gr.Row():
71
- # ── Left column: source loader ──────────────────────────────────────
72
- with gr.Column(scale=1, elem_classes="source-box"):
73
- gr.Markdown("### πŸ“‚ Load a Knowledge Source")
74
-
75
- with gr.Tab("πŸ“„ PDF"):
76
- pdf_input = gr.File(
77
- label="Upload PDF",
78
- file_types=[".pdf"],
79
- type="filepath",
80
- )
81
- pdf_btn = gr.Button("Load PDF", variant="primary")
82
-
83
- with gr.Tab("🌐 Website URL"):
84
- url_input = gr.Textbox(
85
- label="Website URL",
86
- placeholder="https://example.com/page",
87
- )
88
- url_btn = gr.Button("Load URL", variant="primary")
89
-
90
- with gr.Tab("πŸ“ Raw Text"):
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(share=False)
 
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()