MindCraft24729 commited on
Commit
8f91df0
·
verified ·
1 Parent(s): 6fe4884

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -36
app.py CHANGED
@@ -6,64 +6,54 @@ from PIL import Image
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)
51
-
52
- return Image.open(buffer)
53
 
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()
 
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()