Leeps commited on
Commit
374f8ce
1 Parent(s): 266a587

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. api/index.py +95 -52
api/index.py CHANGED
@@ -8,10 +8,16 @@ import requests
8
  import replicate
9
  from flask import Flask, request
10
  import gradio as gr
 
 
11
  from openai import OpenAI
12
 
 
13
  from dotenv import load_dotenv, find_dotenv
14
 
 
 
 
15
  # Locate the .env file
16
  dotenv_path = find_dotenv()
17
 
@@ -33,79 +39,116 @@ 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, prompt):
58
 
59
- # Convert the numpy array to a PIL image
60
- pil_image = Image.fromarray(moodboard.astype('uint8'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- openai_response = call_openai(pil_image)
63
- openai_response = openai_response.replace('moodboard', '')
64
-
 
 
 
 
 
 
 
 
65
  # Call Stable Diffusion API with the response from OpenAI
66
  input = {
67
  "width": 768,
68
  "height": 768,
69
- "prompt": "high quality render of " + prompt + ", " + openai_response[20:],
70
  "negative_prompt": "worst quality, low quality, illustration, 2d, painting, cartoons, sketch",
71
  "refine": "expert_ensemble_refiner",
72
  "apply_watermark": False,
73
  "num_inference_steps": 25,
74
- "num_outputs": 3
75
  }
76
 
77
  output = replicate.run(
78
  "stability-ai/sdxl:7762fd07cf82c948538e41f63f77d685e02b063e37e496e96eefd46c929f9bdc",
79
  input=input
80
  )
 
 
 
 
 
 
 
81
 
82
- # Download the image from the URL
83
- image_url = output[0]
84
- print(image_url)
85
- response = requests.get(image_url)
86
- print(response)
87
- img1 = Image.open(io.BytesIO(response.content))
88
-
89
- image_url = output[1]
90
- print(image_url)
91
- response = requests.get(image_url)
92
- print(response)
93
- img2 = Image.open(io.BytesIO(response.content))
94
-
95
- image_url = output[2]
96
- print(image_url)
97
- response = requests.get(image_url)
98
- print(response)
99
- img3 = Image.open(io.BytesIO(response.content))
100
-
101
- return [img1, img2, img3] # Return the image object
102
 
103
 
104
- # app = Flask(__name__)
105
- # os.environ.get("REPLICATE_API_TOKEN")
106
 
107
- # @app.route("/")
108
- # def index():
109
 
110
- demo = gr.Interface(fn=image_classifier, inputs=["image", "text"], outputs=["image", "image", "image"])
111
- demo.launch(share=True)
 
8
  import replicate
9
  from flask import Flask, request
10
  import gradio as gr
11
+
12
+ import openai
13
  from openai import OpenAI
14
 
15
+
16
  from dotenv import load_dotenv, find_dotenv
17
 
18
+ import json
19
+
20
+
21
  # Locate the .env file
22
  dotenv_path = find_dotenv()
23
 
 
39
  # Encode the image to base64
40
  image_data = base64.b64encode(buffered.getvalue()).decode('utf-8')
41
 
42
+ try:
43
+ response = client.chat.completions.create(
44
+ model="gpt-4o",
45
+ messages=[
46
+ {
47
+ "role": "user",
48
+ "content": [
49
+ {"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"},
50
+ {
51
+ "type": "image_url",
52
+ "image_url": {
53
+ "url": "data:image/jpeg;base64," + image_data,
54
+ },
55
  },
56
+ ],
57
+ }
58
+ ],
59
+ max_tokens=300,
60
+ )
61
+ return response.choices[0].message.content
62
+ except openai.BadRequestError as e:
63
+ print(e)
64
+ print("e type")
65
+ print(type(e))
66
+ raise gr.Error(f"Please retry with a different moodboard file (below 20 MB in size and is of one the following formats: ['png', 'jpeg', 'gif', 'webp'])")
67
+ except Exception as e:
68
+ raise gr.Error("Unknown Error")
69
+
70
 
71
  def image_classifier(moodboard, prompt):
72
 
73
+ if moodboard is not None:
74
+ pil_image = Image.fromarray(moodboard.astype('uint8'))
75
+
76
+ openai_response = call_openai(pil_image)
77
+ openai_response = openai_response.replace('moodboard', '')
78
+ openai_response = openai_response.replace('share', '')
79
+ openai_response = openai_response.replace('unified', '')
80
+ else:
81
+ raise gr.Error(f"Please upload a moodboard to control image generation style")
82
+
83
+ input = {
84
+ "prompt": "high quality render of " + prompt + ", " + openai_response[12:],
85
+ "negative_prompt": "worst quality, low quality, illustration, 2d, painting, cartoons, sketch",
86
+ "output_format": "jpg"
87
+ }
88
+
89
+ try:
90
+ output = replicate.run(
91
+ "stability-ai/stable-diffusion-3",
92
+ input=input
93
+ )
94
+ except Exception as e:
95
+ raise gr.Error(f"Error: {e}")
96
+
97
+ try:
98
+ image_url = output[0]
99
+ response = requests.get(image_url)
100
+ img1 = Image.open(io.BytesIO(response.content))
101
+ except Exception as e:
102
+ raise gr.Error(f"Image download failed: {e}")
103
+
104
+ input["aspect_ratio"] = "3:2"
105
+ input["cfg"] = 6
106
 
107
+ try:
108
+ output = replicate.run(
109
+ "stability-ai/stable-diffusion-3",
110
+ input=input
111
+ )
112
+ image_url = output[0]
113
+ response = requests.get(image_url)
114
+ img2 = Image.open(io.BytesIO(response.content))
115
+ except Exception as e:
116
+ raise gr.Error(f"Second image download failed: {e}")
117
+
118
  # Call Stable Diffusion API with the response from OpenAI
119
  input = {
120
  "width": 768,
121
  "height": 768,
122
+ "prompt": "high quality render of " + prompt + ", " + openai_response[12:],
123
  "negative_prompt": "worst quality, low quality, illustration, 2d, painting, cartoons, sketch",
124
  "refine": "expert_ensemble_refiner",
125
  "apply_watermark": False,
126
  "num_inference_steps": 25,
127
+ "num_outputs": 2
128
  }
129
 
130
  output = replicate.run(
131
  "stability-ai/sdxl:7762fd07cf82c948538e41f63f77d685e02b063e37e496e96eefd46c929f9bdc",
132
  input=input
133
  )
134
+
135
+ images = [img1, img2]
136
+
137
+ for i in range(min(len(output), 2)):
138
+ image_url = output[i]
139
+ response = requests.get(image_url)
140
+ images.append(Image.open(io.BytesIO(response.content)))
141
 
142
+ # Add empty images if fewer than 3 were returned
143
+ while len(images) < 4:
144
+ images.append(Image.new('RGB', (768, 768), 'gray'))
145
+
146
+ images.reverse()
147
+ return images
148
+
149
+ demo = gr.Interface(fn=image_classifier, inputs=["image", "text"], outputs=["image", "image", "image", "image"])
150
+ demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
151
 
152
 
 
 
153
 
 
 
154