Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,330 +1,155 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import hashlib
|
| 4 |
-
import time
|
| 5 |
-
from datetime import datetime
|
| 6 |
import gradio as gr
|
| 7 |
-
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
-
from
|
| 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 |
-
if os.path.exists(manifest_path(case_id)):
|
| 87 |
-
with open(manifest_path(case_id), "r") as f:
|
| 88 |
-
manifest = json.load(f)
|
| 89 |
-
else:
|
| 90 |
-
manifest = []
|
| 91 |
-
|
| 92 |
-
prev_hash = manifest[-1]["entry_hash"] if manifest else "GENESIS"
|
| 93 |
-
|
| 94 |
-
entry = {
|
| 95 |
-
"file": dest,
|
| 96 |
-
"sha256": file_hash,
|
| 97 |
-
"operator": operator,
|
| 98 |
-
"notes": notes,
|
| 99 |
-
"timestamp": datetime.utcnow().isoformat(),
|
| 100 |
-
"previous_hash": prev_hash
|
| 101 |
}
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
with open(manifest_path(case_id), "w") as f:
|
| 109 |
-
json.dump(manifest, f, indent=2)
|
| 110 |
-
|
| 111 |
-
append_audit_log(case_id, "EVIDENCE_REGISTERED", entry)
|
| 112 |
-
|
| 113 |
-
return f"Evidence registered.\nSHA256: {file_hash}"
|
| 114 |
-
|
| 115 |
-
# ============================================================
|
| 116 |
-
# HASH-CHAINED AUDIT LOG
|
| 117 |
-
# ============================================================
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
log = []
|
| 125 |
-
|
| 126 |
-
prev_hash = log[-1]["entry_hash"] if log else "GENESIS"
|
| 127 |
-
|
| 128 |
-
entry = {
|
| 129 |
-
"timestamp": datetime.utcnow().isoformat(),
|
| 130 |
-
"action": action,
|
| 131 |
-
"details": details,
|
| 132 |
-
"previous_hash": prev_hash
|
| 133 |
-
}
|
| 134 |
-
|
| 135 |
-
entry_hash = sha256_data(json.dumps(entry))
|
| 136 |
-
entry["entry_hash"] = entry_hash
|
| 137 |
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
# ============================================================
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
if os.path.exists(manifest_path(case_id)):
|
| 150 |
-
with open(manifest_path(case_id), "r") as f:
|
| 151 |
-
manifest = json.load(f)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
with open(audit_path(case_id), "r") as f:
|
| 160 |
-
log = json.load(f)
|
| 161 |
|
| 162 |
-
|
| 163 |
-
if log[i]["previous_hash"] != log[i-1]["entry_hash"]:
|
| 164 |
-
return "Audit chain broken."
|
| 165 |
|
| 166 |
-
|
|
|
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
# ============================================================
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
client = get_client(model_id)
|
| 179 |
-
response = client.chat_completion(
|
| 180 |
-
messages=messages,
|
| 181 |
-
max_tokens=1000,
|
| 182 |
-
temperature=0.4,
|
| 183 |
-
top_p=0.95,
|
| 184 |
-
)
|
| 185 |
-
return response.choices[0].message.content
|
| 186 |
-
except:
|
| 187 |
-
fallback = get_client(FALLBACK_MODEL)
|
| 188 |
-
response = fallback.chat_completion(
|
| 189 |
-
messages=messages,
|
| 190 |
-
max_tokens=1000,
|
| 191 |
-
temperature=0.4,
|
| 192 |
-
top_p=0.95,
|
| 193 |
-
)
|
| 194 |
-
return "Fallback model used.\n\n" + response.choices[0].message.content
|
| 195 |
-
|
| 196 |
-
def ai_analysis(case_id, input_text, model_label):
|
| 197 |
-
system_prompt = """
|
| 198 |
-
You are an AI augmentation module for digital forensics.
|
| 199 |
-
Structure output with clear analytical sections.
|
| 200 |
-
AI does not modify evidence.
|
| 201 |
-
"""
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
|
| 208 |
-
|
| 209 |
|
| 210 |
-
|
| 211 |
-
notes_path = ai_notes_path(case_id)
|
| 212 |
-
if os.path.exists(notes_path):
|
| 213 |
-
with open(notes_path, "r") as f:
|
| 214 |
-
notes = json.load(f)
|
| 215 |
-
else:
|
| 216 |
-
notes = []
|
| 217 |
|
| 218 |
-
|
| 219 |
-
"timestamp": datetime.utcnow().isoformat(),
|
| 220 |
-
"analysis": result
|
| 221 |
-
}
|
| 222 |
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
return result
|
| 231 |
-
|
| 232 |
-
# ============================================================
|
| 233 |
-
# CASE TIMELINE
|
| 234 |
-
# ============================================================
|
| 235 |
-
|
| 236 |
-
def case_timeline(case_id):
|
| 237 |
-
events = []
|
| 238 |
-
|
| 239 |
-
if os.path.exists(audit_path(case_id)):
|
| 240 |
-
with open(audit_path(case_id), "r") as f:
|
| 241 |
-
log = json.load(f)
|
| 242 |
-
for entry in log:
|
| 243 |
-
events.append(f"{entry['timestamp']} - {entry['action']}")
|
| 244 |
-
|
| 245 |
-
return "\n".join(sorted(events))
|
| 246 |
-
|
| 247 |
-
# ============================================================
|
| 248 |
-
# PDF EXPORT
|
| 249 |
-
# ============================================================
|
| 250 |
-
|
| 251 |
-
def export_case_pdf(case_id):
|
| 252 |
-
path = case_path(case_id)
|
| 253 |
-
pdf_path = os.path.join(path, f"{case_id}_report.pdf")
|
| 254 |
-
|
| 255 |
-
doc = SimpleDocTemplate(pdf_path)
|
| 256 |
-
elements = []
|
| 257 |
-
styles = getSampleStyleSheet()
|
| 258 |
-
|
| 259 |
-
elements.append(Paragraph(f"Case Report: {case_id}", styles["Heading1"]))
|
| 260 |
-
elements.append(Spacer(1, 0.3 * inch))
|
| 261 |
-
|
| 262 |
-
for file_name in ["manifest.json", "audit_log.json", "ai_analysis.json"]:
|
| 263 |
-
file_path = os.path.join(path, file_name)
|
| 264 |
-
if os.path.exists(file_path):
|
| 265 |
-
elements.append(Paragraph(file_name, styles["Heading2"]))
|
| 266 |
-
with open(file_path, "r") as f:
|
| 267 |
-
data = json.load(f)
|
| 268 |
-
elements.append(Paragraph(str(data), styles["Normal"]))
|
| 269 |
-
elements.append(Spacer(1, 0.3 * inch))
|
| 270 |
-
|
| 271 |
-
doc.build(elements)
|
| 272 |
-
return pdf_path
|
| 273 |
-
|
| 274 |
-
# ============================================================
|
| 275 |
-
# UI
|
| 276 |
-
# ============================================================
|
| 277 |
-
|
| 278 |
-
with gr.Blocks(title="Omniscient Investigative Infrastructure vNext") as demo:
|
| 279 |
-
|
| 280 |
-
gr.Markdown("## Omniscient – Advanced Case-Based Investigative Platform")
|
| 281 |
-
|
| 282 |
-
case_id = gr.Textbox(label="Case ID")
|
| 283 |
-
|
| 284 |
-
with gr.Tabs():
|
| 285 |
-
|
| 286 |
-
with gr.Tab("Case Management"):
|
| 287 |
-
create_btn = gr.Button("Create Case")
|
| 288 |
-
list_btn = gr.Button("List Cases")
|
| 289 |
-
case_output = gr.Textbox(lines=6)
|
| 290 |
-
|
| 291 |
-
with gr.Tab("Evidence"):
|
| 292 |
-
operator = gr.Textbox(label="Operator Name")
|
| 293 |
-
notes = gr.Textbox(label="Evidence Notes")
|
| 294 |
-
file_upload = gr.File(label="Upload Evidence")
|
| 295 |
-
register_btn = gr.Button("Register Evidence")
|
| 296 |
-
evidence_output = gr.Textbox(lines=10)
|
| 297 |
-
|
| 298 |
-
with gr.Tab("AI Augmentation"):
|
| 299 |
-
ai_input = gr.Textbox(lines=8)
|
| 300 |
-
model_selector = gr.Dropdown(
|
| 301 |
-
choices=list(MODEL_OPTIONS.keys()),
|
| 302 |
-
value=list(MODEL_OPTIONS.keys())[0]
|
| 303 |
-
)
|
| 304 |
-
ai_btn = gr.Button("Run AI Analysis")
|
| 305 |
-
ai_output = gr.Textbox(lines=20)
|
| 306 |
-
|
| 307 |
-
with gr.Tab("Integrity"):
|
| 308 |
-
verify_btn = gr.Button("Verify Full Integrity")
|
| 309 |
-
timeline_btn = gr.Button("View Case Timeline")
|
| 310 |
-
integrity_output = gr.Textbox(lines=15)
|
| 311 |
-
|
| 312 |
-
with gr.Tab("Export"):
|
| 313 |
-
export_btn = gr.Button("Export Case PDF")
|
| 314 |
-
export_file = gr.File()
|
| 315 |
-
|
| 316 |
-
create_btn.click(create_case, inputs=case_id, outputs=case_output)
|
| 317 |
-
list_btn.click(list_cases, outputs=case_output)
|
| 318 |
-
register_btn.click(register_evidence,
|
| 319 |
-
inputs=[case_id, file_upload, operator, notes],
|
| 320 |
-
outputs=evidence_output)
|
| 321 |
-
ai_btn.click(ai_analysis,
|
| 322 |
-
inputs=[case_id, ai_input, model_selector],
|
| 323 |
-
outputs=ai_output)
|
| 324 |
-
verify_btn.click(verify_integrity, inputs=case_id, outputs=integrity_output)
|
| 325 |
-
timeline_btn.click(case_timeline, inputs=case_id, outputs=integrity_output)
|
| 326 |
-
export_btn.click(export_case_pdf, inputs=case_id, outputs=export_file)
|
| 327 |
-
|
| 328 |
-
if __name__ == "__main__":
|
| 329 |
-
demo.queue()
|
| 330 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
from duckduckgo_search import DDGS
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import numpy as np
|
| 8 |
+
import re
|
| 9 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 10 |
+
|
| 11 |
+
# -------------------------
|
| 12 |
+
# Load Models (HF Safe)
|
| 13 |
+
# -------------------------
|
| 14 |
+
|
| 15 |
+
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 16 |
+
|
| 17 |
+
llm = pipeline(
|
| 18 |
+
"text-generation",
|
| 19 |
+
model="HuggingFaceH4/zephyr-7b-beta",
|
| 20 |
+
max_new_tokens=512,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# -------------------------
|
| 24 |
+
# Simple In-Memory Vector Store
|
| 25 |
+
# -------------------------
|
| 26 |
+
|
| 27 |
+
documents = []
|
| 28 |
+
embeddings = []
|
| 29 |
+
|
| 30 |
+
def add_to_rag(text):
|
| 31 |
+
global documents, embeddings
|
| 32 |
+
documents.append(text)
|
| 33 |
+
embeddings.append(embedder.encode(text))
|
| 34 |
+
|
| 35 |
+
def retrieve_from_rag(query, top_k=3):
|
| 36 |
+
if not embeddings:
|
| 37 |
+
return []
|
| 38 |
+
query_emb = embedder.encode(query)
|
| 39 |
+
sims = cosine_similarity([query_emb], embeddings)[0]
|
| 40 |
+
top_idx = np.argsort(sims)[-top_k:][::-1]
|
| 41 |
+
return [documents[i] for i in top_idx]
|
| 42 |
+
|
| 43 |
+
# -------------------------
|
| 44 |
+
# URL Scraper
|
| 45 |
+
# -------------------------
|
| 46 |
+
|
| 47 |
+
def scrape_url(url):
|
| 48 |
+
try:
|
| 49 |
+
r = requests.get(url, timeout=10)
|
| 50 |
+
soup = BeautifulSoup(r.text, "html.parser")
|
| 51 |
+
text = soup.get_text(separator=" ")
|
| 52 |
+
add_to_rag(text)
|
| 53 |
+
return "Scraped and added to RAG."
|
| 54 |
+
except:
|
| 55 |
+
return "Scrape failed."
|
| 56 |
+
|
| 57 |
+
# -------------------------
|
| 58 |
+
# DuckDuckGo Search
|
| 59 |
+
# -------------------------
|
| 60 |
+
|
| 61 |
+
def ddg_search(query):
|
| 62 |
+
results = []
|
| 63 |
+
with DDGS() as ddgs:
|
| 64 |
+
for r in ddgs.text(query, max_results=5):
|
| 65 |
+
results.append(r["body"])
|
| 66 |
+
combined = "\n".join(results)
|
| 67 |
+
add_to_rag(combined)
|
| 68 |
+
return combined
|
| 69 |
+
|
| 70 |
+
# -------------------------
|
| 71 |
+
# Hybrid Entity Extraction
|
| 72 |
+
# -------------------------
|
| 73 |
+
|
| 74 |
+
def regex_entities(text):
|
| 75 |
+
entities = {
|
| 76 |
+
"emails": re.findall(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", text),
|
| 77 |
+
"phones": re.findall(r"\+?\d[\d -]{8,}\d", text),
|
| 78 |
+
"urls": re.findall(r"https?://\S+", text),
|
| 79 |
+
"ips": re.findall(r"\b(?:\d{1,3}\.){3}\d{1,3}\b", text),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
| 81 |
+
return entities
|
| 82 |
|
| 83 |
+
def llm_refine_entities(text):
|
| 84 |
+
prompt = f"""
|
| 85 |
+
Extract structured OSINT entities from this text.
|
| 86 |
+
Return JSON with:
|
| 87 |
+
people, organizations, locations, vehicles, usernames.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
TEXT:
|
| 90 |
+
{text}
|
| 91 |
+
"""
|
| 92 |
+
output = llm(prompt)[0]["generated_text"]
|
| 93 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
def hybrid_extract(text):
|
| 96 |
+
base = regex_entities(text)
|
| 97 |
+
refined = llm_refine_entities(text[:2000])
|
| 98 |
+
return f"Regex Extracted:\n{base}\n\nLLM Refined:\n{refined}"
|
| 99 |
|
| 100 |
+
# -------------------------
|
| 101 |
+
# Chat Logic
|
| 102 |
+
# -------------------------
|
| 103 |
|
| 104 |
+
def chat(query, use_web, use_rag):
|
| 105 |
+
context = ""
|
|
|
|
| 106 |
|
| 107 |
+
if use_web:
|
| 108 |
+
context += ddg_search(query)
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
if use_rag:
|
| 111 |
+
docs = retrieve_from_rag(query)
|
| 112 |
+
context += "\n".join(docs)
|
| 113 |
|
| 114 |
+
final_prompt = f"""
|
| 115 |
+
Use the following context to answer intelligently:
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
{context}
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
Question: {query}
|
| 120 |
+
"""
|
| 121 |
|
| 122 |
+
response = llm(final_prompt)[0]["generated_text"]
|
| 123 |
+
return response
|
|
|
|
| 124 |
|
| 125 |
+
# -------------------------
|
| 126 |
+
# Gradio UI
|
| 127 |
+
# -------------------------
|
| 128 |
|
| 129 |
+
with gr.Blocks() as demo:
|
| 130 |
+
gr.Markdown("# 🔎 Hybrid OSINT AI Assistant")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
+
with gr.Row():
|
| 133 |
+
query = gr.Textbox(label="Ask Question")
|
| 134 |
+
use_web = gr.Checkbox(label="Use DuckDuckGo Search")
|
| 135 |
+
use_rag = gr.Checkbox(label="Use RAG")
|
| 136 |
|
| 137 |
+
chat_btn = gr.Button("Run")
|
| 138 |
|
| 139 |
+
output = gr.Textbox(label="Response")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
chat_btn.click(chat, inputs=[query, use_web, use_rag], outputs=output)
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
+
gr.Markdown("## 🌐 URL → RAG")
|
| 144 |
+
url_input = gr.Textbox(label="Enter URL")
|
| 145 |
+
scrape_btn = gr.Button("Scrape")
|
| 146 |
+
scrape_output = gr.Textbox()
|
| 147 |
+
scrape_btn.click(scrape_url, inputs=url_input, outputs=scrape_output)
|
| 148 |
|
| 149 |
+
gr.Markdown("## 🧩 OSINT Entity Extraction")
|
| 150 |
+
extract_input = gr.Textbox(label="Paste Text")
|
| 151 |
+
extract_btn = gr.Button("Extract Entities")
|
| 152 |
+
extract_output = gr.Textbox()
|
| 153 |
+
extract_btn.click(hybrid_extract, inputs=extract_input, outputs=extract_output)
|
| 154 |
|
| 155 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|