-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
342 lines (279 loc) · 11.4 KB
/
Copy pathapp.py
File metadata and controls
342 lines (279 loc) · 11.4 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import gradio as gr
import json
import os
import psutil
from datetime import datetime
from pathlib import Path
import PyPDF2
from main import (
create_context,
BgeSmallEmbedModel,
T5LLM, OllamaLLM, GeminiLLM,
network_participants,
make_index,
perform_query,
scrape_save_data
)
from llama_index.core.ingestion import IngestionPipeline
from llama_index.core.node_parser import SentenceSplitter
from syftbox.lib import Client, SyftPermission
import shutil
embed_model = BgeSmallEmbedModel()
# model_name = "qwen2.5:1.5b"
# model_name = "qwen2.5:0.5b"
# model_name = "qwen2.5:1.5b-instruct"
# model_name = "smollm2:135m"
# model_name = "smollm2:360m"
# model_name = "tinyllama"
# model_name = "tinydolphin"
# model_name = "granite3-moe"
model_name = "deepseek-r1:1.5b"
llm = OllamaLLM(model_name=model_name) # T5LLM() #local setup
client = Client.load()
global_context = create_context()
pipeline = IngestionPipeline(
transformations=[SentenceSplitter(
chunk_size=200, chunk_overlap=10), embed_model.embedding_model])
class SessionState:
def __init__(self):
self.chat_history = []
self.current_model = "HuggingFace"
self.gemini_key = None
self.datasite_path = Path(os.path.expanduser(
"~")) / ".federated_rag" / "data"
# for testing uncomment the line below
# self.datasite_path = Path("extra_test/scraping_test")
self.participants = []
self.session_name = "Untitled Session"
self.query_count = 0
session = SessionState()
def store_indices_locally(participants, datasite_path, target):
index_path_dict = {}
for user_folder in participants:
index_path: Path = Path(datasite_path) / \
user_folder / "public" / "vector_index"
index_path_dict[user_folder] = index_path
for user, index_path in index_path_dict.items():
path = target / f"vector_index_{user}"
shutil.copytree(index_path, path, dirs_exist_ok=True)
print(f"Index copied for {user} at {path}")
def initialize_backend():
try:
if not session.datasite_path.exists():
session.datasite_path.mkdir(parents=True, exist_ok=True)
session.participants = network_participants(
client.datasite_path.parent) # SyftBox/datasites
# SyftBox/datasites
scrape_save_data(session.participants, client.datasite_path.parent)
active_participants = make_index(
session.participants,
client.datasite_path.parent,
global_context,
pipeline=pipeline
)
print(f"Active participants: {active_participants}")
print(f"Storing indices locally..")
store_indices_locally(
participants=active_participants,
datasite_path=client.datasite_path.parent,
target=session.datasite_path,
)
except Exception as e:
print("Error occurred during initialization:", str(e))
def process_message(message, history, model_choice, gemini_key=None, file=None):
try:
if model_choice == "Gemini" and not gemini_key:
gr.Warning("Please enter your Gemini API key")
return "", history, session.session_name
if model_choice == "HuggingFace":
try:
response_obj = perform_query(
query=message,
source=session.datasite_path, # ~/ ".federated_rag" / "data"
embed_model=embed_model,
llm=llm,
context=global_context
)
response = response_obj
except Exception as e:
print(f"Error processing query: {str(e)}")
else:
response = f"Processing with Gemini: {message}"
gemini_llm = GeminiLLM(api_key_path=gemini_key)
response_obj = perform_query(
query=message,
source=session.datasite_path, # ~/ ".federated_rag" / "data"
embed_model=embed_model,
llm=gemini_llm,
context=global_context
)
response = response_obj
if file:
try:
pdf_reader = PyPDF2.PdfReader(file.name)
pdf_text = ""
for page in pdf_reader.pages:
pdf_text += page.extract_text()
response += f"\n\nProcessed PDF content: {pdf_text[:500]}..."
except Exception as e:
response += f"\n\nError processing PDF: {str(e)}"
history.append((message, response))
session.query_count += 1
if session.query_count <= 2:
session.session_name = update_session_name(message)
return "", history, session.session_name
except Exception as e:
return "", history, session.session_name
def get_metrics():
cpu_percent = psutil.cpu_percent()
memory_percent = psutil.virtual_memory().percent
return f"CPU: {cpu_percent:.1f}% | Memory: {memory_percent:.1f}%"
def handle_model_selection(model_choice):
session.current_model = model_choice
return gr.update(visible=model_choice == "Gemini")
def update_session_name(query):
words = query.split()[:2]
return f"Session_{' '.join(words)}_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
def save_snapshot(history, session_name):
if not os.path.exists('sessions'):
os.makedirs('sessions')
filename = f"sessions/{session_name}.json"
data = {
"session_name": session_name,
"chat_history": history,
"timestamp": datetime.now().isoformat()
}
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
return f"Session saved as {filename}"
def create_ui():
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("<center><h1>🔮 Federated RAG </h1></center>")
with gr.Tab("FED-RAG-BOT"):
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### Session Info")
session_name_display = gr.Textbox(
label="Session Name",
value=session.session_name,
interactive=False
)
gr.Markdown("### Chat History")
session_history = gr.Textbox(
label="History",
value="",
interactive=False,
lines=10,
show_label=False
)
clear_btn = gr.Button("Clear History")
delete_btn = gr.Button("Delete Session")
with gr.Column(scale=3):
chatbot = gr.Chatbot(height=700)
with gr.Row():
msg = gr.Textbox(
show_label=False,
placeholder="Enter your message...",
scale=8
)
send = gr.Button("Send", scale=1)
with gr.Column(scale=1):
gr.Markdown("### Model Selection")
model_dropdown = gr.Dropdown(
choices=["Gemini", "HuggingFace"],
label="Select Model",
value="HuggingFace"
)
gemini_key_input = gr.Textbox(
label="Gemini API Key",
visible=False,
type="password"
)
gr.Markdown("### System Metrics")
metrics_text = gr.Textbox(
label="Metrics",
interactive=False
)
refresh_btn = gr.Button("Refresh Metrics")
save_btn = gr.Button("Save Snapshot")
model_dropdown.change(
fn=handle_model_selection,
inputs=model_dropdown,
outputs=gemini_key_input
)
send.click(
fn=process_message,
inputs=[msg, chatbot, model_dropdown,
gemini_key_input],
outputs=[msg, chatbot, session_name_display]
)
msg.submit(
fn=process_message,
inputs=[msg, chatbot, model_dropdown,
gemini_key_input],
outputs=[msg, chatbot, session_name_display]
)
refresh_btn.click(
fn=get_metrics,
outputs=metrics_text
)
save_btn.click(
fn=save_snapshot,
inputs=[chatbot, session_name_display],
)
clear_btn.click(
fn=lambda: clear_session_history(session_history),
outputs=session_history
)
delete_btn.click(
fn=lambda: delete_session(
session_name_display, session_history),
outputs=[session_name_display, session_history]
)
demo.load(get_metrics, outputs=metrics_text, every=10)
gr.HTML("""
<script>
function saveSessionData() {
let sessionData = {
chatHistory: window.sessionStorage.getItem('chatHistory') || "[]",
sessionName: window.sessionStorage.getItem('sessionName') || "Untitled Session"
};
localStorage.setItem("sessionData", JSON.stringify(sessionData));
}
function loadSessionData() {
let sessionData = localStorage.getItem("sessionData");
if (sessionData) {
sessionData = JSON.parse(sessionData);
window.sessionStorage.setItem('chatHistory', sessionData.chatHistory);
window.sessionStorage.setItem('sessionName', sessionData.sessionName);
const chatHistory = JSON.parse(sessionData.chatHistory);
const chatWindow = document.querySelector('gr-chatbot');
chatHistory.forEach(([user, bot]) => {
const message = document.createElement('div');
message.classList.add('message');
message.innerHTML = `<div class="user">${user}</div><div class="bot">${bot}</div>`;
chatWindow.appendChild(message);
});
}
}
window.onload = loadSessionData;
window.onbeforeunload = saveSessionData;
</script>
""")
with gr.Tab("STATS"):
gr.Image("imgs/wordcloud.png")
return demo
def clear_session_history(session_history):
session.chat_history = []
return gr.update(value="")
def delete_session(session_name_display, session_history):
session.chat_history = []
session.session_name = "Untitled Session"
session_name_display.update(value=session.session_name)
return gr.update(value=""), session_name_display
def main():
initialize_backend()
demo = create_ui()
demo.launch(server_port=7861, show_error=True)
if __name__ == "__main__":
main()