| import json |
| import logging |
| import os |
|
|
| from google import genai |
| from google.genai import types |
|
|
| log = logging.getLogger("cardextractor") |
|
|
| |
| client = genai.Client(api_key=os.environ["GEMINI_API_KEY"]) |
| MODEL = os.environ.get("GEMINI_MODEL", "gemma-4-26b-a4b-it") |
|
|
| PROMPT = """Extract the contact information from this business card. |
| Return ONLY JSON, no markdown, with exactly these keys: |
| {"name":"","company":"","designation":"","phone":"","email":"","website":"","address":""}""" |
|
|
|
|
| def extract_contact(image_path): |
| with open(image_path, "rb") as f: |
| image_bytes = f.read() |
|
|
| |
| ext = os.path.splitext(image_path)[1].lower().lstrip(".") or "jpeg" |
| mime = "image/png" if ext == "png" else f"image/{ext}" |
|
|
| |
| |
| resp = client.models.generate_content( |
| model=MODEL, |
| contents=[ |
| types.Part.from_bytes(data=image_bytes, mime_type=mime), |
| PROMPT, |
| ], |
| ) |
|
|
| text = resp.text |
| log.info("LLM response: %s", text) |
| start = text.find("{") |
| if start == -1: |
| raise ValueError(f"no JSON in model output: {text!r}") |
| data, _ = json.JSONDecoder().raw_decode(text[start:]) |
| return data |
|
|