Skip to content

Commit

Permalink
Change openai dall-e to flux in hal9 app (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisGuillen03 authored Nov 18, 2024
1 parent 113824d commit 970bce8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
1 change: 1 addition & 0 deletions apps/hal9/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
replicate==1.0.3
39 changes: 15 additions & 24 deletions apps/hal9/tools/image.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
import hal9 as h9
import openai
import os
import requests
from PIL import Image
from io import BytesIO
import shutil
import replicate

def create_image(prompt, filename):
def create_image(prompt):
"""
Creates an image or photograph for the user
'prompt' with the description of the image or photograph
'filename' a descriptive filename with png extension for the image or photograph to generate
"""
client = openai.AzureOpenAI(
azure_endpoint = 'https://hal9-azure-openai-eastus.openai.azure.com/',
api_key = os.environ['DALLE_AZURE'],
api_version = "2024-02-01",
)
filename = "hal9-flux.jpg"
try:
output = replicate.run("black-forest-labs/flux-dev", input={"prompt": prompt})
encoded_image = output[0].read()
image = Image.open(BytesIO(encoded_image))

response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1024x1024",
quality="standard",
n=1,
)
url = response.data[0].url
image.save(filename, format="JPEG")

response = requests.get(url)
shutil.copy(filename, f".storage/{filename}")

with open('.storage/' + filename, 'wb') as file:
file.write(response.content)

return f"Generated a {filename} that {prompt}"
return f"Generated a {filename} that {prompt}"
except Exception as e:
return f"Couldn't generate that image. Please try a different prompt. \n\n Error: {e}"

0 comments on commit 970bce8

Please sign in to comment.