sayedM's picture
Update app.py
16a3623
raw
history blame
2.09 kB
import base64
import io
import cv2
import requests
import json
import gradio as gr
import os
from PIL import Image
# Accessing a specific environment variable
api_key = os.environ.get('PXiVision')
# Checking if the environment variable exists
if not api_key:
print("PXiVision environment variable is not set.")
exit()
# Define a function to call the API and get the results
def get_results(image):
threshold = 0.5
# Convert the NumPy array to PIL image
image = Image.fromarray(image)
# Convert the image to base64 string
with io.BytesIO() as output:
image.save(output, format="JPEG")
base64str = base64.b64encode(output.getvalue()).decode("utf-8")
# Prepare the payload
payload = json.dumps({"base64str": base64str, "threshold": threshold})
# Send the request to the API
response = requests.put(api_key, data=payload)
# Parse the JSON response
data = response.json()
data = json.loads(data)
# Access the values
firstName = data['firstName']
secondName = data['secondName']
address1 = data['address1']
address2 = data['address2']
nationalIdNumber = data['nationalIdNumber']
timeOfResponse = data['timeOfResponse']
requestInfo = data['requestInfo']
# Return the values as a list
return {
"First Name": firstName,
"Second Name": secondName,
"Address 1": address1,
"Address 2": address2,
"National ID Number": nationalIdNumber,
"Time of Response": timeOfResponse,
"Request Info": requestInfo
}
# Define the input component for Gradio
image_input = gr.inputs.Image() # Adjust the shape according to your requirements
# Define the output components for Gradio
output_components = []
for label in ["First Name", "Second Name", "Address 1", "Address 2", "National ID Number", "Time of Response", "Request Info"]:
output_components.append(gr.outputs.TextboxOutput(label=label))
# Launch the Gradio interface
gr.Interface(fn=get_results, inputs=image_input, outputs=output_components).launch(share=True)