Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,56 +4,52 @@ import random
|
|
| 4 |
from io import BytesIO
|
| 5 |
from PIL import Image
|
| 6 |
import os
|
| 7 |
-
import time
|
| 8 |
|
| 9 |
-
#
|
| 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 |
-
|
| 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 |
-
#
|
| 27 |
-
|
| 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
|
| 34 |
-
output_path = "generated_image.jpg"
|
| 35 |
rgb_img = img.convert("RGB")
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
return
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
-
raise gr.Error(f"Unexpected error: {str(e)}")
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
with gr.Blocks(css=".
|
| 45 |
-
gr.Markdown("## CodeHubb
|
| 46 |
|
| 47 |
-
|
| 48 |
-
generate_btn = gr.Button("Generate Image")
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
generate_btn.click(
|
| 54 |
-
fn=generate_image,
|
| 55 |
-
inputs=prompt_input,
|
| 56 |
-
outputs=[image_output, download_output],
|
| 57 |
-
)
|
| 58 |
|
| 59 |
demo.launch()
|
|
|
|
| 4 |
from io import BytesIO
|
| 5 |
from PIL import Image
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
+
# Set base URL and watermark path
|
|
|
|
| 9 |
BASE_URL = "https://image.pollinations.ai/prompt/"
|
| 10 |
+
WATERMARK_PATH = "CodeHubb.png" # Ensure this is uploaded in the same directory
|
| 11 |
|
| 12 |
+
# Load watermark logo
|
| 13 |
+
watermark = Image.open(WATERMARK_PATH).convert("RGBA")
|
| 14 |
+
watermark = watermark.resize((300, 100))
|
| 15 |
+
watermark.putalpha(128) # 50% opacity
|
| 16 |
+
|
| 17 |
+
# Function to generate image
|
| 18 |
def generate_image(prompt):
|
| 19 |
if not prompt:
|
| 20 |
raise gr.Error("Please enter a prompt.")
|
| 21 |
|
| 22 |
seed = random.randint(1, 999999)
|
| 23 |
+
url = f"{BASE_URL}{prompt}?width=2048&height=2048&seed={seed}&nologo=true&model=flux"
|
|
|
|
| 24 |
|
| 25 |
try:
|
| 26 |
response = requests.get(url, stream=True)
|
| 27 |
response.raise_for_status()
|
| 28 |
img = Image.open(BytesIO(response.content)).convert("RGBA")
|
| 29 |
|
| 30 |
+
# Paste watermark in top-left corner
|
| 31 |
+
img.paste(watermark, (20, 20), watermark)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Convert to JPG
|
|
|
|
| 34 |
rgb_img = img.convert("RGB")
|
| 35 |
+
download_path = "/mnt/data/generated_image.jpg"
|
| 36 |
+
rgb_img.save(download_path, "JPEG", quality=95)
|
| 37 |
|
| 38 |
+
return img, download_path
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
+
raise gr.Error(f"❌ Unexpected error: {str(e)}")
|
| 42 |
|
| 43 |
+
# UI Layout
|
| 44 |
+
with gr.Blocks(css=".gr-button-lg {width: 100% !important;}") as demo:
|
| 45 |
+
gr.Markdown("## 🚀 CodeHubb Image Generator\nYour Creative Partner – **Design. Develop. Dominate**")
|
| 46 |
|
| 47 |
+
prompt = gr.Textbox(label="Enter your creative prompt:", placeholder="e.g. cinematic Pakistani village at sunset", lines=2)
|
|
|
|
| 48 |
|
| 49 |
+
generate_btn = gr.Button("🎨 Generate Image", elem_classes="gr-button-lg")
|
| 50 |
+
image_output = gr.Image(label="Generated Image", type="pil")
|
| 51 |
+
download_button = gr.File(label="⬇️ Download as JPG", elem_classes="gr-button-lg")
|
| 52 |
|
| 53 |
+
generate_btn.click(fn=generate_image, inputs=[prompt], outputs=[image_output, download_button])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
demo.launch()
|