Create analyzer.py
Browse files- analyzer.py +65 -0
analyzer.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import traceback
|
| 2 |
+
from loguru import logger
|
| 3 |
+
from .analysis_helpers import analyze_url, analyze_text
|
| 4 |
+
from .orchestrator import ORCH
|
| 5 |
+
|
| 6 |
+
def on_analyze(news_text: str = "", news_url: str = "", image_url: str = "", run_serp: bool = False):
|
| 7 |
+
"""
|
| 8 |
+
Analyze either a text or a URL (mutually exclusive).
|
| 9 |
+
"""
|
| 10 |
+
try:
|
| 11 |
+
if news_url and news_text:
|
| 12 |
+
return {"error": "Please provide either a URL or text — not both."}, "", [], {}
|
| 13 |
+
|
| 14 |
+
if news_url:
|
| 15 |
+
article_text, headline, qa_fallback_note = analyze_url(news_url, run_serp)
|
| 16 |
+
url = news_url
|
| 17 |
+
elif news_text:
|
| 18 |
+
article_text, headline, qa_fallback_note = analyze_text(news_text)
|
| 19 |
+
url = None
|
| 20 |
+
else:
|
| 21 |
+
return {"error": "No input provided."}, "", [], {}
|
| 22 |
+
|
| 23 |
+
claim = ""
|
| 24 |
+
report = ORCH.run(
|
| 25 |
+
claim_text=claim,
|
| 26 |
+
article_text=article_text,
|
| 27 |
+
url=url,
|
| 28 |
+
image_url=image_url or None,
|
| 29 |
+
run_serpapi=run_serp,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
extracted_claims = [r.get("claim") for r in report.get("reports", [])]
|
| 33 |
+
qa_text = ""
|
| 34 |
+
if report.get("reports"):
|
| 35 |
+
qa_text = report["reports"][0].get("qa_summary", "") or qa_fallback_note
|
| 36 |
+
|
| 37 |
+
phishing_tag = extract_phishing_tag(report)
|
| 38 |
+
if report.get("reports"):
|
| 39 |
+
report["reports"][0]["phishing_tag"] = phishing_tag
|
| 40 |
+
|
| 41 |
+
phish = report.get("reports", [{}])[0].get("phishing_analysis", {}) or {}
|
| 42 |
+
phish["phishing_tag"] = phishing_tag
|
| 43 |
+
|
| 44 |
+
return report, qa_text, extracted_claims, phish
|
| 45 |
+
|
| 46 |
+
except Exception:
|
| 47 |
+
logger.exception("on_analyze failed")
|
| 48 |
+
return {"error": traceback.format_exc()}, "", [], {}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def extract_phishing_tag(report):
|
| 52 |
+
summary_phish_flag = report.get("summary", {}).get("phishing_flag")
|
| 53 |
+
if summary_phish_flag is True:
|
| 54 |
+
return "Unsafe"
|
| 55 |
+
elif summary_phish_flag is False:
|
| 56 |
+
return "Safe"
|
| 57 |
+
|
| 58 |
+
first_phish = report.get("reports", [{}])[0].get("phishing_analysis", {}) or {}
|
| 59 |
+
sb = (first_phish.get("safe_browsing") or {})
|
| 60 |
+
vt = (first_phish.get("virustotal") or {})
|
| 61 |
+
if sb.get("safe") is False or vt.get("safe") is False:
|
| 62 |
+
return "Unsafe"
|
| 63 |
+
elif sb.get("safe") is True and vt.get("safe") is True:
|
| 64 |
+
return "Safe"
|
| 65 |
+
return "Unknown"
|