Spaces:
Paused
Paused
Replace Gemini with in-Space open-source LLM (Qwen3.5-2B via llama.cpp)
Browse filesFiles changed (3)
- Dockerfile+1
- app.py+313-218
- requirements.txt+10-4
9 unmodified lines1011121314159 unmodified linesffmpeg \libsm6 \libxext6 \&& rm -rf /var/lib/apt/lists/* \&& git lfs install9 unmodified lines101112131415169 unmodified linesffmpeg \libsm6 \libxext6 \libgomp1 \&& rm -rf /var/lib/apt/lists/* \&& git lfs install
11 unmodified lines12131415161715 unmodified lines3334353637383940414243444514 unmodified lines606162636465666768697015 unmodified lines868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521696 unmodified lines3133143153163173183193203213223238 unmodified lines332333334335336337338228 unmodified lines56756856957057157257357410 unmodified lines58558658758858959059159259359459559659759895 unmodified lines694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746280 unmodified lines102710281029103010311032103319 unmodified lines105310541055105610571058105971 unmodified lines11311132113311341135113611373 unmodified lines114111421143114411451146114752 unmodified lines12001201120212031204120512061207120812091210121112121213121412151216121712181219104 unmodified lines132413251326132713281329133051 unmodified lines1382138313841385138613871388138913901391139213931394111 unmodified lines15061507150815091510151115121513160 unmodified lines167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516 unmodified lines1712171317141715171617171718171917201721172217231724172510 unmodified lines173617371738173917401741174217434 unmodified lines174817491750175117521753175417553 unmodified lines175917601761176217631764176517662 unmodified lines1769177017711772177317741775177694 unmodified lines1871187218731874187518761877187818799 unmodified lines188918901891189218931894189535 unmodified lines1931193219331934193519361937193811 unmodified linesimport osimport subprocessimport sysfrom pathlib import Pathfrom datetime import datetimeimport logging15 unmodified linesfrom fastapi import FastAPI, Requestfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.responses import JSONResponse# Gemini AI (new SDK: google-genai)try:from google import genaiGEMINI_AVAILABLE = Trueexcept ImportError:GEMINI_AVAILABLE = Falselogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)14 unmodified linesTRAINING_DATA_DIR.mkdir(exist_ok=True)# EnvironmentGEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")GEMINI_MODEL = os.environ.get("GEMINI_MODEL", "gemini-2.5-flash")HF_TOKEN = os.environ.get("HF_TOKEN", "")HF_MODEL_REPO = os.environ.get("HF_MODEL_REPO", "Che237/cyberforge-models")logger.info(f"APP_DIR: {APP_DIR}")logger.info(f"NOTEBOOKS_DIR: {NOTEBOOKS_DIR}")logger.info(f"NOTEBOOKS_DIR exists: {NOTEBOOKS_DIR.exists()}")15 unmodified lines# ============================================================================# GEMINI SERVICE (for REST API)# ============================================================================class GeminiService:"""Google Gemini AI for cybersecurity chat and analysis"""SYSTEM_PROMPT = """You are CyberForge AI, an advanced cybersecurity expert. You specialize in:- Real-time threat detection and analysis- Malware and phishing identification- Network security assessment- Browser security monitoring- Risk assessment and mitigationWhen analyzing security queries, provide:1. Risk Level (Critical/High/Medium/Low)2. Threat Types identified3. Confidence Score (0.0-1.0)4. Detailed technical analysis5. Specific actionable recommendationsAlways be precise, professional, and actionable."""def __init__(self):self.client = Noneself.ready = Falseself.custom_knowledge = {}self.training_examples = []def initialize(self):if not GEMINI_AVAILABLE:logger.warning("google-genai not installed, Gemini unavailable")returnif not GEMINI_API_KEY:logger.warning("GEMINI_API_KEY not set, Gemini unavailable")returntry:self.client = genai.Client(api_key=GEMINI_API_KEY)resp = self.client.models.generate_content(model=GEMINI_MODEL,contents="Test. Respond with OK.")if resp.text:self.ready = Truelogger.info(f"✅ Gemini initialized (model: {GEMINI_MODEL})")self._load_knowledge()except Exception as e:logger.error(f"❌ Gemini init failed: {e}")self.ready = Falsedef _load_knowledge(self):try:for f in KNOWLEDGE_BASE_DIR.glob("*.json"):with open(f) as fh:self.custom_knowledge[f.stem] = json.load(fh)logger.info(f"Loaded {len(self.custom_knowledge)} knowledge files")except Exception as e:logger.warning(f"Knowledge load error: {e}")try:for f in TRAINING_DATA_DIR.glob("*.json"):with open(f) as fh:data = json.load(fh)if isinstance(data, list):self.training_examples.extend(data)else:self.training_examples.append(data)logger.info(f"Loaded {len(self.training_examples)} training examples")except Exception as e:logger.warning(f"Training data load error: {e}")def analyze(self, query: str, context: Dict = None, history: List = None) -> Dict:if not self.ready:return self._fallback(query)try:knowledge_str = json.dumps(self.custom_knowledge, indent=1)[:2000] if self.custom_knowledge else "None"examples_str = "\n".join(f"Q: {ex.get('input','')}\nA: {ex.get('output','')}"for ex in self.training_examples[-3:]) if self.training_examples else "None"context_str = f"\nCONTEXT:\n{json.dumps(context, indent=1)[:3000]}\n" if context else ""prompt = f"""{self.SYSTEM_PROMPT}KNOWLEDGE BASE (summary):{knowledge_str}TRAINING EXAMPLES:{examples_str}{context_str}USER QUERY:{query}Provide a comprehensive cybersecurity analysis:"""response = self.client.models.generate_content(model=GEMINI_MODEL,contents=prompt,config={"temperature": 0.3, "max_output_tokens": 2048},)text = response.text if response.text else "No response generated"text_lower = text.lower()if "critical" in text_lower:risk_level, risk_score = "Critical", 9.0elif "high" in text_lower:risk_level, risk_score = "High", 7.0elif "medium" in text_lower:risk_level, risk_score = "Medium", 5.0else:risk_level, risk_score = "Low", 3.0return {"response": text,"confidence": 0.85,"risk_level": risk_level,"risk_score": risk_score,"insights": [f"Analysis performed by Gemini ({GEMINI_MODEL})"],"recommendations": [],"model_used": GEMINI_MODEL,"timestamp": datetime.utcnow().isoformat(),}except Exception as e:logger.error(f"Gemini analysis error: {e}")return self._fallback(query)def _fallback(self, query: str) -> Dict:"""ML-powered analysis when Gemini is unavailable"""import reglobal ml_loader96 unmodified lines"- URL threat analysis (phishing, malware, web attacks)\n""- Network anomaly detection\n""- Real-time threat event monitoring\n\n""*AI chat (Gemini) is currently unavailable. Provide a URL for full ML-based analysis.*")insights = [f"{n_loaded} ML models active"]response_lines.append("\n---\n*Powered by CyberForge ML models — Gemini AI offline.*")return {"response": "\n".join(response_lines),8 unmodified lines# Singletongemini_service = GeminiService()# ============================================================================228 unmodified lines## Loads pretrained BERT-based classifiers from the Hugging Face Hub.# Models are loaded lazily on first request to keep cold-start fast.# A 7B Security LLM is NOT loaded locally (too big for free tier) —# it's accessed via the HF Inference API on demand.class TransformerModelLoader:"""Loads pretrained Transformer classifiers from the HF Hub on demand."""10 unmodified lines"desc": "BERT-based URL phishing/malware classifier",},}# Model used via HF Inference API. ZySec-AI/SecurityLLM isn't served on the# free Inference API tier (404). Mistral-7B-Instruct-v0.3 is widely available# and works well for cyber Q&A. Override via SECURITY_LLM_MODEL env var.SECURITY_LLM_REPO = os.environ.get("SECURITY_LLM_MODEL", "mistralai/Mistral-7B-Instruct-v0.3")def __init__(self):self.pipelines = {} # name → transformers.Pipeline (lazy-loaded)self.load_errors = {} # name → last error messageself.transformers_available = Falsetry:import transformers # noqa: F40195 unmodified lines"inference_source": "entropy-heuristic",}def security_chat(self, query: str, max_tokens: int = 512) -> Dict:"""Cybersecurity Q&A via ZySec-AI/SecurityLLM hosted on HF Inference API.Falls back to Gemini when the LLM is rate-limited or HF token is missing."""if not HF_TOKEN:return {"model": "security-llm","response": None,"error": "HF_TOKEN env var required to call ZySec-AI/SecurityLLM via Inference API","fallback_available": gemini_service.ready,}try:import requestsurl = f"https://api-inference.huggingface.co/models/{self.SECURITY_LLM_REPO}"headers = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}payload = {"inputs": query[:2000],"parameters": {"max_new_tokens": max_tokens, "temperature": 0.3, "return_full_text": False},"options": {"wait_for_model": True},}r = requests.post(url, headers=headers, json=payload, timeout=45)if r.status_code == 200:data = r.json()text = data[0].get("generated_text") if isinstance(data, list) and data else (data.get("generated_text") if isinstance(data, dict) else str(data))return {"model": "security-llm","source": "huggingface_inference_api","response": text,"model_id": self.SECURITY_LLM_REPO,}return {"model": "security-llm","error": f"HF Inference API HTTP {r.status_code}: {r.text[:200]}","fallback_available": gemini_service.ready,}except Exception as e:return {"model": "security-llm","error": f"{type(e).__name__}: {str(e)[:200]}","fallback_available": gemini_service.ready,}def status(self) -> Dict:return {"transformers_available": self.transformers_available,"loaded": list(self.pipelines.keys()),"available": list(self.REGISTRY.keys()) + ["security_llm (via HF Inference API)"],"load_errors": self.load_errors,}@staticmethod280 unmodified lines"status": "healthy","timestamp": datetime.utcnow().isoformat(),"services": {"gemini": gemini_service.ready,"ml_models": ml_loader.ready,"models_loaded": list(ml_loader.models.keys()),"transformers": transformer_loader.transformers_available,19 unmodified linesquery = body.get("query", "")context = body.get("context", {})history = body.get("conversation_history", [])result = gemini_service.analyze(query, context, history)return result71 unmodified lines"""Threat scanning – called by backend mlService.scanForThreats()"""body = await request.json()query = json.dumps(body.get("data", body), indent=1)[:3000]result = gemini_service.analyze(f"Scan these indicators for threats:\n{query}")return result3 unmodified linesbody = await request.json()query = body.get("query", "")context = body.get("context", {})result = gemini_service.analyze(query, context)return {"insights": result.get("response", ""),"confidence": result.get("confidence", 0),52 unmodified lines@api.post("/api/v2/security-chat")async def api_v2_security_chat(request: Request):"""Cybersecurity Q&A via ZySec-AI/SecurityLLM (HF Inference API).Body: { "query": "...", "max_tokens": 512 }"""body = await request.json()query = body.get("query", "").strip()if not query:return JSONResponse(status_code=400, content={"detail": "query required"})max_tokens = int(body.get("max_tokens", 512))result = transformer_loader.security_chat(query, max_tokens=max_tokens)# Auto-fallback to Gemini when LLM unavailableif result.get("error") and gemini_service.ready:gemini_result = gemini_service.analyze(query)gemini_result["source"] = "gemini-fallback"gemini_result["llm_error"] = result["error"]return gemini_resultreturn result104 unmodified lines"domain": "example.com", # optional"ip": "1.2.3.4", # optional"hash": "abc123...", # optional (future use)"context": { ... } # optional — passed to Gemini if available}Response: per-indicator results with unified risk summary.51 unmodified lineselse:risk_level = "unknown"# Optional Gemini enrichment when availableai_analysis = Noneif gemini_service.ready and (url or domain):target = url or domainai_analysis = gemini_service.analyze(f"Scan these IOC indicators for threats: url={target}, domain={effective_domain}",context=context,)return {111 unmodified linespersistent vector store, which requires paid GPU on free tier).2. Translates raw ML signals / IOCs into plain human language when theuser includes raw data and asks for an explanation.3. Falls back to the Security LLM (Mistral-7B via HF Inference API)or the ML-powered heuristic analyzer when Gemini is offline.NOTE ON REAL-TIME "LEARNING": HuggingFace Spaces cpu-basic does NOTsupport fine-tuning or persistent model updates at runtime. What this160 unmodified linesn_models = len(ml_loader.models)sources.append({"type": "ml_models", "label": "ML Models", "value": f"{n_models}/4 loaded"})# ── LLM cascade: Gemini → Security LLM → ML heuristic ────────────# Path 1: Gemini (premium, requires GEMINI_API_KEY env var)if gemini_service.ready:result = gemini_service.analyze(enriched_message, context=context, history=history)# Merge detected risk if ML gave a stronger signalif detected_risk_score > 0 and detected_risk_level != "unknown":ml_score_100 = round(detected_risk_score * 100, 1)if ml_score_100 > result.get("risk_score", 0):result["risk_score"] = ml_score_100result["risk_level"] = detected_risk_levelresult["session_id"] = session_idresult["sources"] = sourcesreturn result# Path 2: Security LLM via HF Inference API (Mistral-7B)llm_result = transformer_loader.security_chat(enriched_message, max_tokens=600)if llm_result.get("response") and not llm_result.get("error"):text = llm_result["response"]text_lower = text.lower()16 unmodified lines"confidence": 0.72,"risk_level": risk_level,"risk_score": risk_score,"model_used": llm_result.get("model_id", transformer_loader.SECURITY_LLM_REPO),"session_id": session_id,"sources": sources,"timestamp": datetime.utcnow().isoformat(),}# Path 3: ML heuristic fallback (always available)fallback = gemini_service._fallback(enriched_message)# Override with ML scan risk if availableif detected_risk_score > 0:ml_score_100 = round(detected_risk_score * 100, 1)10 unmodified lines"""Network traffic analysis – called by backend mlService.analyzeNetworkTraffic()"""body = await request.json()traffic_data = body.get("traffic_data", {})result = gemini_service.analyze(f"Analyze this network traffic for security threats:\n{json.dumps(traffic_data, indent=1)[:3000]}")return result4 unmodified linesbody = await request.json()task_type = body.get("task_type", "")task_data = body.get("task_data", {})result = gemini_service.analyze(f"Execute cybersecurity task '{task_type}':\n{json.dumps(task_data, indent=1)[:3000]}")return result3 unmodified lines"""Browser session analysis"""body = await request.json()session = body.get("session_data", body)result = gemini_service.analyze(f"Analyze this browser session for security threats:\n{json.dumps(session, indent=1)[:3000]}")return result2 unmodified linesasync def api_analyze_threat_feed(request: Request):"""Threat feed analysis"""body = await request.json()result = gemini_service.analyze(f"Analyze this threat feed:\n{json.dumps(body, indent=1)[:3000]}")return result94 unmodified lines| Method | Endpoint | Description ||--------|----------|-------------|| GET | `/health` | Health check — version, models, services || POST | `/analyze` | AI chat / general analysis (Gemini or ML fallback) || POST | `/analyze-url` | URL threat analysis — 4 ML models + risk factors || POST | `/scan-threats` | Deep threat scan (Gemini or ML fallback) || POST | `/api/insights/generate` | AI-generated security insights || POST | `/api/models/predict` | Direct ML model prediction || GET | `/models` | List loaded models with source info |9 unmodified lines| POST | `/api/v2/ioc-scan` | Multi-IOC scanner: URL + domain + DGA + BERT || POST | `/api/v2/url-enrich` | Enriched analysis: ML + BERT + DGA + explainability || POST | `/api/v2/chat` | Security chatbot — context-grounded, RAG-lite, machine→human translation || POST | `/api/v2/security-chat` | Security LLM Q&A (Mistral-7B via HF Inference API) || GET | `/api/v2/status` | Transformer model load status |### Quick Examples35 unmodified lines# Initialize services at startuplogger.info("🚀 Initializing CyberForge AI services...")gemini_service.initialize()ml_loader.initialize()logger.info("✅ Services initialized")# Create Gradio app and mount it on FastAPI11 unmodified lines121314151617181915 unmodified lines3536373839404114 unmodified lines5657585960616263646566676869707172737475767715 unmodified lines9394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114296 unmodified lines2392402412422432442452462472482498 unmodified lines258259260261262263264228 unmodified lines49349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064110 unmodified lines65265365465565665765865966066166266366466566666766895 unmodified lines764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846280 unmodified lines11271128112911301131113211331134113519 unmodified lines115511561157115811591160116171 unmodified lines12331234123512361237123812393 unmodified lines124312441245124612471248124952 unmodified lines130213031304130513061307130813091310131113121313131413151316131713181319104 unmodified lines142414251426142714281429143051 unmodified lines148214831484148514861487148814891490149114921493149414951496111 unmodified lines160816091610161116121613161416151616160 unmodified lines17771778177917801781178217831784178516 unmodified lines1802180318041805180618071808180918101811181218131814181510 unmodified lines1826182718281829183018311832183318344 unmodified lines1839184018411842184318441845184618473 unmodified lines1851185218531854185518561857185818592 unmodified lines18621863186418651866186718681869187094 unmodified lines1965196619671968196919701971197219739 unmodified lines198319841985198619871988198935 unmodified lines20252026202720282029203020312032203311 unmodified linesimport osimport subprocessimport sysimport threadingimport timefrom pathlib import Pathfrom datetime import datetimeimport logging15 unmodified linesfrom fastapi import FastAPI, Requestfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.responses import JSONResponsefrom starlette.concurrency import run_in_threadpoollogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)14 unmodified linesTRAINING_DATA_DIR.mkdir(exist_ok=True)# EnvironmentHF_TOKEN = os.environ.get("HF_TOKEN", "")HF_MODEL_REPO = os.environ.get("HF_MODEL_REPO", "Che237/cyberforge-models")# Open-source chat model that runs inside this Space on CPU (llama.cpp, GGUF).# It replaces Gemini and needs no API key or credit. Any GGUF chat model can be# set here; on the free cpu-basic tier (2 vCPU) stay around 2B parameters or the# answers will not fit inside the callers' timeouts.LOCAL_LLM_REPO = os.environ.get("LOCAL_LLM_REPO", "unsloth/Qwen3.5-2B-GGUF")LOCAL_LLM_FILE = os.environ.get("LOCAL_LLM_FILE", "Qwen3.5-2B-Q4_K_M.gguf")LOCAL_LLM_CTX = int(os.environ.get("LOCAL_LLM_CTX", "4096"))LOCAL_LLM_THREADS = int(os.environ.get("LOCAL_LLM_THREADS", "2"))LOCAL_LLM_MAX_TOKENS = int(os.environ.get("LOCAL_LLM_MAX_TOKENS", "320"))LOCAL_LLM_MAX_INPUT_CHARS = int(os.environ.get("LOCAL_LLM_MAX_INPUT_CHARS", "2400"))LOCAL_LLM_TIME_BUDGET_S = float(os.environ.get("LOCAL_LLM_TIME_BUDGET_S", "22"))LOCAL_LLM_QUEUE_TIMEOUT_S = float(os.environ.get("LOCAL_LLM_QUEUE_TIMEOUT_S", "5"))logger.info(f"APP_DIR: {APP_DIR}")logger.info(f"NOTEBOOKS_DIR: {NOTEBOOKS_DIR}")logger.info(f"NOTEBOOKS_DIR exists: {NOTEBOOKS_DIR.exists()}")15 unmodified lines# ============================================================================# REASONING SERVICE (LLM-backed analysis for the REST API)# ============================================================================class ReasoningService:"""Structured cybersecurity analysis for the REST endpoints.The text comes from TransformerModelLoader.security_chat: the hosted modelwhen the account has Inference Providers credit, otherwise the open-sourcemodel running inside this Space. If no LLM answers, the ML/keyword heuristicin _fallback is used."""def analyze(self, query: str, context: Dict = None, history: List = None,allow_local: bool = True) -> Dict:tl = globals().get("transformer_loader")if tl is None:return self._fallback(query)ctx = f"\n\nCONTEXT:\n{json.dumps(context, indent=1)[:2000]}" if context else ""llm = tl.security_chat(f"{query}{ctx}", max_tokens=700, allow_local=allow_local)text = llm.get("response")if not text or llm.get("error"):return self._fallback(query)tlow = text.lower()if "critical" in tlow:risk_level, risk_score = "Critical", 9.0elif "high" in tlow:risk_level, risk_score = "High", 7.0elif "medium" in tlow:risk_level, risk_score = "Medium", 5.0else:risk_level, risk_score = "Low", 3.0model_id = llm.get("model_id", "llm")return {"response": text,"confidence": 0.82,"risk_level": risk_level,"risk_score": risk_score,"insights": [f"Analysis by {model_id} via {llm.get('source', 'llm')}"],"recommendations": [],"model_used": model_id,"timestamp": datetime.utcnow().isoformat(),}def _fallback(self, query: str) -> Dict:"""ML-powered analysis when no LLM is available"""import reglobal ml_loader96 unmodified lines"- URL threat analysis (phishing, malware, web attacks)\n""- Network anomaly detection\n""- Real-time threat event monitoring\n\n""*The AI chat model is currently unavailable. Provide a URL for full ML-based analysis.*")insights = [f"{n_loaded} ML models active"]response_lines.append("\n---\n*Powered by CyberForge ML models — AI chat model offline.*")return {"response": "\n".join(response_lines),8 unmodified lines# Singletonreasoning_service = ReasoningService()# ============================================================================228 unmodified lines## Loads pretrained BERT-based classifiers from the Hugging Face Hub.# Models are loaded lazily on first request to keep cold-start fast.# The chat LLM is separate: see LocalLLM below (runs in this Space) and# TransformerModelLoader.security_chat (hosted model first, then LocalLLM).class LocalLLM:"""Open-source chat model served from inside the Space with llama.cpp.The GGUF file is downloaded from the Hub once and kept in memory. Generationis serialized (a llama.cpp context is not thread-safe) and time-boxed, so aslow CPU cannot hold a request past the callers' timeouts."""def __init__(self):self.llm = Noneself.error: Optional[str] = Noneself._chatml = Falseself._prefill = ""self._load_lock = threading.Lock()self._gen_lock = threading.Lock()@propertydef ready(self) -> bool:return self.llm is not Nonedef status(self) -> Dict:return {"repo": LOCAL_LLM_REPO,"file": LOCAL_LLM_FILE,"loaded": self.ready,"loading": self._load_lock.locked(),"error": self.error,}def load(self):"""Download and load the model. Safe to call repeatedly."""with self._load_lock:if self.llm is not None or self.error:return self.llmtry:from llama_cpp import Llamatry:path = hf_hub_download(repo_id=LOCAL_LLM_REPO, filename=LOCAL_LLM_FILE, token=HF_TOKEN or None)except Exception:# A stale HF_TOKEN must not block the download of a public modelpath = hf_hub_download(repo_id=LOCAL_LLM_REPO, filename=LOCAL_LLM_FILE, token=False)self._probe(path)llm = Llama(model_path=path, n_ctx=LOCAL_LLM_CTX, n_threads=LOCAL_LLM_THREADS, verbose=False)template = str((llm.metadata or {}).get("tokenizer.chat_template", ""))self._chatml = "<|im_start|>" in template# Qwen3.5 answers directly when the assistant turn opens with an# empty think block; this is what its template emits for# enable_thinking=False.self._prefill = "<think>\n\n</think>\n\n" if "<think>" in template else ""self.llm = llmlogger.info(f"✅ Local LLM loaded: {LOCAL_LLM_REPO}/{LOCAL_LLM_FILE}")except Exception as e:self.error = f"{type(e).__name__}: {str(e)[:300]}"logger.error(f"❌ Local LLM unavailable: {self.error}")return self.llm@staticmethoddef _probe(path: str) -> None:"""Load the model once in a child process. A native crash there (forexample a CPU instruction the prebuilt wheel needs but this machinelacks) raises here instead of killing the API process."""code = ("import sys; from llama_cpp import Llama; ""m = Llama(model_path=sys.argv[1], n_ctx=256, n_threads=1, verbose=False); ""m.create_completion('Hi', max_tokens=1)")r = subprocess.run([sys.executable, "-c", code, path], capture_output=True, text=True, timeout=300)if r.returncode != 0:raise RuntimeError(f"llama.cpp probe exited with {r.returncode}: {(r.stderr or '')[-200:]}")def chat(self, system: str, user: str, max_tokens: int = 512) -> Dict:base = {"model": "local-llm", "source": "space_local_llamacpp", "model_id": LOCAL_LLM_REPO}if self.llm is None:if self._load_lock.locked():return {**base, "response": None, "error": "local LLM is still loading"}self.load()if self.llm is None:return {**base, "response": None, "error": self.error or "local LLM not loaded"}if not self._gen_lock.acquire(timeout=LOCAL_LLM_QUEUE_TIMEOUT_S):return {**base, "response": None, "error": "local LLM is busy with another request"}started = time.monotonic()stream = Nonetry:max_tokens = max(16, min(int(max_tokens), LOCAL_LLM_MAX_TOKENS))# The chat markers are structural: drop them from untrusted textsystem = system.replace("<|im_start|>", "").replace("<|im_end|>", "")user = user.replace("<|im_start|>", "").replace("<|im_end|>", "")if len(user) > LOCAL_LLM_MAX_INPUT_CHARS:# Keep both ends: context sits at the start, the question at the endhalf = LOCAL_LLM_MAX_INPUT_CHARS // 2user = f"{user[:half]}\n[...]\n{user[-half:]}"if self._chatml:prompt = (f"<|im_start|>system\n{system}<|im_end|>\n"f"<|im_start|>user\n{user}<|im_end|>\n"f"<|im_start|>assistant\n{self._prefill}")stream = self.llm.create_completion(prompt, max_tokens=max_tokens, temperature=0.7, top_p=0.8, top_k=20,presence_penalty=1.5, stop=["<|im_end|>", "<|endoftext|>"], stream=True,)read = lambda c: c["choices"][0].get("text") or ""else:stream = self.llm.create_chat_completion(messages=[{"role": "system", "content": system}, {"role": "user", "content": user}],max_tokens=max_tokens, temperature=0.7, top_p=0.8, stream=True,)read = lambda c: c["choices"][0].get("delta", {}).get("content") or ""pieces: List[str] = []truncated = Falsefor chunk in stream:pieces.append(read(chunk))if time.monotonic() - started > LOCAL_LLM_TIME_BUDGET_S:truncated = Truebreakimport retext = re.sub(r"<think>.*?</think>", "", "".join(pieces), flags=re.S).strip()if truncated:# Out of time: end on the last complete sentence or linecut = max(text.rfind(". "), text.rfind(".\n"), text.rfind("\n"))if cut > len(text) * 0.5:text = text[:cut + 1].strip()if not text:return {**base, "response": None, "error": "local LLM returned no text"}return {**base,"response": text,"tokens": len(pieces),"elapsed_ms": round((time.monotonic() - started) * 1000),"truncated": truncated,}except Exception as e:return {**base, "response": None, "error": f"{type(e).__name__}: {str(e)[:200]}"}finally:if stream is not None and hasattr(stream, "close"):stream.close()self._gen_lock.release()local_llm = LocalLLM()class TransformerModelLoader:"""Loads pretrained Transformer classifiers from the HF Hub on demand."""10 unmodified lines"desc": "BERT-based URL phishing/malware classifier",},}# Hosted reasoning model through HF Inference Providers (OpenAI-compatible# router at router.huggingface.co/v1). Billed per token, so it only answers# while the account has credit; otherwise security_chat uses LocalLLM.# Override via REASONING_LLM_MODEL.REASONING_LLM_REPO = os.environ.get("REASONING_LLM_MODEL", "deepseek-ai/DeepSeek-V3-0324")def __init__(self):self.pipelines = {} # name → transformers.Pipeline (lazy-loaded)self.load_errors = {} # name → last error messageself.last_engine = None # "hosted" | "local": which LLM answered lastself._provider_retry_at = 0.0 # monotonic time before which the hosted model is skippedself.transformers_available = Falsetry:import transformers # noqa: F40195 unmodified lines"inference_source": "entropy-heuristic",}SYSTEM_PROMPT = ("You are CyberForge AI, a precise, professional cybersecurity analyst. ""Give concise, technical, actionable answers. State a Risk Level ""(Critical/High/Medium/Low) when assessing a threat, and cite CVE / MITRE ""ATT&CK IDs where relevant.")def provider_available(self) -> bool:"""True when the hosted model is worth trying: a token is set and thelast call did not fail on billing or auth."""return bool(HF_TOKEN) and time.monotonic() >= self._provider_retry_atdef _provider_chat(self, query: str, max_tokens: int) -> Optional[Dict]:"""Ask the hosted model. Returns None when it cannot answer."""import requeststry:r = requests.post("https://router.huggingface.co/v1/chat/completions",headers={"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"},json={"model": self.REASONING_LLM_REPO,"messages": [{"role": "system", "content": self.SYSTEM_PROMPT},{"role": "user", "content": query[:6000]},],"max_tokens": max_tokens,"temperature": 0.3,},timeout=60,)if r.status_code == 200:data = r.json()text = data["choices"][0]["message"]["content"]return {"model": "deepseek","source": "hf_inference_providers","response": text,"model_id": self.REASONING_LLM_REPO,}# 401/402/403 (bad token, no credit) will not fix themselves quicklyself._provider_retry_at = time.monotonic() + (900 if r.status_code in (401, 402, 403) else 60)logger.warning(f"Hosted LLM HTTP {r.status_code}: {r.text[:160]} — using the local model")except Exception as e:self._provider_retry_at = time.monotonic() + 60logger.warning(f"Hosted LLM error: {type(e).__name__}: {str(e)[:160]} — using the local model")return Nonedef security_chat(self, query: str, max_tokens: int = 512, allow_local: bool = True) -> Dict:"""Cybersecurity Q&A.1. Hosted model through HF Inference Providers, while the account has credit.2. The open-source model running inside this Space (free, no key)."""if self.provider_available():hosted = self._provider_chat(query, max_tokens)if hosted:self.last_engine = "hosted"return hostedif not allow_local:return {"model": "security-llm", "response": None, "error": "hosted LLM unavailable"}result = local_llm.chat(self.SYSTEM_PROMPT, query, max_tokens=max_tokens)if result.get("response"):self.last_engine = "local"return resultdef status(self) -> Dict:return {"transformers_available": self.transformers_available,"loaded": list(self.pipelines.keys()),"available": list(self.REGISTRY.keys()) + ["reasoning_llm"],"load_errors": self.load_errors,"reasoning_llm": {"last_engine": self.last_engine,"local": local_llm.status(),"hosted": {"model": self.REASONING_LLM_REPO,"usable": self.provider_available(),},},}@staticmethod280 unmodified lines"status": "healthy","timestamp": datetime.utcnow().isoformat(),"services": {"reasoning_llm": transformer_loader.last_engine, # "hosted" | "local" | None until first answer"reasoning_model": transformer_loader.REASONING_LLM_REPO if HF_TOKEN else None,"local_llm": local_llm.status(),"ml_models": ml_loader.ready,"models_loaded": list(ml_loader.models.keys()),"transformers": transformer_loader.transformers_available,19 unmodified linesquery = body.get("query", "")context = body.get("context", {})history = body.get("conversation_history", [])result = await run_in_threadpool(reasoning_service.analyze, query, context, history)return result71 unmodified lines"""Threat scanning – called by backend mlService.scanForThreats()"""body = await request.json()query = json.dumps(body.get("data", body), indent=1)[:3000]result = await run_in_threadpool(reasoning_service.analyze, f"Scan these indicators for threats:\n{query}")return result3 unmodified linesbody = await request.json()query = body.get("query", "")context = body.get("context", {})result = await run_in_threadpool(reasoning_service.analyze, query, context)return {"insights": result.get("response", ""),"confidence": result.get("confidence", 0),52 unmodified lines@api.post("/api/v2/security-chat")async def api_v2_security_chat(request: Request):"""Cybersecurity Q&A: hosted model when the account has credit, otherwisethe open-source model running in this Space.Body: { "query": "...", "max_tokens": 512 }"""body = await request.json()query = body.get("query", "").strip()if not query:return JSONResponse(status_code=400, content={"detail": "query required"})max_tokens = int(body.get("max_tokens", 512))result = await run_in_threadpool(transformer_loader.security_chat, query, max_tokens)# Last resort: ML heuristic if no LLM answeredif result.get("error"):return reasoning_service._fallback(query)return result104 unmodified lines"domain": "example.com", # optional"ip": "1.2.3.4", # optional"hash": "abc123...", # optional (future use)"context": { ... } # optional — passed to the LLM narrative}Response: per-indicator results with unified risk summary.51 unmodified lineselse:risk_level = "unknown"# Optional LLM narrative. Hosted model only: the in-Space model takes many# seconds on CPU and this endpoint is called with short timeouts.ai_analysis = Noneif transformer_loader.provider_available() and (url or domain):target = url or domainai_analysis = await run_in_threadpool(reasoning_service.analyze,f"Scan these IOC indicators for threats: url={target}, domain={effective_domain}",context, None, False,)return {111 unmodified linespersistent vector store, which requires paid GPU on free tier).2. Translates raw ML signals / IOCs into plain human language when theuser includes raw data and asks for an explanation.3. Uses the hosted model when the account has credit, otherwise theopen-source model running in this Space,or the ML-powered heuristic analyzer when no LLM is available.NOTE ON REAL-TIME "LEARNING": HuggingFace Spaces cpu-basic does NOTsupport fine-tuning or persistent model updates at runtime. What this160 unmodified linesn_models = len(ml_loader.models)sources.append({"type": "ml_models", "label": "ML Models", "value": f"{n_models}/4 loaded"})# LLM cascade: hosted model (when the account has credit), then the# open-source model in this Space, then the ML heuristic below.llm_result = await run_in_threadpool(transformer_loader.security_chat, enriched_message, 600)if llm_result.get("response") and not llm_result.get("error"):text = llm_result["response"]text_lower = text.lower()16 unmodified lines"confidence": 0.72,"risk_level": risk_level,"risk_score": risk_score,"model_used": llm_result.get("model_id", LOCAL_LLM_REPO),"session_id": session_id,"sources": sources,"timestamp": datetime.utcnow().isoformat(),}# Path 3: ML heuristic fallback (always available)fallback = reasoning_service._fallback(enriched_message)# Override with ML scan risk if availableif detected_risk_score > 0:ml_score_100 = round(detected_risk_score * 100, 1)10 unmodified lines"""Network traffic analysis – called by backend mlService.analyzeNetworkTraffic()"""body = await request.json()traffic_data = body.get("traffic_data", {})result = await run_in_threadpool(reasoning_service.analyze,f"Analyze this network traffic for security threats:\n{json.dumps(traffic_data, indent=1)[:3000]}",)return result4 unmodified linesbody = await request.json()task_type = body.get("task_type", "")task_data = body.get("task_data", {})result = await run_in_threadpool(reasoning_service.analyze,f"Execute cybersecurity task '{task_type}':\n{json.dumps(task_data, indent=1)[:3000]}",)return result3 unmodified lines"""Browser session analysis"""body = await request.json()session = body.get("session_data", body)result = await run_in_threadpool(reasoning_service.analyze,f"Analyze this browser session for security threats:\n{json.dumps(session, indent=1)[:3000]}",)return result2 unmodified linesasync def api_analyze_threat_feed(request: Request):"""Threat feed analysis"""body = await request.json()result = await run_in_threadpool(reasoning_service.analyze,f"Analyze this threat feed:\n{json.dumps(body, indent=1)[:3000]}",)return result94 unmodified lines| Method | Endpoint | Description ||--------|----------|-------------|| GET | `/health` | Health check — version, models, services || POST | `/analyze` | AI chat / general analysis (LLM or ML fallback) || POST | `/analyze-url` | URL threat analysis — 4 ML models + risk factors || POST | `/scan-threats` | Deep threat scan (LLM or ML fallback) || POST | `/api/insights/generate` | AI-generated security insights || POST | `/api/models/predict` | Direct ML model prediction || GET | `/models` | List loaded models with source info |9 unmodified lines| POST | `/api/v2/ioc-scan` | Multi-IOC scanner: URL + domain + DGA + BERT || POST | `/api/v2/url-enrich` | Enriched analysis: ML + BERT + DGA + explainability || POST | `/api/v2/chat` | Security chatbot — context-grounded, RAG-lite, machine→human translation || POST | `/api/v2/security-chat` | Security LLM Q&A (hosted model, or the open-source model in this Space) || GET | `/api/v2/status` | Transformer model load status |### Quick Examples35 unmodified lines# Initialize services at startuplogger.info("🚀 Initializing CyberForge AI services...")ml_loader.initialize()# Load the chat model in the background so the API answers health checks at oncethreading.Thread(target=local_llm.load, name="local-llm-loader", daemon=True).start()logger.info("✅ Services initialized")# Create Gradio app and mount it on FastAPI
18 unmodified lines192021222324252627282930313218 unmodified linesfastapi>=0.100.0uvicorn[standard]>=0.22.0# HF Hubhuggingface_hub>=0.25.0# Feature Engineeringtldextract>=5.1.0# Gemini AI (new SDK)google-genai>=1.0.0# Notebook executionnbformat>=5.10.018 unmodified lines192021222324252627282930313233343536373818 unmodified linesfastapi>=0.100.0uvicorn[standard]>=0.22.0# HF Hub (>=0.26 for Inference Providers / OpenAI-compatible router used by DeepSeek)huggingface_hub>=0.26.0# Feature Engineeringtldextract>=5.1.0# Reasoning LLM# hosted: HF Inference Providers (OpenAI-compatible router), plain HTTPS via requests.# local: open-source GGUF chat model run in-process with llama.cpp. The prebuilt# CPU wheel is used so the image needs no compiler; --only-binary makes the# build fail fast instead of compiling from source if that wheel is missing.--extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu--only-binary llama-cpp-pythonllama-cpp-python==0.3.36# Notebook executionnbformat>=5.10.0
Dockerfile+1
app.py+313-218
requirements.txt+10-4