Spaces:
Running
Running
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() | |