Leeps commited on
Commit
924edd2
·
verified ·
1 Parent(s): 43c575c

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. api/index.py +62 -54
api/index.py CHANGED
@@ -33,63 +33,78 @@ def call_openai(pil_image):
33
  # Encode the image to base64
34
  image_data = base64.b64encode(buffered.getvalue()).decode('utf-8')
35
 
36
- response = client.chat.completions.create(
37
- model="gpt-4o",
38
- messages=[
39
- {
40
- "role": "user",
41
- "content": [
42
- {"type": "text", "text": "You are a product designer. I've attached a moodboard here. In one sentence, what do all of these elements have in common? Answer from a design language perspective, if you were telling another designer to create something similar, including any repeating colors and materials and shapes and textures"},
43
- {
44
- "type": "image_url",
45
- "image_url": {
46
- "url": "data:image/jpeg;base64," + image_data,
 
 
47
  },
48
- },
49
- ],
50
- }
51
- ],
52
- max_tokens=300,
53
- )
54
-
55
- return response.choices[0].message.content
 
 
 
 
 
 
 
56
 
57
  def image_classifier(moodboard, starter_image, image_strength, prompt):
58
 
59
- # Convert the numpy array to a PIL image
60
- pil_image = Image.fromarray(moodboard.astype('uint8'))
61
- starter_image_pil = Image.fromarray(starter_image.astype('uint8'))
62
-
63
- # Resize the starter image if either dimension is larger than 768 pixels
64
- if starter_image_pil.size[0] > 768 or starter_image_pil.size[1] > 768:
65
- # Calculate the new size while maintaining the aspect ratio
66
- if starter_image_pil.size[0] > starter_image_pil.size[1]:
67
- # Width is larger than height
68
- new_width = 768
69
- new_height = int((768 / starter_image_pil.size[0]) * starter_image_pil.size[1])
70
- else:
71
- # Height is larger than width
72
- new_height = 768
73
- new_width = int((768 / starter_image_pil.size[1]) * starter_image_pil.size[0])
 
 
 
 
 
74
 
75
- # Resize the image
76
- starter_image_pil = starter_image_pil.resize((new_width, new_height), Image.LANCZOS)
77
-
78
- openai_response = call_openai(pil_image)
79
- openai_response = openai_response.replace('moodboard', '')
80
-
81
- # Save the starter image to a bytes buffer
82
- buffered = io.BytesIO()
83
- starter_image_pil.save(buffered, format="JPEG")
84
-
85
- # Encode the starter image to base64
86
- starter_image_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
 
87
 
88
  # Call Stable Diffusion API with the response from OpenAI
89
  input = {
90
  "width": 768,
91
  "height": 768,
92
- "prompt": "high quality render of " + prompt + ", " + openai_response[20:],
93
  "negative_prompt": "worst quality, low quality, illustration, 2d, painting, cartoons, sketch",
94
  "refine": "expert_ensemble_refiner",
95
  "image": "data:image/jpeg;base64," + starter_image_base64,
@@ -125,12 +140,5 @@ def image_classifier(moodboard, starter_image, image_strength, prompt):
125
 
126
  return [img1, img2, img3] # Return the image object
127
 
128
-
129
- # app = Flask(__name__)
130
- # os.environ.get("REPLICATE_API_TOKEN")
131
-
132
- # @app.route("/")
133
- # def index():
134
-
135
  demo = gr.Interface(fn=image_classifier, inputs=["image", "image", gr.Slider(0, 1, step=0.025, value=0.2, label="Image Strength"), "text"], outputs=["image", "image", "image"])
136
  demo.launch(share=False)
 
33
  # Encode the image to base64
34
  image_data = base64.b64encode(buffered.getvalue()).decode('utf-8')
35
 
36
+ try:
37
+ response = client.chat.completions.create(
38
+ model="gpt-4o",
39
+ messages=[
40
+ {
41
+ "role": "user",
42
+ "content": [
43
+ {"type": "text", "text": "You are a product designer. I've attached a moodboard here. In one sentence, what do all of these elements have in common? Answer from a design language perspective, if you were telling another designer to create something similar, including any repeating colors and materials and shapes and textures"},
44
+ {
45
+ "type": "image_url",
46
+ "image_url": {
47
+ "url": "data:image/jpeg;base64," + image_data,
48
+ },
49
  },
50
+ ],
51
+ }
52
+ ],
53
+ max_tokens=300,
54
+ )
55
+
56
+ return response.choices[0].message.content
57
+
58
+ except openai.BadRequestError as e:
59
+ print(e)
60
+ print("e type")
61
+ print(type(e))
62
+ raise gr.Error(f"Please retry with a different moodboard file")
63
+ except Exception as e:
64
+ raise gr.Error("Unknown Error")
65
 
66
  def image_classifier(moodboard, starter_image, image_strength, prompt):
67
 
68
+ if moodboard is not None and starter_image is not None:
69
+
70
+ # Convert the numpy array to a PIL image
71
+ pil_image = Image.fromarray(moodboard.astype('uint8'))
72
+ starter_image_pil = Image.fromarray(starter_image.astype('uint8'))
73
+
74
+ # Resize the starter image if either dimension is larger than 768 pixels
75
+ if starter_image_pil.size[0] > 768 or starter_image_pil.size[1] > 768:
76
+ # Calculate the new size while maintaining the aspect ratio
77
+ if starter_image_pil.size[0] > starter_image_pil.size[1]:
78
+ # Width is larger than height
79
+ new_width = 768
80
+ new_height = int((768 / starter_image_pil.size[0]) * starter_image_pil.size[1])
81
+ else:
82
+ # Height is larger than width
83
+ new_height = 768
84
+ new_width = int((768 / starter_image_pil.size[1]) * starter_image_pil.size[0])
85
+
86
+ # Resize the image
87
+ starter_image_pil = starter_image_pil.resize((new_width, new_height), Image.LANCZOS)
88
 
89
+ openai_response = call_openai(pil_image)
90
+ openai_response = openai_response.replace('moodboard', '')
91
+ openai_response = openai_response.replace('share', '')
92
+ openai_response = openai_response.replace('unified', '')
93
+
94
+ # Save the starter image to a bytes buffer
95
+ buffered = io.BytesIO()
96
+ starter_image_pil.save(buffered, format="JPEG")
97
+
98
+ # Encode the starter image to base64
99
+ starter_image_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
100
+ else:
101
+ raise gr.Error(f"Please upload a moodboard to control image generation style")
102
 
103
  # Call Stable Diffusion API with the response from OpenAI
104
  input = {
105
  "width": 768,
106
  "height": 768,
107
+ "prompt": "high quality render of " + prompt + ", " + openai_response[12:],
108
  "negative_prompt": "worst quality, low quality, illustration, 2d, painting, cartoons, sketch",
109
  "refine": "expert_ensemble_refiner",
110
  "image": "data:image/jpeg;base64," + starter_image_base64,
 
140
 
141
  return [img1, img2, img3] # Return the image object
142
 
 
 
 
 
 
 
 
143
  demo = gr.Interface(fn=image_classifier, inputs=["image", "image", gr.Slider(0, 1, step=0.025, value=0.2, label="Image Strength"), "text"], outputs=["image", "image", "image"])
144
  demo.launch(share=False)