Spaces:
Running
Running
import gradio as gr | |
import base64 | |
import requests | |
import json | |
# Replace with your actual API key and model name | |
API_KEY = "your_openrouter_api_key" | |
IMAGE_MODEL = "OpenGVLab/InternVL3-14B" | |
def process_passport(image): | |
try: | |
# Encode image to base64 | |
encoded_image = base64.b64encode(image.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() | |
print("📡 OpenRouter Response:", result) | |
# Return the whole raw JSON result | |
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() | |