sayedM commited on
Commit
d5080b5
1 Parent(s): 5c978c9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, "UID": "huggingfacetestsayed"})
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
+ num = data['num']
43
+ char = data['char']
44
+ timeOfResponse = data['time']
45
+ requestInfo = data['info']
46
+ return [num, char, timeOfResponse, requestInfo]
47
+
48
+ # Define the input component for Gradio
49
+ image_input = gr.inputs.Image() # Adjust the shape according to your requirements
50
+
51
+ # Define the output components for Gradio
52
+ output_components = []
53
+ for label in ["Numbers", "char", "Time of Response", "Request Info"]:
54
+ output_components.append(gr.outputs.Textbox(label=label))
55
+
56
+ # Launch the Gradio interface
57
+ gr.Interface(fn=get_results,examples=["test.jpg"], inputs=image_input, outputs=output_components).launch(share=False)