NavyDevilDoc commited on
Commit
08500b7
Β·
verified Β·
1 Parent(s): 714851b

Upload app.py

Browse files
Files changed (1) hide show
  1. src/app.py +445 -0
src/app.py ADDED
@@ -0,0 +1,445 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import os
4
+ import unicodedata
5
+ import resources # Assuming this file exists in your repo
6
+ import tracker
7
+ import rag_engine # Now safe to import at top level (lazy loading enabled)
8
+ from openai import OpenAI
9
+ from datetime import datetime
10
+
11
+ # --- CONFIGURATION ---
12
+ st.set_page_config(page_title="Navy AI Toolkit", page_icon="βš“", layout="wide")
13
+
14
+ # 1. SETUP CREDENTIALS
15
+ API_URL_ROOT = os.getenv("API_URL") # For Ollama models
16
+ OPENAI_KEY = os.getenv("OPENAI_API_KEY") # For GPT-4o
17
+
18
+ # --- INITIALIZATION ---
19
+ if "roles" not in st.session_state:
20
+ st.session_state.roles = []
21
+
22
+ # --- LOGIN / REGISTER LOGIC ---
23
+ if "authentication_status" not in st.session_state or st.session_state["authentication_status"] is None:
24
+ # If not logged in, show tabs
25
+ login_tab, register_tab = st.tabs(["πŸ”‘ Login", "πŸ“ Register"])
26
+
27
+ with login_tab:
28
+ is_logged_in = tracker.check_login()
29
+ # FIX: Trigger User DB Download ONLY on fresh login
30
+ if is_logged_in:
31
+ tracker.download_user_db(st.session_state.username)
32
+ st.rerun() # Refresh to show the app
33
+
34
+ with register_tab:
35
+ st.header("Create Account")
36
+ with st.form("reg_form"):
37
+ new_user = st.text_input("Username")
38
+ new_name = st.text_input("Display Name")
39
+ new_email = st.text_input("Email")
40
+ new_pwd = st.text_input("Password", type="password")
41
+ invite = st.text_input("Invitation Passcode")
42
+
43
+ if st.form_submit_button("Register"):
44
+ success, msg = tracker.register_user(new_email, new_user, new_name, new_pwd, invite)
45
+ if success:
46
+ st.success(msg)
47
+ else:
48
+ st.error(msg)
49
+
50
+ # Stop execution if not logged in
51
+ if not st.session_state.get("authentication_status"):
52
+ st.stop()
53
+
54
+ # --- GLOBAL PLACEHOLDERS ---
55
+ metric_placeholder = None
56
+ admin_metric_placeholder = None
57
+
58
+ # --- SIDEBAR (CONSOLIDATED) ---
59
+ with st.sidebar:
60
+ st.header("πŸ‘€ User Profile")
61
+ st.write(f"Welcome, **{st.session_state.name}**")
62
+
63
+ st.header("πŸ“Š Usage Tracker")
64
+ metric_placeholder = st.empty()
65
+
66
+ # Admin Tools
67
+ if "admin" in st.session_state.roles:
68
+ st.divider()
69
+ st.header("πŸ›‘οΈ Admin Tools")
70
+ admin_metric_placeholder = st.empty()
71
+
72
+ # FIX: Point to the correct persistence path
73
+ log_path = tracker.get_log_path()
74
+ if log_path.exists():
75
+ with open(log_path, "r") as f:
76
+ log_data = f.read()
77
+ st.download_button(
78
+ label="πŸ“₯ Download Usage Logs",
79
+ data=log_data,
80
+ file_name=f"usage_log_{datetime.now().strftime('%Y-%m-%d')}.json",
81
+ mime="application/json"
82
+ )
83
+ else:
84
+ st.warning("No logs found yet.")
85
+
86
+ # Logout
87
+ if "authenticator" in st.session_state:
88
+ st.session_state.authenticator.logout(location='sidebar')
89
+
90
+ st.divider()
91
+
92
+ # --- MODEL SELECTOR ---
93
+ st.header("🧠 Model Selector")
94
+
95
+ model_map = {
96
+ "Granite 4 (IBM)": "granite4:latest",
97
+ "Llama 3.2 (Meta)": "llama3.2:latest",
98
+ "Gemma 3 (Google)": "gemma3:latest"
99
+ }
100
+
101
+ model_options = list(model_map.keys())
102
+ model_captions = ["Slower for now, but free and private" for _ in model_options]
103
+
104
+ if "admin" in st.session_state.roles:
105
+ model_options.append("GPT-4o (Omni)")
106
+ model_captions.append("Fast, smart, sends data to OpenAI")
107
+
108
+ model_choice = st.radio(
109
+ "Choose your Intelligence:",
110
+ model_options,
111
+ captions=model_captions
112
+ )
113
+ st.info(f"Connected to: **{model_choice}**")
114
+
115
+ st.divider()
116
+ st.header("βš™οΈ Controls")
117
+ max_len = st.slider("Max Response Length (Tokens)", 100, 2000, 500)
118
+
119
+ # --- HELPER FUNCTIONS ---
120
+ def update_sidebar_metrics():
121
+ """Refreshes the global placeholders defined in the sidebar."""
122
+ if metric_placeholder is None:
123
+ return
124
+
125
+ stats = tracker.get_daily_stats()
126
+ user_stats = stats["users"].get(st.session_state.username, {"input":0, "output":0})
127
+
128
+ metric_placeholder.metric("My Tokens Today", user_stats["input"] + user_stats["output"])
129
+
130
+ if "admin" in st.session_state.roles and admin_metric_placeholder is not None:
131
+ admin_metric_placeholder.metric("Team Total Today", stats["total_tokens"])
132
+
133
+ # Call metrics once on load
134
+ update_sidebar_metrics()
135
+
136
+ def query_local_model(user_prompt, system_persona, max_tokens, model_name):
137
+ if not API_URL_ROOT:
138
+ return "Error: API_URL not set.", None
139
+
140
+ url = API_URL_ROOT + "/generate"
141
+ payload = {
142
+ "text": user_prompt,
143
+ "persona": system_persona,
144
+ "max_tokens": max_tokens,
145
+ "model": model_name
146
+ }
147
+
148
+ try:
149
+ response = requests.post(url, json=payload, timeout=120)
150
+
151
+ if response.status_code == 200:
152
+ response_data = response.json()
153
+ ans = response_data.get("response", "")
154
+ usage = response_data.get("usage", {"input":0, "output":0})
155
+ return ans, usage
156
+
157
+ return f"Error {response.status_code}: {response.text}", None
158
+
159
+ except Exception as e:
160
+ return f"Connection Error: {e}", None
161
+
162
+ def query_gpt4o(prompt, persona, max_tokens):
163
+ if not OPENAI_KEY:
164
+ return "Error: OPENAI_API_KEY not set.", None
165
+
166
+ client = OpenAI(api_key=OPENAI_KEY)
167
+
168
+ try:
169
+ response = client.chat.completions.create(
170
+ model="gpt-4o",
171
+ max_tokens=max_tokens,
172
+ messages=[
173
+ {"role": "system", "content": persona},
174
+ {"role": "user", "content": prompt}
175
+ ],
176
+ temperature=0.3
177
+ )
178
+ usage_obj = response.usage
179
+ usage_dict = {"input": usage_obj.prompt_tokens, "output": usage_obj.completion_tokens}
180
+ return response.choices[0].message.content, usage_dict
181
+
182
+ except Exception as e:
183
+ return f"OpenAI Error: {e}", None
184
+
185
+ def clean_text(text):
186
+ if not text: return ""
187
+ text = unicodedata.normalize('NFKC', text)
188
+ replacements = {'β€œ': '"', '”': '"', 'β€˜': "'", '’': "'", '–': '-', 'β€”': '-', '…': '...', '\u00a0': ' '}
189
+ for old, new in replacements.items():
190
+ text = text.replace(old, new)
191
+ return text.strip()
192
+
193
+ def ask_ai(user_prompt, system_persona, max_tokens):
194
+ if "GPT-4o" in model_choice:
195
+ return query_gpt4o(user_prompt, system_persona, max_tokens)
196
+ else:
197
+ technical_name = model_map[model_choice]
198
+ return query_local_model(user_prompt, system_persona, max_tokens, technical_name)
199
+
200
+ # --- MAIN UI ---
201
+ st.title("AI Toolkit")
202
+ tab1, tab2, tab3, tab4 = st.tabs(["πŸ“§ Email Builder", "πŸ’¬ Chat Playground", "πŸ› οΈ Prompt Architect", "πŸ“š Knowledge Base"])
203
+
204
+ # --- TAB 1: EMAIL BUILDER ---
205
+ with tab1:
206
+ st.header("Structured Email Generator")
207
+ if "email_draft" not in st.session_state:
208
+ st.session_state.email_draft = ""
209
+
210
+ st.subheader("1. Define the Voice")
211
+ style_mode = st.radio("How should the AI write?", ["Use a Preset Persona", "Mimic My Style"], horizontal=True)
212
+
213
+ selected_persona_instruction = ""
214
+ if style_mode == "Use a Preset Persona":
215
+ persona_name = st.selectbox("Select a Persona", list(resources.TONE_LIBRARY.keys()))
216
+ selected_persona_instruction = resources.TONE_LIBRARY[persona_name]
217
+ st.info(f"**System Instruction:** {selected_persona_instruction}")
218
+ else:
219
+ st.info("Upload 1-3 text files of your previous emails.")
220
+ uploaded_style_files = st.file_uploader("Upload Samples (.txt)", type=["txt"], accept_multiple_files=True)
221
+ if uploaded_style_files:
222
+ style_context = ""
223
+ for uploaded_file in uploaded_style_files:
224
+ string_data = uploaded_file.read().decode("utf-8")
225
+ style_context += f"---\n{string_data}\n---\n"
226
+ selected_persona_instruction = f"Analyze these examples and mimic the style:\n{style_context}"
227
+
228
+ st.divider()
229
+ st.subheader("2. Details")
230
+ c1, c2 = st.columns(2)
231
+ with c1: recipient = st.text_input("Recipient")
232
+ with c2: topic = st.text_input("Topic")
233
+
234
+ st.caption("Content Source")
235
+ input_method = st.toggle("Upload notes file?")
236
+ raw_notes = ""
237
+ if input_method:
238
+ notes_file = st.file_uploader("Upload Notes (.txt)", type=["txt"])
239
+ if notes_file: raw_notes = notes_file.read().decode("utf-8")
240
+ else:
241
+ raw_notes = st.text_area("Paste notes:", height=150)
242
+
243
+ # Context Bar
244
+ est_tokens = len(raw_notes) / 4
245
+ st.progress(min(est_tokens / 128000, 1.0), text=f"Context: {int(est_tokens)} tokens")
246
+
247
+ if st.button("Draft Email", type="primary"):
248
+ if not raw_notes:
249
+ st.warning("Please provide notes.")
250
+ else:
251
+ clean_notes = clean_text(raw_notes)
252
+ with st.spinner(f"Drafting with {model_choice}..."):
253
+ prompt = f"TASK: Write email.\nTO: {recipient}\nTOPIC: {topic}\nSTYLE: {selected_persona_instruction}\nDATA: {clean_notes}"
254
+
255
+ reply, usage = ask_ai(prompt, "You are an expert ghostwriter.", max_len)
256
+ st.session_state.email_draft = reply
257
+
258
+ if usage:
259
+ m_name = "Granite" if "Granite" in model_choice else "GPT-4o"
260
+ tracker.log_usage(m_name, usage["input"], usage["output"])
261
+ update_sidebar_metrics() # Force update
262
+
263
+ if st.session_state.email_draft:
264
+ st.subheader("Draft Result")
265
+ st.text_area("Copy your email:", value=st.session_state.email_draft, height=300)
266
+
267
+ # --- TAB 2: CHAT PLAYGROUND ---
268
+ with tab2:
269
+ st.header("Choose Your Model and Start a Discussion")
270
+
271
+ if "chat_response" not in st.session_state:
272
+ st.session_state.chat_response = ""
273
+
274
+ user_input = st.text_input("Ask a question:")
275
+
276
+ c1, c2 = st.columns([1,1])
277
+ with c1:
278
+ use_rag = st.toggle("πŸ”Œ Enable Knowledge Base", value=True)
279
+ with c2:
280
+ est_tokens = len(user_input) / 4
281
+ st.progress(min(est_tokens / 2000, 1.0), text=f"Input: {int(est_tokens)} tokens")
282
+
283
+ if st.button("Send Query"):
284
+ if not user_input:
285
+ st.warning("Please enter a question.")
286
+ else:
287
+ final_prompt = user_input
288
+ system_persona = "You are a helpful assistant."
289
+
290
+ # --- RAG LOGIC ---
291
+ if use_rag:
292
+ with st.spinner("🧠 Searching Knowledge Base..."):
293
+ # 1. Retrieve & Rerank (Now using the fixed function)
294
+ retrieved_docs = rag_engine.search_knowledge_base(
295
+ user_input,
296
+ st.session_state.username,
297
+ k=3
298
+ )
299
+
300
+ if retrieved_docs:
301
+ # 2. Format Context
302
+ context_text = ""
303
+ for i, doc in enumerate(retrieved_docs):
304
+ # Add metadata relevance score if available
305
+ score = doc.metadata.get('relevance_score', 'N/A')
306
+ src = os.path.basename(doc.metadata.get('source', 'Unknown'))
307
+ context_text += f"---\nSOURCE: {src} (Rel: {score})\nTEXT: {doc.page_content}\n"
308
+
309
+ # 3. Update Prompt
310
+ system_persona = (
311
+ "You are a Navy Document Analyst. "
312
+ "Answer the user's question strictly based on the Context provided below. "
313
+ "If the answer is not in the Context, state 'I cannot find that information in the provided documents.' \n\n"
314
+ f"### CONTEXT:\n{context_text}"
315
+ )
316
+ st.success(f"Found {len(retrieved_docs)} relevant documents.")
317
+ with st.expander("View Context Used"):
318
+ st.text(context_text)
319
+ else:
320
+ st.warning("No relevant documents found. Using general knowledge.")
321
+
322
+ # --- GENERATION ---
323
+ with st.spinner(f"Thinking with {model_choice}..."):
324
+ reply, usage = ask_ai(final_prompt, system_persona, max_len)
325
+ st.session_state.chat_response = reply
326
+
327
+ if usage:
328
+ m_name = "Granite" if "Granite" in model_choice else "GPT-4o"
329
+ tracker.log_usage(m_name, usage["input"], usage["output"])
330
+ update_sidebar_metrics()
331
+
332
+ if st.session_state.chat_response:
333
+ st.divider()
334
+ st.markdown("**AI Response:**")
335
+ st.write(st.session_state.chat_response)
336
+
337
+ # --- TAB 3: PROMPT ARCHITECT ---
338
+ with tab3:
339
+ st.header("πŸ› οΈ Mega-Prompt Factory")
340
+ st.info("Build standard templates for NIPRGPT.")
341
+
342
+ c1, c2 = st.columns([1,1])
343
+ with c1:
344
+ st.subheader("1. Parameters")
345
+ p = st.text_area("Persona", placeholder="Act as...", height=100)
346
+ c = st.text_area("Context", placeholder="Background...", height=100)
347
+ t = st.text_area("Task", placeholder="Action...", height=100)
348
+ v = st.text_input("Placeholder Name", value="PASTE_DATA_HERE")
349
+
350
+ with c2:
351
+ st.subheader("2. Result")
352
+ final = f"### ROLE\n{p}\n### CONTEXT\n{c}\n### TASK\n{t}\n### INPUT DATA\n\"\"\"\n[{v}]\n\"\"\""
353
+ st.code(final, language="markdown")
354
+ st.download_button("πŸ’Ύ Download .txt", final, "template.txt")
355
+
356
+ # --- TAB 4: KNOWLEDGE BASE ---
357
+ with tab4:
358
+ st.header("🧠 Unit Knowledge Base")
359
+
360
+ is_admin = "admin" in st.session_state.roles
361
+ kb_tab1, kb_tab2 = st.tabs(["πŸ“€ Add Documents", "πŸ—‚οΈ Manage Database"])
362
+
363
+ # --- SUB-TAB 1: UPLOAD ---
364
+ with kb_tab1:
365
+ if is_admin:
366
+ st.subheader("Ingest New Knowledge")
367
+ uploaded_file = st.file_uploader("Upload Instructions, Manuals, or Logs", type=["pdf", "docx", "txt", "md"])
368
+
369
+ col1, col2 = st.columns([1, 2])
370
+ with col1:
371
+ chunk_strategy = st.selectbox(
372
+ "Chunking Strategy",
373
+ ["paragraph", "token", "page"],
374
+ help="Paragraph: Manuals. Token: Dense text. Page: Forms."
375
+ )
376
+
377
+ if uploaded_file and st.button("Process & Add"):
378
+ with st.spinner("Analyzing and Indexing..."):
379
+ # Use safe save + process
380
+ temp_path = rag_engine.save_uploaded_file(uploaded_file)
381
+ success, msg = rag_engine.process_and_add_document(
382
+ temp_path,
383
+ st.session_state.username,
384
+ chunk_strategy
385
+ )
386
+
387
+ if success:
388
+ st.success(msg)
389
+ st.rerun()
390
+ else:
391
+ st.error(f"Failed: {msg}")
392
+ else:
393
+ st.info("πŸ”’ Only Admins can upload documents.")
394
+
395
+ st.divider()
396
+ st.subheader("πŸ”Ž Quick Test")
397
+ test_query = st.text_input("Ask the brain something...")
398
+ if test_query:
399
+ results = rag_engine.search_knowledge_base(test_query, st.session_state.username)
400
+ for i, doc in enumerate(results):
401
+ # Using cleaned safe basename
402
+ src_name = os.path.basename(doc.metadata.get('source', '?'))
403
+ score = doc.metadata.get('relevance_score', 'N/A')
404
+ with st.expander(f"Match {i+1}: {src_name} (Score: {score})"):
405
+ st.write(doc.page_content)
406
+
407
+ # --- SUB-TAB 2: MANAGE ---
408
+ with kb_tab2:
409
+ st.subheader("πŸ—„οΈ Database Inventory")
410
+
411
+ # 1. Fetch current docs
412
+ docs = rag_engine.list_documents(st.session_state.username)
413
+
414
+ if not docs:
415
+ st.info("Knowledge Base is empty.")
416
+ else:
417
+ st.markdown(f"**Total Documents:** {len(docs)}")
418
+
419
+ for doc in docs:
420
+ c1, c2, c3 = st.columns([3, 1, 1])
421
+ with c1:
422
+ st.text(f"πŸ“„ {doc['filename']}")
423
+ with c2:
424
+ st.caption(f"{doc['chunks']} chunks")
425
+ with c3:
426
+ if is_admin:
427
+ if st.button("πŸ—‘οΈ Delete", key=doc['source']):
428
+ with st.spinner("Deleting..."):
429
+ success, msg = rag_engine.delete_document(st.session_state.username, doc['source'])
430
+ if success:
431
+ st.success(msg)
432
+ st.rerun()
433
+ else:
434
+ st.error(msg)
435
+ else:
436
+ st.caption("Read Only")
437
+
438
+ if is_admin and docs:
439
+ st.divider()
440
+ with st.expander("🚨 Danger Zone"):
441
+ if st.button("☒️ RESET ENTIRE DATABASE", type="primary"):
442
+ success, msg = rag_engine.reset_knowledge_base(st.session_state.username)
443
+ if success:
444
+ st.success(msg)
445
+ st.rerun()