Update app.py
Browse files
app.py
CHANGED
|
@@ -52,62 +52,62 @@ chatgpt_chain = LLMChain(
|
|
| 52 |
)
|
| 53 |
|
| 54 |
#Helper function as this is what Pebblely API takes in
|
| 55 |
-
def image_to_base64(img_path):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
#Removes backdrop from image using Pebblely API
|
| 60 |
-
def removeBackdrop(image_path, output_path):
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
#Takes the images from Pebblely output and generates images with different backdrops based on user prompts
|
| 81 |
-
def DallEBackdropGen(image_path, prompt, n_img, size):
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
#Just outputs the images from DallE in a nice grid for easy consumption
|
| 93 |
-
def outputGenImages(ori_image, response_data):
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
-
|
| 111 |
|
| 112 |
def upload_and_process(image, promptText, promptImg, lens_option, n_images, size):
|
| 113 |
# We are assuming the removeBackdrop, DallEBackdropGen, outputGenImages are defined and available in your code
|
|
@@ -144,7 +144,7 @@ def upload_and_process(image, promptText, promptImg, lens_option, n_images, size
|
|
| 144 |
# # Get and return the images
|
| 145 |
# result_images = outputGenImages(image_path, response_data)
|
| 146 |
|
| 147 |
-
|
| 148 |
|
| 149 |
return text_output#, result_images # Output should be a list of PIL Image objects or numpy arrays
|
| 150 |
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
#Helper function as this is what Pebblely API takes in
|
| 55 |
+
# def image_to_base64(img_path):
|
| 56 |
+
# with open(img_path, "rb") as image_file:
|
| 57 |
+
# return base64.b64encode(image_file.read()).decode('utf-8')
|
| 58 |
+
|
| 59 |
+
# #Removes backdrop from image using Pebblely API
|
| 60 |
+
# def removeBackdrop(image_path, output_path):
|
| 61 |
+
# endpoint_url = "https://api.pebblely.com/remove-background/v1/"
|
| 62 |
+
|
| 63 |
+
# response = requests.post(
|
| 64 |
+
# endpoint_url,
|
| 65 |
+
# headers={
|
| 66 |
+
# "Content-Type": "application/json",
|
| 67 |
+
# "X-Pebblely-Access-Token": os.environ["PEBBLELY_TOKEN"]
|
| 68 |
+
# },
|
| 69 |
+
# json={
|
| 70 |
+
# "image": image_to_base64(image_path),
|
| 71 |
+
# }
|
| 72 |
+
# )
|
| 73 |
+
# print(response)
|
| 74 |
+
# image_b64 = response.json()["data"]
|
| 75 |
+
# image_encoded = image_b64.encode("utf-8")
|
| 76 |
+
# image_bytes = io.BytesIO(base64.b64decode(image_encoded))
|
| 77 |
+
# image = Image.open(image_bytes)
|
| 78 |
+
# image.save(output_path)
|
| 79 |
|
| 80 |
#Takes the images from Pebblely output and generates images with different backdrops based on user prompts
|
| 81 |
+
# def DallEBackdropGen(image_path, prompt, n_img, size):
|
| 82 |
+
# response = openai.Image.create_edit(
|
| 83 |
+
# image=open(image_path, "rb"),
|
| 84 |
+
# prompt=prompt,
|
| 85 |
+
# n=n_img,
|
| 86 |
+
# size=size
|
| 87 |
+
# )
|
| 88 |
+
# print(type(response))
|
| 89 |
+
# print(response)
|
| 90 |
+
# return response['data']
|
| 91 |
|
| 92 |
#Just outputs the images from DallE in a nice grid for easy consumption
|
| 93 |
+
# def outputGenImages(ori_image, response_data):
|
| 94 |
+
# nrows = len(response_data)
|
| 95 |
+
# # List of image URLs
|
| 96 |
+
# image_urls = [response_data[i]['url'] for i in range(nrows)]
|
| 97 |
|
| 98 |
+
# # Load all the images into a list
|
| 99 |
+
# images = [Image.open(BytesIO(requests.get(url).content)) for url in image_urls]
|
| 100 |
|
| 101 |
+
# # Create a new image of the right size
|
| 102 |
+
# width, height = images[0].size # assumes all images are the same size
|
| 103 |
+
# total_height = height * nrows
|
| 104 |
+
# new_im = Image.new('RGB', (width, total_height))
|
| 105 |
|
| 106 |
+
# # Paste in each image
|
| 107 |
+
# for i, img in enumerate(images):
|
| 108 |
+
# new_im.paste(img, (0, i * height))
|
| 109 |
|
| 110 |
+
# return new_im
|
| 111 |
|
| 112 |
def upload_and_process(image, promptText, promptImg, lens_option, n_images, size):
|
| 113 |
# We are assuming the removeBackdrop, DallEBackdropGen, outputGenImages are defined and available in your code
|
|
|
|
| 144 |
# # Get and return the images
|
| 145 |
# result_images = outputGenImages(image_path, response_data)
|
| 146 |
|
| 147 |
+
text_output = f"<h2>{json.dumps(responses, indent=2)}</h2>"
|
| 148 |
|
| 149 |
return text_output#, result_images # Output should be a list of PIL Image objects or numpy arrays
|
| 150 |
|