File size: 1,714 Bytes
8461ba5
 
 
 
 
8435795
 
8461ba5
 
 
8435795
 
8461ba5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import base64
import requests
import json

API_KEY = "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"
IMAGE_MODEL = "opengvlab/internvl3-14b:free"

def process_passport(image):
    try:
        with open(image, "rb") as f:
            encoded_image = base64.b64encode(f.read()).decode("utf-8")
        data_url = f"data:image/jpeg;base64,{encoded_image}"

        prompt = "Extract all visible information from the front page of the passport. Output in JSON format."

        payload = {
            "model": IMAGE_MODEL,
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {"type": "text", "text": prompt},
                        {"type": "image_url", "image_url": {"url": data_url}}
                    ]
                }
            ]
        }

        headers = {
            "Authorization": f"Bearer {API_KEY}",
            "Content-Type": "application/json"
        }

        response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
        result = response.json()

        return json.dumps(result, indent=2)

    except Exception as e:
        return f"⚠️ Error: {str(e)}"

# Gradio Interface
iface = gr.Interface(
    fn=process_passport,
    inputs=gr.Image(type="file", label="Upload Passport Front"),
    outputs=gr.Code(label="OpenRouter Raw JSON Result", language="json"),
    title="Passport Front Image Extractor",
    description="Upload a front image of a passport. The app will use OpenRouter to extract the visible details and return the result as JSON."
)

if __name__ == "__main__":
    iface.launch()