SayedMo commited on
Commit
cef0ce8
·
1 Parent(s): f38334a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import numpy as np
10
+ from PIL import ImageOps
11
+
12
+ # Accessing a specific environment variable
13
+ api_key = os.environ.get('devisionx')
14
+
15
+ # Checking if the environment variable exists
16
+ if not api_key:
17
+ print("devisionx environment variable is not set.")
18
+ exit()
19
+
20
+ # Define a function to call the API and get the results
21
+
22
+ def base64str_to_PILImage(base64str):
23
+ base64_img_bytes = base64str.encode('utf-8')
24
+ base64bytes = base64.b64decode(base64_img_bytes)
25
+ bytesObj = io.BytesIO(base64bytes)
26
+ return ImageOps.exif_transpose(Image.open(bytesObj))
27
+
28
+ def get_results(image, prompt):
29
+ threshold = 0.5
30
+
31
+ # Convert the NumPy array to PIL image
32
+ image = Image.fromarray(image)
33
+
34
+ # Convert the image to base64 string
35
+ with io.BytesIO() as output:
36
+ image.save(output, format="JPEG")
37
+ base64str = base64.b64encode(output.getvalue()).decode("utf-8")
38
+
39
+ # Prepare the payload (Adjust this part according to the API requirements)
40
+ payload = json.dumps({"base64str": base64str, "classes": prompt})
41
+
42
+ # Send the request to the API
43
+ response = requests.put(api_key, data=payload)
44
+
45
+ # Parse the JSON response
46
+ data = response.json()
47
+ print(response.status_code)
48
+ print(data)
49
+
50
+ # Access the values (Adjust this part according to the API response format)
51
+ output_image_base64 = data['firstName'] # Assuming the API returns the output image as base64
52
+
53
+
54
+ # Convert the output image from base64 to PIL and then to NumPy array
55
+ output_image = base64str_to_PILImage(output_image_base64)
56
+ output_image = np.array(output_image)
57
+
58
+ return output_image
59
+
60
+ # Define the input components for Gradio (adding a new input for the prompt)
61
+ image_input = gr.inputs.Image()
62
+ text_input = gr.inputs.Textbox(label="Prompt") # New input for the text prompt
63
+
64
+ # Define the output components for Gradio (including both image and text)
65
+ outputs = gr.Image(type="numpy", label="Output Image")
66
+
67
+ # Launch the Gradio interface
68
+ gr.Interface(fn=get_results, inputs=[image_input, text_input], outputs=outputs).launch(share=False)