MindCraft24729 commited on
Commit
6fe4884
Β·
verified Β·
1 Parent(s): 23a32f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -21
app.py CHANGED
@@ -6,40 +6,45 @@ 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
- # βœ… Load generated image
28
  img = Image.open(BytesIO(response.content)).convert("RGBA")
29
 
30
- # βœ… Resize watermark to fixed width (e.g., 200px)
31
  wm_width = 200
32
  wm_ratio = wm_width / watermark.width
33
  wm_size = (wm_width, int(watermark.height * wm_ratio))
34
- resized_watermark = watermark.resize(wm_size)
35
 
36
  # βœ… Paste watermark at top-left
37
- img.paste(resized_watermark, (10, 10), resized_watermark)
38
 
39
- # βœ… Convert to RGB and then to JPEG
40
  final_img = img.convert("RGB")
41
-
42
- # βœ… Save in-memory as JPEG (Gradio will return it)
43
  buffer = BytesIO()
44
  final_img.save(buffer, format="JPEG")
45
  buffer.seek(0)
@@ -49,19 +54,16 @@ def generate_image(prompt, model):
49
  except Exception as e:
50
  raise gr.Error(f"Unexpected error: {str(e)}")
51
 
52
-
53
- # βœ… Gradio UI with custom CSS
54
  with gr.Blocks(css="custom_hide_footer.css") as demo:
55
- gr.Markdown("## 🎨 CodeHubb AI Image Generator")
56
 
57
- with gr.Row():
58
- prompt_input = gr.Textbox(label="Prompt", placeholder="e.g., girl in abaya, soft background")
59
- model_input = gr.Dropdown(choices=["kontext", "turbo", "flux"], label="Model", value="turbo")
60
 
61
- generate_btn = gr.Button("Generate")
62
- output_image = gr.Image(label="Generated Image")
63
 
64
- generate_btn.click(generate_image, inputs=[prompt_input, model_input], outputs=output_image)
65
 
66
  demo.queue()
67
  demo.launch()
 
6
  import os
7
  import time
8
 
9
+ # βœ… Set base URL manually (since you're not using secrets)
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" # Default model
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
+ # βœ… Resize watermark to fixed width
38
  wm_width = 200
39
  wm_ratio = wm_width / watermark.width
40
  wm_size = (wm_width, int(watermark.height * wm_ratio))
41
+ resized_wm = watermark.resize(wm_size)
42
 
43
  # βœ… Paste watermark at top-left
44
+ img.paste(resized_wm, (10, 10), resized_wm)
45
 
46
+ # βœ… Convert to RGB JPEG
47
  final_img = img.convert("RGB")
 
 
48
  buffer = BytesIO()
49
  final_img.save(buffer, format="JPEG")
50
  buffer.seek(0)
 
54
  except Exception as e:
55
  raise gr.Error(f"Unexpected error: {str(e)}")
56
 
57
+ # βœ… Gradio Interface
 
58
  with gr.Blocks(css="custom_hide_footer.css") as demo:
59
+ gr.Markdown("## CodeHubb AI Image Generator πŸ–ΌοΈ\nPowered by Flux Model\n*Design. Develop. Dominate.*")
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
+ generate_btn.click(generate_image, inputs=prompt_input, outputs=output_image)
67
 
68
  demo.queue()
69
  demo.launch()