Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import requests
|
|
| 3 |
from io import BytesIO
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
TOKEN = os.getenv("TOKEN1")
|
| 8 |
API_URL = os.getenv("API_URL")
|
|
@@ -19,21 +20,14 @@ def get_image_from_url(url):
|
|
| 19 |
response = requests.get(url, stream=True)
|
| 20 |
response.raise_for_status()
|
| 21 |
image = Image.open(BytesIO(response.content))
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
if image.format == "WEBP":
|
| 25 |
-
image = image.convert("RGBA") # Convert to RGBA to handle transparency
|
| 26 |
-
output_buffer = BytesIO()
|
| 27 |
-
image.save(output_buffer, format="PNG")
|
| 28 |
-
output_buffer.seek(0) # Reset buffer position for reading
|
| 29 |
-
image = Image.open(output_buffer) # Re-open image from buffer
|
| 30 |
-
|
| 31 |
-
return image
|
| 32 |
|
| 33 |
except requests.exceptions.RequestException as e:
|
| 34 |
-
return f"Error fetching image: {e}"
|
| 35 |
except Exception as e:
|
| 36 |
-
return f"Error processing image: {e}"
|
|
|
|
| 37 |
|
| 38 |
def generate_image(prompt, aspect_ratio, realism):
|
| 39 |
global token_id
|
|
@@ -58,17 +52,34 @@ def generate_image(prompt, aspect_ratio, realism):
|
|
| 58 |
response_data = generate_image(prompt, aspect_ratio, realism)
|
| 59 |
tokens_tried = 0
|
| 60 |
return response_data
|
| 61 |
-
return "No credits available"
|
| 62 |
-
return response_data
|
| 63 |
elif "output" in response_data:
|
| 64 |
url = response_data['output']
|
| 65 |
-
|
|
|
|
| 66 |
else:
|
| 67 |
-
return "Error: Unexpected response from server"
|
| 68 |
except Exception as e:
|
| 69 |
-
return f"Error"
|
| 70 |
-
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
interface = gr.Interface(
|
| 73 |
fn=generate_image,
|
| 74 |
inputs=[
|
|
@@ -80,7 +91,10 @@ interface = gr.Interface(
|
|
| 80 |
),
|
| 81 |
gr.Checkbox(label="Realism", value=False), # Checkbox for realism (True/False)
|
| 82 |
],
|
| 83 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 84 |
title="Image Generator",
|
| 85 |
description="Provide a prompt, select an aspect ratio, and set realism to generate an image.",
|
| 86 |
)
|
|
|
|
| 3 |
from io import BytesIO
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
| 6 |
+
import tempfile
|
| 7 |
|
| 8 |
TOKEN = os.getenv("TOKEN1")
|
| 9 |
API_URL = os.getenv("API_URL")
|
|
|
|
| 20 |
response = requests.get(url, stream=True)
|
| 21 |
response.raise_for_status()
|
| 22 |
image = Image.open(BytesIO(response.content))
|
| 23 |
+
return image, url # Return the image and the URL
|
| 24 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
except requests.exceptions.RequestException as e:
|
| 27 |
+
return f"Error fetching image: {e}", None
|
| 28 |
except Exception as e:
|
| 29 |
+
return f"Error processing image: {e}", None
|
| 30 |
+
|
| 31 |
|
| 32 |
def generate_image(prompt, aspect_ratio, realism):
|
| 33 |
global token_id
|
|
|
|
| 52 |
response_data = generate_image(prompt, aspect_ratio, realism)
|
| 53 |
tokens_tried = 0
|
| 54 |
return response_data
|
| 55 |
+
return "No credits available", None
|
| 56 |
+
return response_data, None
|
| 57 |
elif "output" in response_data:
|
| 58 |
url = response_data['output']
|
| 59 |
+
image, url = get_image_from_url(url)
|
| 60 |
+
return image, url # Return the image and the URL
|
| 61 |
else:
|
| 62 |
+
return "Error: Unexpected response from server", None
|
| 63 |
except Exception as e:
|
| 64 |
+
return f"Error", None
|
|
|
|
| 65 |
|
| 66 |
+
def download_image(image_url):
|
| 67 |
+
if not image_url:
|
| 68 |
+
return None # Return None if image_url is empty
|
| 69 |
+
try:
|
| 70 |
+
response = requests.get(image_url, stream=True)
|
| 71 |
+
response.raise_for_status()
|
| 72 |
+
|
| 73 |
+
# Create a temporary file
|
| 74 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_file:
|
| 75 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 76 |
+
tmp_file.write(chunk)
|
| 77 |
+
temp_file_path = tmp_file.name
|
| 78 |
+
return temp_file_path
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
+
# Define the Gradio interface
|
| 83 |
interface = gr.Interface(
|
| 84 |
fn=generate_image,
|
| 85 |
inputs=[
|
|
|
|
| 91 |
),
|
| 92 |
gr.Checkbox(label="Realism", value=False), # Checkbox for realism (True/False)
|
| 93 |
],
|
| 94 |
+
outputs=[
|
| 95 |
+
gr.Image(type="pil"), # Output image
|
| 96 |
+
gr.File(label="Download Image", file_types = [".png", ".jpg", ".jpeg"]), # Output File to be downloaded
|
| 97 |
+
],
|
| 98 |
title="Image Generator",
|
| 99 |
description="Provide a prompt, select an aspect ratio, and set realism to generate an image.",
|
| 100 |
)
|