File size: 3,325 Bytes
b489ffd | 1 2 3 4 5 6 7 8 9 10 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 87 88 89 90 91 92 93 94 95 96 97 | def generate_ai_explanation(model_prediction, model_confidence, fact_check):
"""
Creates human-readable explanation using classifier + Google Fact Check data
"""
confidence = model_confidence
# If Google Fact Check verified the claim
if fact_check.get("verified"):
publisher = fact_check.get("publisher", "a fact-checking source")
rating = fact_check.get("rating", "unverified")
url = fact_check.get("url", "")
rating_lower = rating.lower()
if rating_lower == "false":
final_verdict = "FAKE"
verdict_color = "red"
elif "mostly false" in rating_lower:
final_verdict = "MISLEADING"
verdict_color = "orange"
elif "misleading" in rating_lower:
final_verdict = "MISLEADING"
verdict_color = "orange"
elif rating_lower == "true":
final_verdict = "REAL"
verdict_color = "green"
elif "mostly true" in rating_lower:
final_verdict = "REAL"
verdict_color = "green"
else:
final_verdict = "NEEDS CONTEXT"
verdict_color = "yellow"
if final_verdict == "FAKE":
explanation = (
f"{publisher} reviewed this claim and rated it '{rating}'. "
"The claim contains misinformation and contradicts verified fact-checking sources."
)
elif final_verdict == "MISLEADING":
explanation = (
f"{publisher} reviewed this claim and rated it '{rating}'. "
"The claim may contain partial truths but is presented in a misleading or incomplete way."
)
elif final_verdict == "REAL":
explanation = (
f"{publisher} reviewed this claim and rated it '{rating}'. "
"The claim aligns with verified reporting and appears credible."
)
else:
explanation = (
f"{publisher} reviewed this claim and rated it '{rating}'. "
"Additional context is needed to fully assess this claim."
)
return {
"final_verdict": final_verdict,
"verdict_color": verdict_color,
"explanation": explanation,
"source_url": url,
"source_publisher": publisher
}
# Fallback if no fact-check data
if model_prediction == "FAKE":
final_verdict = "FAKE"
verdict_color = "red"
explanation = (
f"The classifier flagged this content as potentially fake with {confidence}% confidence. "
"No direct fact-check source was found, so manual verification is recommended."
)
else:
final_verdict = "REAL"
verdict_color = "green"
explanation = (
f"The classifier found this content likely credible with {confidence}% confidence. "
"However, no direct fact-check source was found."
)
return {
"final_verdict": final_verdict,
"verdict_color": verdict_color,
"explanation": explanation,
"source_url": None,
"source_publisher": None
} |