change to openai image generation
Browse files
app.py
CHANGED
|
@@ -106,6 +106,7 @@ def get_status(flower_name):
|
|
| 106 |
|
| 107 |
|
| 108 |
|
|
|
|
| 109 |
# Main function to process the uploaded image
|
| 110 |
def process_image(img):
|
| 111 |
print("Starting prediction...")
|
|
@@ -121,30 +122,33 @@ def process_image(img):
|
|
| 121 |
endangerment_status = get_status(predicted_class)
|
| 122 |
print(f"Status found: {endangerment_status}")
|
| 123 |
|
| 124 |
-
# Generate artistic interpretation
|
| 125 |
-
print("Sending request to
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
"
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
print("Image retrieved and ready to return")
|
| 144 |
return classification_results, generated_image, wiki_url, endangerment_status
|
| 145 |
|
| 146 |
|
| 147 |
-
|
| 148 |
# Function to clear all outputs
|
| 149 |
def clear_outputs():
|
| 150 |
return {
|
|
|
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
+
# Main function to process the uploaded image
|
| 110 |
# Main function to process the uploaded image
|
| 111 |
def process_image(img):
|
| 112 |
print("Starting prediction...")
|
|
|
|
| 122 |
endangerment_status = get_status(predicted_class)
|
| 123 |
print(f"Status found: {endangerment_status}")
|
| 124 |
|
| 125 |
+
# Generate artistic interpretation using DALL-E
|
| 126 |
+
print("Sending request to DALL-E...")
|
| 127 |
+
try:
|
| 128 |
+
response = openai.images.generate(
|
| 129 |
+
model="dall-e-3",
|
| 130 |
+
prompt=random.choice(prompt_templates).format(flower=predicted_class),
|
| 131 |
+
size="1024x1024",
|
| 132 |
+
quality="standard",
|
| 133 |
+
n=1,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# Get the image URL
|
| 137 |
+
image_url = response.data[0].url
|
| 138 |
+
print(f"Image URL: {image_url}")
|
| 139 |
+
|
| 140 |
+
# Download the generated image
|
| 141 |
+
response = requests.get(image_url)
|
| 142 |
+
generated_image = Image.open(io.BytesIO(response.content))
|
| 143 |
+
|
| 144 |
+
except Exception as e:
|
| 145 |
+
print(f"Error generating image: {e}")
|
| 146 |
+
generated_image = None
|
| 147 |
|
| 148 |
print("Image retrieved and ready to return")
|
| 149 |
return classification_results, generated_image, wiki_url, endangerment_status
|
| 150 |
|
| 151 |
|
|
|
|
| 152 |
# Function to clear all outputs
|
| 153 |
def clear_outputs():
|
| 154 |
return {
|