sayedM commited on
Commit
40144d4
1 Parent(s): 97db0d0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import io
3
+ import cv2
4
+ import requests
5
+ import json
6
+ import gradio as gr
7
+ import os
8
+ from PIL import Image
9
+
10
+ # Accessing a specific environment variable
11
+ api_key = os.environ.get('PXiVision')
12
+
13
+ # Checking if the environment variable exists
14
+ if not api_key:
15
+ print("PXiVision environment variable is not set.")
16
+ exit()
17
+
18
+ # Define a function to call the API and get the results
19
+ def get_results(image):
20
+ threshold = 0.5
21
+
22
+ # Convert the NumPy array to PIL image
23
+ image = Image.fromarray(image)
24
+
25
+ # Convert the image to base64 string
26
+ with io.BytesIO() as output:
27
+ image.save(output, format="JPEG")
28
+ base64str = base64.b64encode(output.getvalue()).decode("utf-8")
29
+
30
+ # Prepare the payload
31
+ payload = json.dumps({"base64str": base64str, "threshold": threshold})
32
+
33
+ # Send the request to the API
34
+ response = requests.put(api_key, data=payload)
35
+
36
+ # Parse the JSON response
37
+ data = response.json()
38
+ # data = json.loads(data)
39
+
40
+
41
+ # Access the values
42
+ firstName = data['firstName']
43
+ secondName = data['secondName']
44
+ address1 = data['address1']
45
+ address2 = data['address2']
46
+ nationalIdNumber = data['nationalIdNumber']
47
+ timeOfResponse = data['timeOfResponse']
48
+ requestInfo = data['requestInfo']
49
+ return [firstName, secondName, address1, address2, nationalIdNumber, timeOfResponse, requestInfo]
50
+
51
+ # Define the input component for Gradio
52
+ image_input = gr.inputs.Image() # Adjust the shape according to your requirements
53
+
54
+ # Define the output components for Gradio
55
+ output_components = []
56
+ for label in ["First Name", "Second Name", "Address 1", "Address 2", "National ID Number", "Time of Response", "Request Info"]:
57
+ output_components.append(gr.outputs.Textbox(label=label))
58
+
59
+ # Launch the Gradio interface
60
+ gr.Interface(fn=get_results, inputs=image_input, outputs=output_components).launch(share=False)