Spaces:
Running
Running
Fix: ML-powered fallback for analyze endpoint when Gemini unavailable
Browse files
app.py
CHANGED
|
@@ -210,18 +210,123 @@ Provide a comprehensive cybersecurity analysis:"""
|
|
| 210 |
return self._fallback(query)
|
| 211 |
|
| 212 |
def _fallback(self, query: str) -> Dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
return {
|
| 214 |
-
"response": (
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
"
|
| 220 |
-
"
|
| 221 |
-
"risk_score": 0,
|
| 222 |
-
"insights": [],
|
| 223 |
-
"recommendations": ["Verify GEMINI_API_KEY is set", "Check Space logs"],
|
| 224 |
-
"model_used": "fallback",
|
| 225 |
"timestamp": datetime.utcnow().isoformat(),
|
| 226 |
}
|
| 227 |
|
|
|
|
| 210 |
return self._fallback(query)
|
| 211 |
|
| 212 |
def _fallback(self, query: str) -> Dict:
|
| 213 |
+
"""ML-powered analysis when Gemini is unavailable"""
|
| 214 |
+
import re
|
| 215 |
+
global ml_loader
|
| 216 |
+
|
| 217 |
+
url_pattern = re.compile(r'https?://[^\s"\'<>]+')
|
| 218 |
+
urls = url_pattern.findall(query)
|
| 219 |
+
|
| 220 |
+
risk_level = "Unknown"
|
| 221 |
+
risk_score = 0.0
|
| 222 |
+
insights = []
|
| 223 |
+
response_lines = ["## CyberForge ML Security Analysis\n"]
|
| 224 |
+
|
| 225 |
+
try:
|
| 226 |
+
loader = ml_loader
|
| 227 |
+
n_loaded = len(loader.models)
|
| 228 |
+
except Exception:
|
| 229 |
+
loader = None
|
| 230 |
+
n_loaded = 0
|
| 231 |
+
|
| 232 |
+
if urls and loader and n_loaded > 0:
|
| 233 |
+
threat_models_fired = []
|
| 234 |
+
all_scores = []
|
| 235 |
+
|
| 236 |
+
for url in urls[:3]:
|
| 237 |
+
features = extract_url_features(url)
|
| 238 |
+
url_threats = []
|
| 239 |
+
url_scores = []
|
| 240 |
+
|
| 241 |
+
for model_name in ["phishing_detection", "malware_detection", "web_attack_detection"]:
|
| 242 |
+
pred = loader.predict(model_name, features)
|
| 243 |
+
if pred.get("prediction", 0) == 1:
|
| 244 |
+
label = model_name.replace("_detection", "").replace("_", " ").title()
|
| 245 |
+
url_threats.append(label)
|
| 246 |
+
url_scores.append(pred.get("confidence", 0.5))
|
| 247 |
+
threat_models_fired.append(model_name)
|
| 248 |
+
|
| 249 |
+
avg = sum(url_scores) / len(url_scores) if url_scores else 0.15
|
| 250 |
+
all_scores.append(avg)
|
| 251 |
+
lvl = "HIGH" if avg > 0.6 else "MEDIUM" if avg > 0.35 else "LOW"
|
| 252 |
+
threats_str = ", ".join(url_threats) if url_threats else "None detected"
|
| 253 |
+
display_url = url if len(url) <= 70 else url[:67] + "..."
|
| 254 |
+
response_lines.append(f"**URL:** `{display_url}`")
|
| 255 |
+
response_lines.append(f"- Risk: **{lvl}** | Threats: {threats_str} | Score: {avg:.0%}\n")
|
| 256 |
+
|
| 257 |
+
overall = sum(all_scores) / len(all_scores) if all_scores else 0.2
|
| 258 |
+
if overall > 0.65:
|
| 259 |
+
risk_level, risk_score = "High", 7.5
|
| 260 |
+
elif overall > 0.4:
|
| 261 |
+
risk_level, risk_score = "Medium", 5.0
|
| 262 |
+
else:
|
| 263 |
+
risk_level, risk_score = "Low", 2.5
|
| 264 |
+
|
| 265 |
+
insights = [f"{n_loaded} ML models active"] + [
|
| 266 |
+
f"Threat model triggered: {m.replace('_detection', '').replace('_', ' ').title()}"
|
| 267 |
+
for m in set(threat_models_fired)
|
| 268 |
+
]
|
| 269 |
+
|
| 270 |
+
if risk_level == "High":
|
| 271 |
+
response_lines.append("### Recommendation\n⚠️ **Block immediately.** Phishing or malware indicators detected — do not visit this URL.")
|
| 272 |
+
elif risk_level == "Medium":
|
| 273 |
+
response_lines.append("### Recommendation\n⚡ **Exercise caution.** Validate with additional threat intelligence before accessing.")
|
| 274 |
+
else:
|
| 275 |
+
response_lines.append("### Recommendation\n✅ **URL appears structurally safe** based on ML analysis.")
|
| 276 |
+
|
| 277 |
+
else:
|
| 278 |
+
query_lower = query.lower()
|
| 279 |
+
malware_kws = ["malware", "virus", "ransomware", "trojan", "spyware", "backdoor", "worm"]
|
| 280 |
+
phishing_kws = ["phishing", "credential", "fake login", "spoof", "scam", "social engineering"]
|
| 281 |
+
safe_kws = ["safe", "legitimate", "trusted", "secure", "verify"]
|
| 282 |
+
|
| 283 |
+
if any(k in query_lower for k in malware_kws):
|
| 284 |
+
risk_level, risk_score = "High", 7.0
|
| 285 |
+
response_lines.append(
|
| 286 |
+
"**Malware indicators detected in your query.**\n\n"
|
| 287 |
+
"Recommended actions:\n"
|
| 288 |
+
"- Isolate the affected system immediately\n"
|
| 289 |
+
"- Run a full endpoint detection scan\n"
|
| 290 |
+
"- Review recently installed software and browser extensions\n"
|
| 291 |
+
"- Check startup processes and scheduled tasks for persistence\n"
|
| 292 |
+
"- Rotate credentials if any exposure is suspected"
|
| 293 |
+
)
|
| 294 |
+
elif any(k in query_lower for k in phishing_kws):
|
| 295 |
+
risk_level, risk_score = "High", 7.0
|
| 296 |
+
response_lines.append(
|
| 297 |
+
"**Phishing threat indicators detected.**\n\n"
|
| 298 |
+
"Recommended actions:\n"
|
| 299 |
+
"- Do not submit credentials to the suspected site\n"
|
| 300 |
+
"- Verify sender identity through a secondary channel\n"
|
| 301 |
+
"- Report to your IT security team immediately\n"
|
| 302 |
+
"- Enable MFA on all accounts that may be affected\n"
|
| 303 |
+
"- Review email headers for spoofing indicators"
|
| 304 |
+
)
|
| 305 |
+
elif any(k in query_lower for k in safe_kws):
|
| 306 |
+
risk_level, risk_score = "Low", 2.0
|
| 307 |
+
response_lines.append("No immediate threat indicators detected. Continue standard monitoring procedures.")
|
| 308 |
+
else:
|
| 309 |
+
response_lines.append(
|
| 310 |
+
f"CyberForge ML is operational with **{n_loaded}/4 models** loaded.\n\n"
|
| 311 |
+
"For best results, include a URL or specific threat indicators in your query.\n\n"
|
| 312 |
+
"**Available analysis capabilities:**\n"
|
| 313 |
+
"- URL threat analysis (phishing, malware, web attacks)\n"
|
| 314 |
+
"- Network anomaly detection\n"
|
| 315 |
+
"- Real-time threat event monitoring\n\n"
|
| 316 |
+
"*AI chat (Gemini) is currently unavailable. Provide a URL for full ML-based analysis.*"
|
| 317 |
+
)
|
| 318 |
+
insights = [f"{n_loaded} ML models active"]
|
| 319 |
+
|
| 320 |
+
response_lines.append("\n---\n*Powered by CyberForge ML models — Gemini AI offline.*")
|
| 321 |
+
|
| 322 |
return {
|
| 323 |
+
"response": "\n".join(response_lines),
|
| 324 |
+
"confidence": 0.65 if urls else 0.4,
|
| 325 |
+
"risk_level": risk_level,
|
| 326 |
+
"risk_score": risk_score,
|
| 327 |
+
"insights": insights,
|
| 328 |
+
"recommendations": [],
|
| 329 |
+
"model_used": "cyberforge-ml-fallback",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
"timestamp": datetime.utcnow().isoformat(),
|
| 331 |
}
|
| 332 |
|