Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,50 +6,55 @@ from PIL import Image
|
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
BASE_URL = os.environ.get('POLLINATIONS_URL'
|
| 11 |
|
|
|
|
| 12 |
def generate_image(prompt, model):
|
| 13 |
if not prompt:
|
| 14 |
-
raise gr.Error("Please enter a prompt.")
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
seed = random.randint(1, 999999)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
url = f"{BASE_URL}{prompt}?width=2048&height=2048&seed={seed}&nologo=true&model={model}"
|
| 21 |
|
| 22 |
-
max_retries = 2
|
| 23 |
for attempt in range(max_retries):
|
| 24 |
try:
|
| 25 |
response = requests.get(url, stream=True)
|
| 26 |
-
response.raise_for_status()
|
| 27 |
|
| 28 |
-
# Convert to PIL Image
|
| 29 |
img = Image.open(BytesIO(response.content))
|
| 30 |
return img
|
| 31 |
|
| 32 |
except requests.exceptions.HTTPError as e:
|
| 33 |
if response.status_code == 500 and 'Access to kontext model' in response.text:
|
| 34 |
if attempt < max_retries - 1:
|
| 35 |
-
time.sleep(1)
|
| 36 |
continue
|
| 37 |
-
raise gr.Error("Access denied
|
| 38 |
else:
|
| 39 |
-
raise gr.Error(f"Error: {response.status_code}
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
-
raise gr.Error(f"Unexpected error: {str(e)}")
|
| 42 |
|
| 43 |
-
# Gradio Interface
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
-
gr.Markdown("#
|
| 46 |
|
| 47 |
-
prompt_input = gr.Textbox(label="Prompt", placeholder="e.g.,
|
| 48 |
-
model_input = gr.Dropdown(choices=["kontext", "turbo", "flux"], label="Model", value="turbo")
|
| 49 |
|
| 50 |
-
generate_btn = gr.Button("Generate")
|
| 51 |
-
output_image = gr.Image(label="Generated Image")
|
| 52 |
|
| 53 |
generate_btn.click(generate_image, inputs=[prompt_input, model_input], outputs=output_image)
|
| 54 |
-
|
| 55 |
-
demo.
|
|
|
|
|
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
|
| 9 |
+
# ✅ Base URL - use secret or fallback for testing
|
| 10 |
+
BASE_URL = os.environ.get('POLLINATIONS_URL', 'https://image.pollinations.ai/prompt/')
|
| 11 |
|
| 12 |
+
# ✅ Image Generation Function
|
| 13 |
def generate_image(prompt, model):
|
| 14 |
if not prompt:
|
| 15 |
+
raise gr.Error("⚠️ Please enter a prompt.")
|
| 16 |
|
| 17 |
+
if not BASE_URL:
|
| 18 |
+
raise gr.Error("⚠️ Base URL not found. Check your environment variable.")
|
| 19 |
+
|
| 20 |
+
# Random seed for uniqueness
|
| 21 |
seed = random.randint(1, 999999)
|
| 22 |
|
| 23 |
+
# Full image generation URL
|
| 24 |
url = f"{BASE_URL}{prompt}?width=2048&height=2048&seed={seed}&nologo=true&model={model}"
|
| 25 |
|
| 26 |
+
max_retries = 2
|
| 27 |
for attempt in range(max_retries):
|
| 28 |
try:
|
| 29 |
response = requests.get(url, stream=True)
|
| 30 |
+
response.raise_for_status()
|
| 31 |
|
|
|
|
| 32 |
img = Image.open(BytesIO(response.content))
|
| 33 |
return img
|
| 34 |
|
| 35 |
except requests.exceptions.HTTPError as e:
|
| 36 |
if response.status_code == 500 and 'Access to kontext model' in response.text:
|
| 37 |
if attempt < max_retries - 1:
|
| 38 |
+
time.sleep(1)
|
| 39 |
continue
|
| 40 |
+
raise gr.Error("❌ Access denied to 'kontext' model. Try 'turbo' or 'flux'.")
|
| 41 |
else:
|
| 42 |
+
raise gr.Error(f"❌ HTTP Error: {response.status_code}\n{response.text}")
|
| 43 |
+
|
| 44 |
except Exception as e:
|
| 45 |
+
raise gr.Error(f"❌ Unexpected error: {str(e)}")
|
| 46 |
|
| 47 |
+
# ✅ Gradio Interface
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("# 🎨 AI Image Generator with Pollinations")
|
| 50 |
|
| 51 |
+
prompt_input = gr.Textbox(label="🎯 Prompt", placeholder="e.g., A scenic mountain with sunset")
|
| 52 |
+
model_input = gr.Dropdown(choices=["kontext", "turbo", "flux"], label="🧠 AI Model", value="turbo")
|
| 53 |
|
| 54 |
+
generate_btn = gr.Button("🚀 Generate Image")
|
| 55 |
+
output_image = gr.Image(label="🖼️ Generated Image")
|
| 56 |
|
| 57 |
generate_btn.click(generate_image, inputs=[prompt_input, model_input], outputs=output_image)
|
| 58 |
+
|
| 59 |
+
demo.queue()
|
| 60 |
+
demo.launch()
|