Update app.py
Browse files
app.py
CHANGED
@@ -110,26 +110,55 @@ def search_pixabay(query: str, media_type: str, image_type: str, orientation: st
|
|
110 |
return None, None, f"An unexpected error occurred: {e}"
|
111 |
|
112 |
# --- Together AI Image Generation Functions ---
|
113 |
-
def together_text_to_image(prompt: str):
|
114 |
"""
|
115 |
-
Generates an image from text using the Together AI API.
|
116 |
|
117 |
Args:
|
118 |
-
prompt (str): The text prompt
|
|
|
|
|
|
|
|
|
119 |
|
120 |
Returns:
|
121 |
-
str: The URL of the generated image, or an error message.
|
122 |
"""
|
123 |
if not client:
|
124 |
return "Together AI client not initialized. Please set the TOGETHER_API_KEY environment variable."
|
125 |
if not prompt:
|
126 |
return "Please enter a prompt for text-to-image generation."
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
try:
|
129 |
image_completion = client.images.generate(
|
130 |
-
model="black-forest-labs/FLUX.1.1-pro",
|
131 |
-
width=
|
132 |
-
height=
|
133 |
steps=40,
|
134 |
prompt=prompt,
|
135 |
)
|
@@ -137,6 +166,7 @@ def together_text_to_image(prompt: str):
|
|
137 |
except Exception as e:
|
138 |
return f"Error generating image from text: {e}"
|
139 |
|
|
|
140 |
def image_to_url(image_path):
|
141 |
try:
|
142 |
url = 'https://uguu.se/upload'
|
@@ -244,11 +274,13 @@ with gr.Blocks(title="Media Generation and Search Explorer") as demo:
|
|
244 |
gr.Warning("This requires setting the TOGETHER_API_KEY environment variable.")
|
245 |
with gr.Row():
|
246 |
together_text_to_image_prompt = gr.Textbox(label="Enter your prompt", scale=2)
|
|
|
|
|
247 |
together_text_to_image_button = gr.Button("Generate Image", scale=1)
|
248 |
together_text_to_image_output = gr.Image(label="Generated Image (URL)", type="filepath", interactive=False)
|
249 |
|
250 |
together_text_to_image_button.click(
|
251 |
-
fn=together_text_to_image,
|
252 |
inputs=together_text_to_image_prompt,
|
253 |
outputs=together_text_to_image_output,
|
254 |
)
|
|
|
110 |
return None, None, f"An unexpected error occurred: {e}"
|
111 |
|
112 |
# --- Together AI Image Generation Functions ---
|
113 |
+
def together_text_to_image(prompt: str = "", width: int = 1024, height: int = 1024):
|
114 |
"""
|
115 |
+
Generates an image from a text prompt using the Together AI API and the FLUX.1.1-pro model.
|
116 |
|
117 |
Args:
|
118 |
+
prompt (str): The text prompt to generate the image from. This must be a non-empty string.
|
119 |
+
width (int, optional): The width of the generated image in pixels.
|
120 |
+
Must be between 512 and 1440. Will auto-adjust if out of bounds. Default is 1024.
|
121 |
+
height (int, optional): The height of the generated image in pixels.
|
122 |
+
Must be between 512 and 1440. Will auto-adjust if out of bounds. Default is 1024.
|
123 |
|
124 |
Returns:
|
125 |
+
str: The URL of the generated image if successful, or an error message if not.
|
126 |
"""
|
127 |
if not client:
|
128 |
return "Together AI client not initialized. Please set the TOGETHER_API_KEY environment variable."
|
129 |
if not prompt:
|
130 |
return "Please enter a prompt for text-to-image generation."
|
131 |
|
132 |
+
# Clamp dimensions while preserving aspect ratio
|
133 |
+
min_size, max_size = 512, 1440
|
134 |
+
if width < min_size or width > max_size or height < min_size or height > max_size:
|
135 |
+
aspect_ratio = width / height
|
136 |
+
|
137 |
+
# Adjust based on which dimension is more out of bounds
|
138 |
+
if width < min_size or height < min_size:
|
139 |
+
if width < height:
|
140 |
+
width = min_size
|
141 |
+
height = int(round(width / aspect_ratio))
|
142 |
+
else:
|
143 |
+
height = min_size
|
144 |
+
width = int(round(height * aspect_ratio))
|
145 |
+
elif width > max_size or height > max_size:
|
146 |
+
if width > height:
|
147 |
+
width = max_size
|
148 |
+
height = int(round(width / aspect_ratio))
|
149 |
+
else:
|
150 |
+
height = max_size
|
151 |
+
width = int(round(height * aspect_ratio))
|
152 |
+
|
153 |
+
# Re-clamp just in case rounding pushed a value out of range
|
154 |
+
width = max(min_size, min(width, max_size))
|
155 |
+
height = max(min_size, min(height, max_size))
|
156 |
+
|
157 |
try:
|
158 |
image_completion = client.images.generate(
|
159 |
+
model="black-forest-labs/FLUX.1.1-pro", # Hardcoded model
|
160 |
+
width=width,
|
161 |
+
height=height,
|
162 |
steps=40,
|
163 |
prompt=prompt,
|
164 |
)
|
|
|
166 |
except Exception as e:
|
167 |
return f"Error generating image from text: {e}"
|
168 |
|
169 |
+
|
170 |
def image_to_url(image_path):
|
171 |
try:
|
172 |
url = 'https://uguu.se/upload'
|
|
|
274 |
gr.Warning("This requires setting the TOGETHER_API_KEY environment variable.")
|
275 |
with gr.Row():
|
276 |
together_text_to_image_prompt = gr.Textbox(label="Enter your prompt", scale=2)
|
277 |
+
together_text_to_image_width = gr.Slider("Width", value=1024)
|
278 |
+
together_text_to_image_height = gr.Slider("Height", value=1024)
|
279 |
together_text_to_image_button = gr.Button("Generate Image", scale=1)
|
280 |
together_text_to_image_output = gr.Image(label="Generated Image (URL)", type="filepath", interactive=False)
|
281 |
|
282 |
together_text_to_image_button.click(
|
283 |
+
fn=[together_text_to_image, together_text_to_image_width, together_text_to_image_height],
|
284 |
inputs=together_text_to_image_prompt,
|
285 |
outputs=together_text_to_image_output,
|
286 |
)
|