File size: 1,464 Bytes
284e9bf
 
 
 
 
 
 
 
7ad7c0a
 
 
 
284e9bf
 
 
7ad7c0a
875e5d4
 
 
 
284e9bf
 
 
 
 
 
 
 
 
 
7ad7c0a
 
284e9bf
 
 
7a1ecf6
875e5d4
284e9bf
5ea229f
 
 
 
7a1ecf6
284e9bf
5ea229f
284e9bf
5ea229f
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
import pytesseract
from pdf2image import convert_from_path
import google.generativeai as genai
import os, json

# --- Form Schema ---
FORMS = {
    "pancard_form": [
        "Name", "DOB", "Gender", "FatherName", "MotherName",
        "Address", "City", "State", "Pincode", "Mobile", "Email",
        "DocumentType", "DocumentNumber", "IssueAuthority",
        "IssueDate", "ExpiryDate"
    ]
}

# --- Configure Gemini ---
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
    raise ValueError("❌ GEMINI_API_KEY not found. Please set it in Hugging Face Space Secrets.")
genai.configure(api_key=api_key)

def extract_text_from_pdf(pdf_path):
    pages = convert_from_path(pdf_path)
    text = ""
    for page in pages:
        text += pytesseract.image_to_string(page) + "\n"
    return text.strip()
def extract_key_values_with_gemini(raw_text, form_type="pancard_form"):
    prompt = f"""
You are an intelligent document parser.
Given the following document text, extract only these fields: {FORMS[form_type]}.
Return strictly as JSON key-value pairs.
Document text:
{raw_text}
"""
    model = genai.GenerativeModel("models/gemini-2.5-flash")
    print("Gemini API called successfully ✅")
    response = model.generate_content(prompt)
    text = response.text.strip()

    # --- Cleanup ---
    text = text.replace("```json", "").replace("```", "").strip()

    try:
        return json.loads(text)
    except Exception:
        return {"raw_output": text}