Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#In-built libraries
|
| 2 |
+
import json
|
| 3 |
+
import tempfile
|
| 4 |
+
import traceback
|
| 5 |
+
from typing import Dict
|
| 6 |
+
|
| 7 |
+
#third-party libraries
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import ollama
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def save_temp_image(image: Image.Image) -> str:
|
| 14 |
+
"""
|
| 15 |
+
Saves the given PIL Image object as a temporary PNG file.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
image (Image.Image): The image to be saved.
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
str: The file path of the saved temporary image.
|
| 22 |
+
"""
|
| 23 |
+
# Create a temp file WITHOUT extension
|
| 24 |
+
with tempfile.NamedTemporaryFile(suffix=".tmp", delete=False) as tmp_file:
|
| 25 |
+
# Save image as PNG regardless of original format
|
| 26 |
+
image.save(tmp_file.name, format="PNG")
|
| 27 |
+
return tmp_file.name
|
| 28 |
+
|
| 29 |
+
def id_extractor(image: Image.Image) -> Dict:
|
| 30 |
+
"""
|
| 31 |
+
Extracts key details from the provided image using the ollama chat model.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
image (Image.Image): The image from which to extract details.
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
Dict: A dictionary containing the extracted details.
|
| 38 |
+
If the image is None or an error occurs, returns an empty dictionary.
|
| 39 |
+
"""
|
| 40 |
+
try:
|
| 41 |
+
error_trace = None
|
| 42 |
+
|
| 43 |
+
if image is None:
|
| 44 |
+
# Return empty dictionary and make the output invisible
|
| 45 |
+
return {}, gr.update(visible=False)
|
| 46 |
+
|
| 47 |
+
# Save the image temporarily
|
| 48 |
+
image_path = save_temp_image(image)
|
| 49 |
+
|
| 50 |
+
# Send the image to the ollama chat model for processing
|
| 51 |
+
response = ollama.chat(
|
| 52 |
+
model='qwen2.5vl:7b',
|
| 53 |
+
messages=[{
|
| 54 |
+
'role': 'user',
|
| 55 |
+
'content': "Extract key details like 'name', 'date of birth', 'ID number', 'Issuer' from the image as JSON, excluding signatures.",
|
| 56 |
+
'images': [image_path]
|
| 57 |
+
}]
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Clean up the response content
|
| 61 |
+
resp = response.message.content.replace("```json", "").replace("```", "").strip()
|
| 62 |
+
return json.loads(resp)
|
| 63 |
+
|
| 64 |
+
except json.JSONDecodeError as e:
|
| 65 |
+
# Capture and print the error traceback
|
| 66 |
+
error_trace = traceback.format_exc()
|
| 67 |
+
print(error_trace)
|
| 68 |
+
return "Kindly upload an image with good clarity"
|
| 69 |
+
|
| 70 |
+
# Define the Gradio interface for the ID extractor
|
| 71 |
+
id_interface = gr.Interface(
|
| 72 |
+
fn=id_extractor,
|
| 73 |
+
inputs=gr.Image(type="pil", label="Upload an image"),
|
| 74 |
+
outputs=gr.JSON(label="Extracted Details"),
|
| 75 |
+
title="Upload your ID",
|
| 76 |
+
description="Upload an image of a document. Key details will be extracted automatically."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Launch the Gradio interface
|
| 80 |
+
id_interface.launch(mcp_server=True)
|