Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,31 +2,25 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import random
|
| 4 |
from io import BytesIO
|
| 5 |
-
from PIL import Image,
|
|
|
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Load watermark logo (CodeHubb logo)
|
| 13 |
-
WATERMARK_PATH = "CodeHubb.png"
|
| 14 |
-
watermark = Image.open(WATERMARK_PATH).convert("RGBA")
|
| 15 |
-
watermark = watermark.resize((150, 40)) # Resize as needed
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# Create a transparent layer and paste watermark
|
| 20 |
-
layer = Image.new("RGBA", image.size, (0, 0, 0, 0))
|
| 21 |
-
layer.paste(watermark, (10, 10)) # Top-left corner with 10px padding
|
| 22 |
-
final_image = Image.alpha_composite(image, layer)
|
| 23 |
-
return final_image.convert("RGB")
|
| 24 |
|
| 25 |
def generate_image(prompt, model):
|
| 26 |
if not prompt:
|
| 27 |
raise gr.Error("Please enter a prompt.")
|
| 28 |
|
|
|
|
| 29 |
seed = random.randint(1, 999999)
|
|
|
|
|
|
|
| 30 |
url = f"{BASE_URL}{prompt}?width=2048&height=2048&seed={seed}&nologo=true&model={model}"
|
| 31 |
|
| 32 |
max_retries = 2
|
|
@@ -34,29 +28,43 @@ def generate_image(prompt, model):
|
|
| 34 |
try:
|
| 35 |
response = requests.get(url, stream=True)
|
| 36 |
response.raise_for_status()
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
except requests.exceptions.HTTPError as e:
|
| 42 |
if response.status_code == 500 and 'Access to kontext model' in response.text:
|
| 43 |
if attempt < max_retries - 1:
|
| 44 |
time.sleep(1)
|
| 45 |
continue
|
| 46 |
-
raise gr.Error("Access denied for kontext model
|
| 47 |
else:
|
| 48 |
raise gr.Error(f"Error: {response.status_code} - {response.text}")
|
| 49 |
except Exception as e:
|
| 50 |
raise gr.Error(f"Unexpected error: {str(e)}")
|
| 51 |
|
| 52 |
# Gradio Interface
|
| 53 |
-
with gr.Blocks() as demo:
|
| 54 |
-
gr.Markdown("
|
| 55 |
|
| 56 |
-
prompt_input = gr.Textbox(label="Prompt", placeholder="e.g.,
|
| 57 |
model_input = gr.Dropdown(choices=["kontext", "turbo", "flux"], label="Model", value="turbo")
|
| 58 |
|
| 59 |
-
generate_btn = gr.Button("Generate")
|
| 60 |
output_image = gr.Image(label="Generated Image")
|
| 61 |
|
| 62 |
generate_btn.click(generate_image, inputs=[prompt_input, model_input], outputs=output_image)
|
|
|
|
| 2 |
import requests
|
| 3 |
import random
|
| 4 |
from io import BytesIO
|
| 5 |
+
from PIL import Image, ImageEnhance
|
| 6 |
+
|
| 7 |
import os
|
| 8 |
import time
|
| 9 |
|
| 10 |
+
# واٹر مارک کا فائل نام
|
| 11 |
+
WATERMARK_PATH = "CodeHubb_50_opacity.png" # اسی نام سے اپلوڈ کریں Hugging Face پر
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Base URL from HF secret or fallback for local
|
| 14 |
+
BASE_URL = os.environ.get('POLLINATIONS_URL')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Construct Pollinations URL
|
| 24 |
url = f"{BASE_URL}{prompt}?width=2048&height=2048&seed={seed}&nologo=true&model={model}"
|
| 25 |
|
| 26 |
max_retries = 2
|
|
|
|
| 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 |
+
# Load watermark
|
| 36 |
+
watermark = Image.open(WATERMARK_PATH).convert("RGBA")
|
| 37 |
+
|
| 38 |
+
# Resize watermark to 15% width of image
|
| 39 |
+
scale_factor = 0.15
|
| 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 |
+
# Paste watermark in top-left corner
|
| 45 |
+
img.paste(watermark, (10, 10), watermark)
|
| 46 |
+
|
| 47 |
+
return img
|
| 48 |
|
| 49 |
except requests.exceptions.HTTPError as e:
|
| 50 |
if response.status_code == 500 and 'Access to kontext model' in response.text:
|
| 51 |
if attempt < max_retries - 1:
|
| 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 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="e.g., futuristic car at sunset")
|
| 65 |
model_input = gr.Dropdown(choices=["kontext", "turbo", "flux"], label="Model", value="turbo")
|
| 66 |
|
| 67 |
+
generate_btn = gr.Button("Generate Image")
|
| 68 |
output_image = gr.Image(label="Generated Image")
|
| 69 |
|
| 70 |
generate_btn.click(generate_image, inputs=[prompt_input, model_input], outputs=output_image)
|