NexusInstruments commited on
Commit
ca88fa5
·
verified ·
1 Parent(s): 63ff4f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -83
app.py CHANGED
@@ -1,88 +1,50 @@
1
- from dotenv import load_dotenv
2
- import streamlit as st
3
- import sys, os, requests, pathlib, contextlib
4
-
5
- # ─── Load Configuration from .env ───────────────────────────────
6
- def load_config():
7
- load_dotenv()
8
- config = {
9
- "APP_MODE": os.getenv("APP_MODE", "streamlit"),
10
- "OLLAMA_MODEL": os.getenv("OLLAMA_MODEL", "llama3"),
11
- "HF_API_KEY": os.getenv("HF_API_KEY", ""),
12
- "BACKEND_URL": os.getenv("BACKEND_URL", "http://localhost:9000"),
13
- "TAIPY_URL": os.getenv("TAIPY_URL", "http://localhost:8080"),
14
- }
15
- return config
16
-
17
- config = load_config()
18
-
19
- # ─── Ensure utils/ importable ───────────────────────────────────
20
- ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
21
- if ROOT_PATH not in sys.path:
22
- sys.path.insert(0, ROOT_PATH)
23
-
24
- # ─── Page Config ────────────────────────────────────────────────
25
- st.set_page_config(page_title="Omniscient Dashboard", layout="wide")
26
-
27
- # ─── Title ──────────────────────────────────────────────────────
28
- st.title("🤖 Omniscient LLM Dashboard")
29
- st.write("Welcome! This Space is running **Streamlit multipage mode** with Taipy integration.")
30
-
31
- # ─── Backend Health Check ───────────────────────────────────────
32
- def backend_status(url):
33
  try:
34
- res = requests.get(f"{url}/health", timeout=3)
35
- return "🟢 Online" if res.ok else "🟠 Unresponsive"
36
- except Exception:
37
- return "🔴 Offline"
38
-
39
- # ─── Metrics Section ────────────────────────────────────────────
40
- col1, col2, col3, col4 = st.columns(4)
41
-
42
- active_sessions = len(st.session_state.get("messages", []))
43
- col1.metric("💬 Active Sessions", active_sessions)
44
-
45
- uploaded_files = len(st.session_state.get("uploaded_files", []))
46
- col2.metric("📂 Uploaded Files", uploaded_files)
47
-
48
- errors = len(st.session_state.get("errors", []))
49
- col3.metric("⚠️ Errors", errors)
50
-
51
- col4.metric("🔌 API Backend", backend_status(config["BACKEND_URL"]))
52
-
53
- st.success("System ready ✅")
54
-
55
- # ─── Dynamic Sidebar Navigation ─────────────────────────────────
56
- st.sidebar.header("📑 Navigation")
57
-
58
- pages_path = os.path.join(os.path.dirname(__file__), "pages")
59
- if os.path.isdir(pages_path):
60
- st.sidebar.write("**Modules:**")
61
- pages = sorted([p.stem for p in pathlib.Path(pages_path).glob("*.py")])
62
- for page in pages:
63
- st.sidebar.markdown(f"- {page}")
64
-
65
- st.sidebar.markdown("---")
66
- st.sidebar.info("🧠 Omniscient Framework v0.1 — AI-driven OSINT & DFIR Hub")
67
-
68
- # ─── Session Debug (optional) ───────────────────────────────────
69
- with st.expander("🧩 Session Diagnostics", expanded=False):
70
- st.json({k: v for k, v in st.session_state.items() if len(str(v)) < 1000})
71
-
72
- # ─── Embed Taipy Realtime Dashboard ─────────────────────────────
73
- st.subheader("📊 Realtime System Dashboard (Taipy)")
74
 
75
- space_id = None
76
- with contextlib.suppress(Exception):
77
- secrets_obj = getattr(st, "secrets", None)
78
- if secrets_obj:
79
- with contextlib.suppress(Exception):
80
- space_id = secrets_obj.get("SPACE_ID", None)
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- if space_id:
83
- st.info("Taipy dashboard is disabled on Hugging Face Spaces (requires local port).")
84
- else:
85
  try:
86
- st.components.v1.iframe(config["TAIPY_URL"], height=600)
 
 
87
  except Exception as e:
88
- st.warning(f"Unable to load Taipy dashboard: {e}")
 
1
+ from fastapi import FastAPI, UploadFile, Form
2
+ from fastapi.responses import JSONResponse
3
+ from utils.summarizer import summarize_text
4
+ from utils.file_utils import normalize_log_line, keyword_search
5
+ from utils.docgen import generate_doc
6
+
7
+ app = FastAPI(title="Omniscient Backend API")
8
+
9
+ # ─── Health Check ─────────────────────────────────────────────
10
+ @app.get("/health")
11
+ def health():
12
+ return JSONResponse({"ok": True, "message": "Omniscient backend alive"})
13
+
14
+ # ─── File Summarization ───────────────────────────────────────
15
+ @app.post("/summarize")
16
+ async def summarize(file: UploadFile):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  try:
18
+ content = (await file.read()).decode("utf-8", errors="ignore")
19
+ summary = summarize_text(content)
20
+ return {"file": file.filename, "summary": summary}
21
+ except Exception as e:
22
+ return {"error": str(e)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ # ─── Log Normalization & Search ───────────────────────────────
25
+ @app.post("/analyze-log")
26
+ async def analyze_log(file: UploadFile, query: str = Form(None)):
27
+ try:
28
+ content = (await file.read()).decode("utf-8", errors="ignore")
29
+ normalized = [normalize_log_line(line) for line in content.splitlines()]
30
+ result = {
31
+ "total_lines": len(normalized),
32
+ "unique_entries": len(set(normalized)),
33
+ "preview": normalized[:50],
34
+ }
35
+ if query:
36
+ matches = keyword_search("\n".join(normalized), query)
37
+ result["matches"] = matches[:50]
38
+ return {"file": file.filename, "analysis": result}
39
+ except Exception as e:
40
+ return {"error": str(e)}
41
 
42
+ # ─── Script Documentation Generator ──────────────────────────
43
+ @app.post("/generate-doc")
44
+ async def gen_doc(file: UploadFile):
45
  try:
46
+ content = (await file.read()).decode("utf-8", errors="ignore")
47
+ doc = generate_doc(file.filename, "uploaded", content)
48
+ return {"file": file.filename, "doc": doc}
49
  except Exception as e:
50
+ return {"error": str(e)}