Update app.py
Browse files
app.py
CHANGED
@@ -1,151 +1,132 @@
|
|
1 |
-
import os
|
2 |
-
import requests
|
3 |
import gradio as gr
|
|
|
|
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
5 |
import io
|
6 |
-
|
7 |
-
from
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
WATERMARK_TEXT = "SelamGPT"
|
15 |
-
MAX_RETRIES = 3
|
16 |
-
TIMEOUT = 60
|
17 |
-
EXECUTOR = ThreadPoolExecutor(max_workers=2)
|
18 |
|
19 |
# ===== WATERMARK FUNCTION =====
|
20 |
-
def add_watermark(
|
21 |
-
|
|
|
22 |
try:
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
print(f"Watermark error: {str(e)}")
|
46 |
-
return Image.open(io.BytesIO(image_bytes))
|
47 |
-
|
48 |
-
# ===== IMAGE GENERATION =====
|
49 |
-
def generate_image(prompt):
|
50 |
if not prompt.strip():
|
51 |
return None, "⚠️ Please enter a prompt"
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
print(f"Model loading, waiting {wait_time}s...")
|
79 |
-
time.sleep(wait_time)
|
80 |
-
continue
|
81 |
-
else:
|
82 |
-
return None, f"⚠️ API Error: {response.text[:200]}"
|
83 |
-
except requests.Timeout:
|
84 |
-
return None, f"⚠️ Timeout: Model took >{TIMEOUT}s to respond"
|
85 |
-
except Exception as e:
|
86 |
-
return None, f"⚠️ Unexpected error: {str(e)[:200]}"
|
87 |
-
|
88 |
-
return None, "⚠️ Failed after multiple attempts. Please try later."
|
89 |
-
|
90 |
-
# ===== GRADIO THEME =====
|
91 |
theme = gr.themes.Default(
|
92 |
-
primary_hue="
|
93 |
secondary_hue="amber",
|
94 |
-
font=[gr.themes.GoogleFont("Poppins"), "
|
95 |
)
|
96 |
|
97 |
-
#
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
max_lines=5
|
111 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
with gr.Row():
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
gr.
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
height
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
generate_btn.click(
|
138 |
-
fn=generate_image,
|
139 |
-
inputs=prompt_input,
|
140 |
-
outputs=[output_image, status_output],
|
141 |
-
queue=True
|
142 |
-
)
|
143 |
-
|
144 |
-
clear_btn.click(
|
145 |
-
fn=lambda: [None, ""],
|
146 |
-
outputs=[output_image, status_output]
|
147 |
-
)
|
148 |
|
149 |
if __name__ == "__main__":
|
150 |
-
demo.
|
151 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
5 |
import io
|
6 |
+
|
7 |
+
from diffusers import DiffusionPipeline
|
8 |
+
import torch
|
9 |
+
|
10 |
+
# ===== CONFIG =====
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
13 |
+
model_repo_id = "stabilityai/sdxl-turbo"
|
14 |
+
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype, variant="fp16" if device == "cuda" else None)
|
15 |
+
pipe.to(device)
|
16 |
+
|
17 |
+
MAX_SEED = np.iinfo(np.int32).max
|
18 |
+
MAX_IMAGE_SIZE = 1024
|
19 |
WATERMARK_TEXT = "SelamGPT"
|
|
|
|
|
|
|
20 |
|
21 |
# ===== WATERMARK FUNCTION =====
|
22 |
+
def add_watermark(image):
|
23 |
+
draw = ImageDraw.Draw(image)
|
24 |
+
font_size = 24
|
25 |
try:
|
26 |
+
font = ImageFont.truetype("Roboto-Bold.ttf", font_size)
|
27 |
+
except:
|
28 |
+
font = ImageFont.load_default()
|
29 |
+
text_width = draw.textlength(WATERMARK_TEXT, font=font)
|
30 |
+
x = image.width - text_width - 10
|
31 |
+
y = image.height - 34
|
32 |
+
draw.text((x+1, y+1), WATERMARK_TEXT, font=font, fill=(0, 0, 0, 128))
|
33 |
+
draw.text((x, y), WATERMARK_TEXT, font=font, fill=(255, 255, 255))
|
34 |
+
return image
|
35 |
+
|
36 |
+
# ===== IMAGE GENERATION FUNCTION =====
|
37 |
+
def generate(
|
38 |
+
prompt,
|
39 |
+
negative_prompt,
|
40 |
+
seed,
|
41 |
+
randomize_seed,
|
42 |
+
width,
|
43 |
+
height,
|
44 |
+
guidance_scale,
|
45 |
+
num_inference_steps,
|
46 |
+
progress=gr.Progress(track_tqdm=True),
|
47 |
+
):
|
|
|
|
|
|
|
|
|
|
|
48 |
if not prompt.strip():
|
49 |
return None, "⚠️ Please enter a prompt"
|
50 |
+
|
51 |
+
if randomize_seed:
|
52 |
+
seed = random.randint(0, MAX_SEED)
|
53 |
+
|
54 |
+
generator = torch.manual_seed(seed)
|
55 |
+
result = pipe(
|
56 |
+
prompt=prompt,
|
57 |
+
negative_prompt=negative_prompt,
|
58 |
+
width=width,
|
59 |
+
height=height,
|
60 |
+
guidance_scale=guidance_scale,
|
61 |
+
num_inference_steps=num_inference_steps,
|
62 |
+
generator=generator,
|
63 |
+
).images[0]
|
64 |
+
|
65 |
+
image = add_watermark(result)
|
66 |
+
return image, seed
|
67 |
+
|
68 |
+
# ===== EXAMPLES =====
|
69 |
+
examples = [
|
70 |
+
"A futuristic Ethiopian city with flying cars",
|
71 |
+
"An ancient Aksumite queen in a high-tech palace, digital painting",
|
72 |
+
"A cyberpunk Habesha coffee ceremony on Mars",
|
73 |
+
]
|
74 |
+
|
75 |
+
# ===== INTERFACE =====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
theme = gr.themes.Default(
|
77 |
+
primary_hue="cyan",
|
78 |
secondary_hue="amber",
|
79 |
+
font=[gr.themes.GoogleFont("Poppins"), "sans-serif"]
|
80 |
)
|
81 |
|
82 |
+
css = "#container { max-width: 800px; margin: 0 auto; }"
|
83 |
+
|
84 |
+
with gr.Blocks(css=css, theme=theme, title="SelamGPT Turbo Image Generator") as demo:
|
85 |
+
with gr.Column(elem_id="container"):
|
86 |
+
gr.Markdown("# 🎨 SelamGPT Turbo Image Generator\n*Powered by SDXL-Turbo (Fast & Creative)*")
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
prompt = gr.Textbox(
|
90 |
+
label="Prompt",
|
91 |
+
show_label=False,
|
92 |
+
placeholder="Describe the image...",
|
93 |
+
lines=2,
|
94 |
+
scale=3
|
|
|
95 |
)
|
96 |
+
generate_btn = gr.Button("Generate", variant="primary")
|
97 |
+
|
98 |
+
image_output = gr.Image(label="Generated Image", type="pil", format="png", height=512)
|
99 |
+
seed_output = gr.Textbox(label="Seed Used", interactive=False)
|
100 |
+
|
101 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
102 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Things to avoid (optional)", max_lines=1)
|
103 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
104 |
+
seed = gr.Slider(0, MAX_SEED, label="Seed", step=1, value=0)
|
105 |
+
|
106 |
with gr.Row():
|
107 |
+
width = gr.Slider(256, MAX_IMAGE_SIZE, step=32, label="Width", value=1024)
|
108 |
+
height = gr.Slider(256, MAX_IMAGE_SIZE, step=32, label="Height", value=1024)
|
109 |
+
|
110 |
+
with gr.Row():
|
111 |
+
guidance_scale = gr.Slider(0.0, 10.0, step=0.1, label="Guidance Scale", value=0.0)
|
112 |
+
num_inference_steps = gr.Slider(1, 50, step=1, label="Inference Steps", value=2)
|
113 |
+
|
114 |
+
gr.Examples(examples=examples, inputs=[prompt])
|
115 |
+
|
116 |
+
generate_btn.click(
|
117 |
+
fn=generate,
|
118 |
+
inputs=[
|
119 |
+
prompt,
|
120 |
+
negative_prompt,
|
121 |
+
seed,
|
122 |
+
randomize_seed,
|
123 |
+
width,
|
124 |
+
height,
|
125 |
+
guidance_scale,
|
126 |
+
num_inference_steps
|
127 |
+
],
|
128 |
+
outputs=[image_output, seed_output]
|
129 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
if __name__ == "__main__":
|
132 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|