Skip to content

Commit

Permalink
Add image saving functionality to OtterHD endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Luodian committed Nov 7, 2023
1 parent a94be9f commit ac45c79
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions pipeline/serve/deploy/otterhd_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import re
from io import BytesIO
from datetime import datetime
import hashlib
from PIL import Image
import io, os

app = Flask(__name__)

Expand All @@ -20,10 +23,38 @@

# Ensure model is in evaluation mode
model.eval()

prompt_txt_path = "../user_logs/prompts.txt"
images_folder_path = "../user_logs"

def save_image_unique(pil_image, directory=images_folder_path):
# Ensure the directory exists
if not os.path.exists(directory):
os.makedirs(directory)

# Convert the PIL Image into a bytes object
img_byte_arr = io.BytesIO()
pil_image.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()

# Compute the hash of the image data
hasher = hashlib.sha256()
hasher.update(img_byte_arr)
hash_hex = hasher.hexdigest()

# Create a file name with the hash value
file_name = f"{hash_hex}.png"
file_path = os.path.join(directory, file_name)

# Check if a file with the same name exists
if os.path.isfile(file_path):
print(f"Image already exists with the name: {file_name}")
else:
# If the file does not exist, save the image
with open(file_path, 'wb') as new_file:
new_file.write(img_byte_arr)
print(f"Image saved with the name: {file_name}")

return file_path

# Define endpoint
@app.route("/app/otter", methods=["POST"])
Expand All @@ -42,19 +73,17 @@ def process_image_and_prompt():
image = Image.open(BytesIO(base64.b64decode(image_data)))
prompt = query_content["prompt"]
formated_time = start_time.strftime("%Y-%m-%d %H:%M:%S")

image_path = f"{images_folder_path}/{formated_time}.jpg"
with open(image_path, "wb") as f:
f.write(base64.b64decode(image_data))
image_path = save_image_unique(image)

# Preprocess the image and prompt, and run the model
response = predict(image, prompt)
torch.cuda.empty_cache()

with open(prompt_txt_path, "a") as f:
f.write(f"*************************{formated_time}**************************" + "\n")
f.write(prompt + "\n")
f.write(response + "\n\n")
f.write(f"Image saved to {image_path}" + "\n")
f.write(f"Prompt: {prompt}" + "\n")
f.write(f"Response: {response}" + "\n\n")

# Return the response
return jsonify({"result": response})
Expand Down

0 comments on commit ac45c79

Please sign in to comment.