sayedM commited on
Commit
deb3e38
·
verified ·
1 Parent(s): c0a9973

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import base64
4
+ from PIL import Image
5
+ import io
6
+ import json
7
+
8
+ # The target URL for the API endpoint
9
+ API_URL = os.environ.get('PXiVision')
10
+
11
+ def process_image(input_image: Image.Image):
12
+ """
13
+ Takes a PIL Image, converts it to base64, sends it to an API,
14
+ and returns the JSON response.
15
+
16
+ Args:
17
+ input_image: A PIL Image object from the Gradio input.
18
+
19
+ Returns:
20
+ A dictionary (JSON) from the API response or an error message.
21
+ """
22
+ if input_image is None:
23
+ return {"error": "Please upload an image first."}
24
+
25
+ try:
26
+ # Convert PIL Image to bytes
27
+ with io.BytesIO() as byte_arr:
28
+ input_image.save(byte_arr, format='JPEG') # You can change format if needed (e.g., PNG)
29
+ image_bytes = byte_arr.getvalue()
30
+
31
+ # Encode the bytes to a base64 string
32
+ base64_image = base64.b64encode(image_bytes).decode("utf-8")
33
+
34
+ # Prepare the request payload
35
+ payload = {
36
+ "image": base64_image
37
+ }
38
+
39
+ # Send the POST request to the API
40
+ response = requests.post(API_URL, json=payload, timeout=60) # Added a 60-second timeout
41
+
42
+ # Check if the request was successful
43
+ response.raise_for_status() # This will raise an exception for HTTP errors (4xx, 5xx)
44
+
45
+ # Return the JSON response from the API
46
+ return response.json()
47
+
48
+ except requests.exceptions.RequestException as e:
49
+ # Handle network-related errors
50
+ return {"error": f"API request failed: {e}"}
51
+ except Exception as e:
52
+ # Handle other potential errors (e.g., image processing)
53
+ return {"error": f"An unexpected error occurred: {e}"}
54
+
55
+ # --- Create the Gradio Interface ---
56
+
57
+ # Define the input and output components for the interface
58
+ image_input = gr.Image(type="pil", label="Upload National ID Image")
59
+ json_output = gr.JSON(label="Extracted Information")
60
+
61
+ # Create a descriptive title and description for the app
62
+ title = "Egyptian National ID Extractor"
63
+ description = """
64
+ Upload a clear image of an Egyptian National ID card.
65
+ The application will send the image to a processing service and display the extracted information,
66
+ such as name, address, and National ID number.
67
+ """
68
+
69
+ # Assemble the interface
70
+ demo = gr.Interface(
71
+ fn=process_image,
72
+ inputs=image_input,
73
+ outputs=json_output,
74
+ title=title,
75
+ description=description,
76
+ allow_flagging="never",
77
+ examples=[
78
+ ['RyEJpZSdJPm4YU2QLH4khP5bzpbrZHC0Vj0oigis.jpg'] # Example image path
79
+ ]
80
+ )
81
+
82
+ # --- Launch the App ---
83
+ if __name__ == "__main__":
84
+ demo.launch()