Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,64 +6,54 @@ from PIL import Image
|
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
|
| 9 |
-
#
|
|
|
|
| 10 |
BASE_URL = "https://image.pollinations.ai/prompt/"
|
| 11 |
|
| 12 |
-
# ✅ Load watermark logo (must be named 'CodeHubb.png' and present in root)
|
| 13 |
-
watermark_path = "CodeHubb.png"
|
| 14 |
-
watermark = Image.open(watermark_path).convert("RGBA")
|
| 15 |
-
|
| 16 |
-
# ✅ Reduce watermark opacity to 50%
|
| 17 |
-
alpha = watermark.split()[3]
|
| 18 |
-
alpha = alpha.point(lambda p: int(p * 0.5))
|
| 19 |
-
watermark.putalpha(alpha)
|
| 20 |
-
|
| 21 |
-
# ✅ Image generation function
|
| 22 |
def generate_image(prompt):
|
| 23 |
if not prompt:
|
| 24 |
raise gr.Error("Please enter a prompt.")
|
| 25 |
|
| 26 |
seed = random.randint(1, 999999)
|
| 27 |
-
model = "flux"
|
| 28 |
-
|
| 29 |
url = f"{BASE_URL}{prompt}?width=1024&height=1024&seed={seed}&nologo=true&model={model}"
|
| 30 |
|
| 31 |
try:
|
| 32 |
response = requests.get(url, stream=True)
|
| 33 |
response.raise_for_status()
|
| 34 |
-
|
| 35 |
img = Image.open(BytesIO(response.content)).convert("RGBA")
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
final_img = img.convert("RGB")
|
| 48 |
-
buffer = BytesIO()
|
| 49 |
-
final_img.save(buffer, format="JPEG")
|
| 50 |
-
buffer.seek(0)
|
| 51 |
-
|
| 52 |
-
return Image.open(buffer)
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
raise gr.Error(f"Unexpected error: {str(e)}")
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
with gr.Blocks(css="
|
| 59 |
-
gr.Markdown("## CodeHubb AI Image Generator
|
| 60 |
-
|
| 61 |
-
prompt_input = gr.Textbox(label="Prompt", placeholder="e.g., A futuristic city at sunset", lines=2)
|
| 62 |
|
|
|
|
| 63 |
generate_btn = gr.Button("Generate Image")
|
| 64 |
-
output_image = gr.Image(label="Generated Image", type="pil")
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
demo.queue()
|
| 69 |
demo.launch()
|
|
|
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
|
| 9 |
+
# Branding
|
| 10 |
+
WATERMARK_PATH = "CodeHubb.png"
|
| 11 |
BASE_URL = "https://image.pollinations.ai/prompt/"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def generate_image(prompt):
|
| 14 |
if not prompt:
|
| 15 |
raise gr.Error("Please enter a prompt.")
|
| 16 |
|
| 17 |
seed = random.randint(1, 999999)
|
| 18 |
+
model = "flux"
|
|
|
|
| 19 |
url = f"{BASE_URL}{prompt}?width=1024&height=1024&seed={seed}&nologo=true&model={model}"
|
| 20 |
|
| 21 |
try:
|
| 22 |
response = requests.get(url, stream=True)
|
| 23 |
response.raise_for_status()
|
|
|
|
| 24 |
img = Image.open(BytesIO(response.content)).convert("RGBA")
|
| 25 |
|
| 26 |
+
# Watermark
|
| 27 |
+
if os.path.exists(WATERMARK_PATH):
|
| 28 |
+
watermark = Image.open(WATERMARK_PATH).convert("RGBA")
|
| 29 |
+
watermark = watermark.resize((150, 50))
|
| 30 |
+
watermark.putalpha(128) # 50% opacity
|
| 31 |
+
img.paste(watermark, (10, 10), watermark)
|
| 32 |
|
| 33 |
+
# Convert to JPG and Save
|
| 34 |
+
output_path = "generated_image.jpg"
|
| 35 |
+
rgb_img = img.convert("RGB")
|
| 36 |
+
rgb_img.save(output_path, "JPEG")
|
| 37 |
|
| 38 |
+
return rgb_img, output_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
raise gr.Error(f"Unexpected error: {str(e)}")
|
| 42 |
|
| 43 |
+
# Gradio UI
|
| 44 |
+
with gr.Blocks(css=".footer {display: none !important;}") as demo:
|
| 45 |
+
gr.Markdown("## CodeHubb AI Image Generator 🧠\n**Design. Develop. Dominate.**")
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
prompt_input = gr.Textbox(label="Enter your prompt", placeholder="e.g. horse running in desert")
|
| 48 |
generate_btn = gr.Button("Generate Image")
|
|
|
|
| 49 |
|
| 50 |
+
image_output = gr.Image(label="Generated Image")
|
| 51 |
+
download_output = gr.File(label="Download JPG")
|
| 52 |
+
|
| 53 |
+
generate_btn.click(
|
| 54 |
+
fn=generate_image,
|
| 55 |
+
inputs=prompt_input,
|
| 56 |
+
outputs=[image_output, download_output],
|
| 57 |
+
)
|
| 58 |
|
|
|
|
| 59 |
demo.launch()
|