Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,73 +2,51 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import random
|
| 4 |
from io import BytesIO
|
| 5 |
-
from PIL import Image
|
| 6 |
-
|
| 7 |
import os
|
| 8 |
import time
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
def generate_image(prompt, model):
|
| 17 |
if not prompt:
|
| 18 |
raise gr.Error("Please enter a prompt.")
|
| 19 |
|
| 20 |
-
# Randomize seed
|
| 21 |
seed = random.randint(1, 999999)
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 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 |
-
# Convert to PIL Image
|
| 33 |
-
img = Image.open(BytesIO(response.content)).convert("RGBA")
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
wm_width = int(img.width * scale_factor)
|
| 41 |
-
wm_height = int(watermark.height * wm_width / watermark.width)
|
| 42 |
-
watermark = watermark.resize((wm_width, wm_height), Image.LANCZOS)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
img.paste(watermark, (10, 10), watermark)
|
| 46 |
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
time.sleep(1)
|
| 53 |
-
continue
|
| 54 |
-
raise gr.Error("Access denied for kontext model. Try turbo/flux or authenticate at pollinations.ai.")
|
| 55 |
-
else:
|
| 56 |
-
raise gr.Error(f"Error: {response.status_code} - {response.text}")
|
| 57 |
-
except Exception as e:
|
| 58 |
-
raise gr.Error(f"Unexpected error: {str(e)}")
|
| 59 |
-
|
| 60 |
-
# Gradio Interface
|
| 61 |
-
with gr.Blocks(css=".footer {display: none !important}") as demo:
|
| 62 |
-
gr.Markdown("## CodeHubb AI Image Generator\n**Design. Develop. Dominate.**")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
-
generate_btn = gr.Button("Generate
|
| 68 |
output_image = gr.Image(label="Generated Image")
|
| 69 |
|
| 70 |
generate_btn.click(generate_image, inputs=[prompt_input, model_input], outputs=output_image)
|
| 71 |
|
| 72 |
demo.queue()
|
| 73 |
demo.launch()
|
| 74 |
-
with gr.Blocks(css="custom_hide_footer.css") as demo:
|
|
|
|
| 2 |
import requests
|
| 3 |
import random
|
| 4 |
from io import BytesIO
|
| 5 |
+
from PIL import Image
|
|
|
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
|
| 9 |
+
# ✅ TEMP BASE_URL (later move to environment variable)
|
| 10 |
+
BASE_URL = "https://image.pollinations.ai/prompt/"
|
| 11 |
|
| 12 |
+
# ✅ Load watermark image (make sure it's uploaded as 'CodeHubb_50_opacity.png')
|
| 13 |
+
WATERMARK_PATH = "CodeHubb.png"
|
| 14 |
+
watermark = Image.open(WATERMARK_PATH).convert("RGBA")
|
| 15 |
|
| 16 |
def generate_image(prompt, model):
|
| 17 |
if not prompt:
|
| 18 |
raise gr.Error("Please enter a prompt.")
|
| 19 |
|
|
|
|
| 20 |
seed = random.randint(1, 999999)
|
| 21 |
+
url = f"{BASE_URL}{prompt}?width=1024&height=1024&seed={seed}&nologo=true&model={model}"
|
| 22 |
|
| 23 |
+
try:
|
| 24 |
+
response = requests.get(url, stream=True)
|
| 25 |
+
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Open main image
|
| 28 |
+
img = Image.open(BytesIO(response.content)).convert("RGBA")
|
| 29 |
|
| 30 |
+
# Paste watermark
|
| 31 |
+
img.paste(watermark, (10, 10), mask=watermark)
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
return img.convert("RGB")
|
|
|
|
| 34 |
|
| 35 |
+
except Exception as e:
|
| 36 |
+
raise gr.Error(f"Unexpected error: {str(e)}")
|
| 37 |
|
| 38 |
+
# ✅ Gradio UI with custom CSS
|
| 39 |
+
with gr.Blocks(css="custom_hide_footer.css") as demo:
|
| 40 |
+
gr.Markdown("## 🎨 CodeHubb AI Image Generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
with gr.Row():
|
| 43 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="e.g., girl in abaya, soft background")
|
| 44 |
+
model_input = gr.Dropdown(choices=["kontext", "turbo", "flux"], label="Model", value="turbo")
|
| 45 |
|
| 46 |
+
generate_btn = gr.Button("Generate")
|
| 47 |
output_image = gr.Image(label="Generated Image")
|
| 48 |
|
| 49 |
generate_btn.click(generate_image, inputs=[prompt_input, model_input], outputs=output_image)
|
| 50 |
|
| 51 |
demo.queue()
|
| 52 |
demo.launch()
|
|
|