File size: 2,307 Bytes
2bd1398
 
 
67ec86c
 
 
2bd1398
 
 
67ec86c
 
2bd1398
 
 
 
 
 
 
 
 
 
 
 
 
 
67ec86c
2bd1398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd00313
67ec86c
 
 
2bd1398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
from dotenv import load_dotenv
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from groq_client import call_groq


def extract_json(raw: str) -> dict:
    if not raw:
        return None
    try:
        return json.loads(raw)
    except json.JSONDecodeError:
        pass
    try:
        start = raw.find("{")
        end = raw.rfind("}") + 1
        if start != -1 and end != 0:
            return json.loads(raw[start:end])
    except json.JSONDecodeError:
        pass
    return None


def explain_verdict(claim: str, verdict: str, confidence: float, explanation: str, judge_result: dict) -> dict:
    overall_quality = judge_result.get("overall_quality", "UNKNOWN")
    quality_explanation = judge_result.get("quality_explanation", "")

    prompt = f"""You are a medical communicator who explains complex medical verdicts in plain English for everyday people.

CLAIM: "{claim}"
VERDICT: {verdict}
CONFIDENCE: {confidence * 100:.0f}%
TECHNICAL EXPLANATION: {explanation}
EVIDENCE QUALITY: {overall_quality}
QUALITY NOTE: {quality_explanation}

Write a plain English summary that:
1. States the verdict clearly in simple words
2. Explains WHY in 1-2 sentences a non-doctor would understand
3. Mentions how strong the evidence is
4. Adds a practical takeaway if relevant

Respond in this exact JSON format:
{{
    "plain_english": "2-3 sentence plain English summary",
    "takeaway": "one practical takeaway for the user",
    "evidence_strength": "Strong" or "Moderate" or "Weak" or "Insufficient"
}}

Respond with JSON only. No extra text, no markdown formatting, no backticks."""

    raw = call_groq(prompt, response_format={"type": "json_object"})
    result = extract_json(raw)
    if result is not None:
        return result

    return {
        "plain_english": explanation,
        "takeaway": "",
        "evidence_strength": "Unknown"
    }


if __name__ == "__main__":
    result = explain_verdict(
        claim="vaccines cause autism",
        verdict="FALSE",
        confidence=0.9,
        explanation="No peer-reviewed evidence supports a link between vaccines and autism.",
        judge_result={"overall_quality": "HIGH", "quality_explanation": "Multiple RCTs found."}
    )
    print(json.dumps(result, indent=2))