Spaces:
Running
Running
Sandy2636
commited on
Commit
·
8461ba5
1
Parent(s):
c387e96
Add application file
Browse files- app.py +58 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import base64
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
|
6 |
+
# Replace with your actual API key and model name
|
7 |
+
API_KEY = "your_openrouter_api_key"
|
8 |
+
IMAGE_MODEL = "OpenGVLab/InternVL3-14B"
|
9 |
+
|
10 |
+
def process_passport(image):
|
11 |
+
try:
|
12 |
+
# Encode image to base64
|
13 |
+
encoded_image = base64.b64encode(image.read()).decode("utf-8")
|
14 |
+
data_url = f"data:image/jpeg;base64,{encoded_image}"
|
15 |
+
|
16 |
+
# Adjust this prompt for passport info extraction
|
17 |
+
prompt = "Extract all visible information from the front page of the passport. Output in JSON format."
|
18 |
+
|
19 |
+
payload = {
|
20 |
+
"model": IMAGE_MODEL,
|
21 |
+
"messages": [
|
22 |
+
{
|
23 |
+
"role": "user",
|
24 |
+
"content": [
|
25 |
+
{"type": "text", "text": prompt},
|
26 |
+
{"type": "image_url", "image_url": {"url": data_url}}
|
27 |
+
]
|
28 |
+
}
|
29 |
+
]
|
30 |
+
}
|
31 |
+
|
32 |
+
headers = {
|
33 |
+
"Authorization": f"Bearer {API_KEY}",
|
34 |
+
"Content-Type": "application/json"
|
35 |
+
}
|
36 |
+
|
37 |
+
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
38 |
+
result = response.json()
|
39 |
+
|
40 |
+
print("📡 OpenRouter Response:", result)
|
41 |
+
|
42 |
+
# Return the whole raw JSON result
|
43 |
+
return json.dumps(result, indent=2)
|
44 |
+
|
45 |
+
except Exception as e:
|
46 |
+
return f"⚠️ Error: {str(e)}"
|
47 |
+
|
48 |
+
# Gradio Interface
|
49 |
+
iface = gr.Interface(
|
50 |
+
fn=process_passport,
|
51 |
+
inputs=gr.Image(type="file", label="Upload Passport Front"),
|
52 |
+
outputs=gr.Code(label="OpenRouter Raw JSON Result", language="json"),
|
53 |
+
title="Passport Front Image Extractor",
|
54 |
+
description="Upload a front image of a passport. The app will use OpenRouter to extract the visible details and return the result as JSON."
|
55 |
+
)
|
56 |
+
|
57 |
+
if __name__ == "__main__":
|
58 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio~=3.50.2
|
2 |
+
requests>=2.25.0,<3.0.0
|
3 |
+
# pillow might be needed explicitly if not pulled by gradio for image handling
|
4 |
+
Pillow>=9.0.0
|