Create gemini_client.py
Browse files- gemini_client.py +71 -0
gemini_client.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# gemini_client.py
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import logging
|
| 5 |
+
from typing import Dict, Any
|
| 6 |
+
try:
|
| 7 |
+
from google import genai
|
| 8 |
+
except Exception:
|
| 9 |
+
genai = None
|
| 10 |
+
|
| 11 |
+
logger = logging.getLogger("gemini_client")
|
| 12 |
+
GENAI_MODEL = os.getenv("GENAI_MODEL", "gemini-2.5-flash")
|
| 13 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 14 |
+
|
| 15 |
+
genai_client = None
|
| 16 |
+
if genai:
|
| 17 |
+
try:
|
| 18 |
+
genai_client = genai.Client(api_key=GEMINI_API_KEY) if GEMINI_API_KEY else genai.Client()
|
| 19 |
+
logger.info("Gemini client initialized")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
logger.warning("Could not initialize genai client: %s", e)
|
| 22 |
+
genai_client = None
|
| 23 |
+
else:
|
| 24 |
+
logger.info("genai not available; Gemini disabled")
|
| 25 |
+
|
| 26 |
+
def _extract_json_from_text(text: str):
|
| 27 |
+
# simple JSON extractor re-used from original script
|
| 28 |
+
if not text:
|
| 29 |
+
return None
|
| 30 |
+
start = None
|
| 31 |
+
depth = 0
|
| 32 |
+
for i, ch in enumerate(text):
|
| 33 |
+
if ch == "{":
|
| 34 |
+
if start is None:
|
| 35 |
+
start = i
|
| 36 |
+
depth += 1
|
| 37 |
+
elif ch == "}":
|
| 38 |
+
if depth > 0:
|
| 39 |
+
depth -= 1
|
| 40 |
+
if depth == 0 and start is not None:
|
| 41 |
+
return text[start:i+1]
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
def call_gemini_synthesizer(claim: str, article_text: str, tools_outputs: Dict[str, Any]) -> Dict[str, Any]:
|
| 45 |
+
if not genai_client:
|
| 46 |
+
return {"error": "Gemini not configured"}
|
| 47 |
+
tools_snapshot = json.dumps(tools_outputs, indent=0)[:15000]
|
| 48 |
+
prompt = f"""
|
| 49 |
+
You are an expert fact-checker. Based on these tool outputs (JSON) and the claim, return JSON:
|
| 50 |
+
{{"verdict":"True|False|Misleading|Unclear","score":1..10,"explanation":"...","issues":[],"recommendations":[]}}
|
| 51 |
+
Tool outputs:
|
| 52 |
+
{tools_snapshot}
|
| 53 |
+
|
| 54 |
+
Claim:
|
| 55 |
+
{claim}
|
| 56 |
+
|
| 57 |
+
Article:
|
| 58 |
+
{article_text}
|
| 59 |
+
Return JSON only.
|
| 60 |
+
"""
|
| 61 |
+
try:
|
| 62 |
+
resp = genai_client.models.generate_content(model=GENAI_MODEL, contents=prompt)
|
| 63 |
+
raw = getattr(resp, "text", None) or str(resp)
|
| 64 |
+
maybe = _extract_json_from_text(raw)
|
| 65 |
+
if maybe:
|
| 66 |
+
parsed = json.loads(maybe)
|
| 67 |
+
return {"raw": raw, "parsed": parsed}
|
| 68 |
+
return {"raw": raw, "error": "No JSON in response"}
|
| 69 |
+
except Exception as e:
|
| 70 |
+
logger.exception("call_gemini_synthesizer failed")
|
| 71 |
+
return {"error": str(e)}
|