| import traceback | |
| from loguru import logger | |
| from .analysis_helpers import analyze_url, analyze_text | |
| from .orchestrator import ORCH | |
| def on_analyze(news_text: str = "", news_url: str = "", image_url: str = "", run_serp: bool = False): | |
| """ | |
| Analyze either a text or a URL (mutually exclusive). | |
| """ | |
| try: | |
| if news_url and news_text: | |
| return {"error": "Please provide either a URL or text β not both."}, "", [], {} | |
| if news_url: | |
| article_text, headline, qa_fallback_note = analyze_url(news_url, run_serp) | |
| url = news_url | |
| elif news_text: | |
| article_text, headline, qa_fallback_note = analyze_text(news_text) | |
| url = None | |
| else: | |
| return {"error": "No input provided."}, "", [], {} | |
| claim = "" | |
| report = ORCH.run( | |
| claim_text=claim, | |
| article_text=article_text, | |
| url=url, | |
| image_url=image_url or None, | |
| run_serpapi=run_serp, | |
| ) | |
| extracted_claims = [r.get("claim") for r in report.get("reports", [])] | |
| qa_text = "" | |
| if report.get("reports"): | |
| qa_text = report["reports"][0].get("qa_summary", "") or qa_fallback_note | |
| phishing_tag = extract_phishing_tag(report) | |
| if report.get("reports"): | |
| report["reports"][0]["phishing_tag"] = phishing_tag | |
| phish = report.get("reports", [{}])[0].get("phishing_analysis", {}) or {} | |
| phish["phishing_tag"] = phishing_tag | |
| return report, qa_text, extracted_claims, phish | |
| except Exception: | |
| logger.exception("on_analyze failed") | |
| return {"error": traceback.format_exc()}, "", [], {} | |
| def extract_phishing_tag(report): | |
| summary_phish_flag = report.get("summary", {}).get("phishing_flag") | |
| if summary_phish_flag is True: | |
| return "Unsafe" | |
| elif summary_phish_flag is False: | |
| return "Safe" | |
| first_phish = report.get("reports", [{}])[0].get("phishing_analysis", {}) or {} | |
| sb = (first_phish.get("safe_browsing") or {}) | |
| vt = (first_phish.get("virustotal") or {}) | |
| if sb.get("safe") is False or vt.get("safe") is False: | |
| return "Unsafe" | |
| elif sb.get("safe") is True and vt.get("safe") is True: | |
| return "Safe" | |
| return "Unknown" | |