Update app.py
Browse files
app.py
CHANGED
@@ -15,43 +15,32 @@ WATERMARK_TEXT = "SelamGPT"
|
|
15 |
MAX_RETRIES = 3
|
16 |
TIMEOUT = 60
|
17 |
EXECUTOR = ThreadPoolExecutor(max_workers=2)
|
18 |
-
GEEZ_FONT_PATH = "fonts/NotoSansEthiopic-Bold.ttf"
|
19 |
-
|
20 |
-
# ===== AUTHENTIC ETHIOPIAN THEME =====
|
21 |
-
theme = gr.themes.Default(
|
22 |
-
primary_hue="green",
|
23 |
-
secondary_hue="yellow",
|
24 |
-
neutral_hue="red",
|
25 |
-
font=[gr.themes.GoogleFont("Noto Sans Ethiopic"), "Arial", "sans-serif"]
|
26 |
-
).set(
|
27 |
-
button_primary_background_fill="linear-gradient(90deg, #078C03 0%, #FCDD09 50%, #DA1212 100%)", # Ethiopian flag gradient
|
28 |
-
button_primary_text_color="#ffffff",
|
29 |
-
button_secondary_background_fill="#FCDD09", # Yellow
|
30 |
-
slider_color="#DA1212" # Red
|
31 |
-
)
|
32 |
|
33 |
# ===== WATERMARK FUNCTION =====
|
34 |
def add_watermark(image_bytes):
|
|
|
35 |
try:
|
36 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
37 |
draw = ImageDraw.Draw(image)
|
38 |
|
39 |
-
|
40 |
try:
|
41 |
-
font = ImageFont.truetype(
|
42 |
except:
|
43 |
-
font = ImageFont.load_default(
|
44 |
|
45 |
-
# Calculate position
|
46 |
text_width = draw.textlength(WATERMARK_TEXT, font=font)
|
47 |
-
x = image.width - text_width -
|
48 |
-
y = image.height -
|
49 |
|
50 |
-
|
51 |
-
draw.text((x
|
52 |
-
draw.text((x, y), WATERMARK_TEXT, font=font, fill="#FCDD09") # Yellow text
|
53 |
|
54 |
-
|
|
|
|
|
|
|
|
|
55 |
except Exception as e:
|
56 |
print(f"Watermark error: {str(e)}")
|
57 |
return Image.open(io.BytesIO(image_bytes))
|
@@ -61,29 +50,17 @@ def generate_image(prompt):
|
|
61 |
if not prompt.strip():
|
62 |
return None, "⚠️ Please enter a prompt"
|
63 |
|
64 |
-
# Ethiopian cultural enhancement
|
65 |
-
cultural_enhancements = [
|
66 |
-
"Ethiopian style", "Habesha culture", "vibrant colors",
|
67 |
-
"traditional elements", "East African aesthetic"
|
68 |
-
]
|
69 |
-
|
70 |
-
enhanced_prompt = f"{prompt}, {', '.join(cultural_enhancements)}"
|
71 |
-
|
72 |
-
params = {
|
73 |
-
"height": 1024,
|
74 |
-
"width": 1024,
|
75 |
-
"num_inference_steps": 30,
|
76 |
-
"guidance_scale": 8.0,
|
77 |
-
"negative_prompt": "western, non-African, cartoonish, low quality"
|
78 |
-
}
|
79 |
-
|
80 |
def api_call():
|
81 |
return requests.post(
|
82 |
API_URL,
|
83 |
headers=headers,
|
84 |
json={
|
85 |
-
"inputs":
|
86 |
-
"parameters":
|
|
|
|
|
|
|
|
|
87 |
"options": {"wait_for_model": True}
|
88 |
},
|
89 |
timeout=TIMEOUT
|
@@ -110,41 +87,45 @@ def generate_image(prompt):
|
|
110 |
|
111 |
return None, "⚠️ Failed after multiple attempts. Please try later."
|
112 |
|
113 |
-
# ===== GRADIO
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
gr.Markdown("""
|
119 |
-
|
120 |
-
|
121 |
-
<h1 style="color:#FCDD09">E</h1>
|
122 |
-
<h1 style="color:#DA1212">L</h1>
|
123 |
-
<h1 style="color:#078C03">A</h1>
|
124 |
-
<h1 style="color:#FCDD09">M</h1>
|
125 |
-
<h1 style="color:#DA1212">G</h1>
|
126 |
-
<h1 style="color:#078C03">P</h1>
|
127 |
-
<h1 style="color:#FCDD09">T</h1>
|
128 |
-
</center>
|
129 |
-
<center><i>Ethiopian AI Image Generator</i></center>
|
130 |
""")
|
131 |
|
132 |
with gr.Row():
|
133 |
with gr.Column(scale=3):
|
134 |
prompt_input = gr.Textbox(
|
135 |
-
label="Describe your image
|
136 |
-
placeholder="
|
137 |
-
lines=3
|
|
|
138 |
)
|
139 |
with gr.Row():
|
140 |
-
generate_btn = gr.Button("Generate", variant="primary")
|
141 |
clear_btn = gr.Button("Clear")
|
142 |
|
143 |
gr.Examples(
|
144 |
examples=[
|
145 |
-
["
|
146 |
-
["
|
147 |
-
["
|
148 |
],
|
149 |
inputs=prompt_input
|
150 |
)
|
|
|
15 |
MAX_RETRIES = 3
|
16 |
TIMEOUT = 60
|
17 |
EXECUTOR = ThreadPoolExecutor(max_workers=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# ===== WATERMARK FUNCTION =====
|
20 |
def add_watermark(image_bytes):
|
21 |
+
"""Add watermark with optimized PNG output"""
|
22 |
try:
|
23 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
24 |
draw = ImageDraw.Draw(image)
|
25 |
|
26 |
+
font_size = 24
|
27 |
try:
|
28 |
+
font = ImageFont.truetype("Roboto-Bold.ttf", font_size)
|
29 |
except:
|
30 |
+
font = ImageFont.load_default(font_size)
|
31 |
|
|
|
32 |
text_width = draw.textlength(WATERMARK_TEXT, font=font)
|
33 |
+
x = image.width - text_width - 10
|
34 |
+
y = image.height - 34
|
35 |
|
36 |
+
draw.text((x+1, y+1), WATERMARK_TEXT, font=font, fill=(0, 0, 0, 128))
|
37 |
+
draw.text((x, y), WATERMARK_TEXT, font=font, fill=(255, 255, 255))
|
|
|
38 |
|
39 |
+
# Convert to optimized PNG
|
40 |
+
img_byte_arr = io.BytesIO()
|
41 |
+
image.save(img_byte_arr, format='PNG', optimize=True, quality=85)
|
42 |
+
img_byte_arr.seek(0)
|
43 |
+
return Image.open(img_byte_arr)
|
44 |
except Exception as e:
|
45 |
print(f"Watermark error: {str(e)}")
|
46 |
return Image.open(io.BytesIO(image_bytes))
|
|
|
50 |
if not prompt.strip():
|
51 |
return None, "⚠️ Please enter a prompt"
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
def api_call():
|
54 |
return requests.post(
|
55 |
API_URL,
|
56 |
headers=headers,
|
57 |
json={
|
58 |
+
"inputs": prompt,
|
59 |
+
"parameters": {
|
60 |
+
"height": 1024,
|
61 |
+
"width": 1024,
|
62 |
+
"num_inference_steps": 30
|
63 |
+
},
|
64 |
"options": {"wait_for_model": True}
|
65 |
},
|
66 |
timeout=TIMEOUT
|
|
|
87 |
|
88 |
return None, "⚠️ Failed after multiple attempts. Please try later."
|
89 |
|
90 |
+
# ===== GRADIO THEME ===== (add this right before the interface section)
|
91 |
+
theme = gr.themes.Default(
|
92 |
+
primary_hue="green",
|
93 |
+
secondary_hue="yellow",
|
94 |
+
neutral_hue="red",
|
95 |
+
font=[gr.themes.GoogleFont("Poppins"), "Arial", "sans-serif"]
|
96 |
+
).set(
|
97 |
+
button_primary_background_fill="#078C03", # Ethiopian green
|
98 |
+
button_primary_text_color="#FCDD09", # Ethiopian yellow
|
99 |
+
button_secondary_background_fill="#DA1212", # Ethiopian red
|
100 |
+
slider_color="#DA1212", # Red for sliders
|
101 |
+
checkbox_label_background_fill="#FCDD09", # Yellow for checkboxes
|
102 |
+
block_background_fill_dark="#1e1e2e" # Dark background
|
103 |
+
)
|
104 |
+
|
105 |
+
# ===== GRADIO INTERFACE ===== (keep your existing interface code)
|
106 |
+
with gr.Blocks(theme=theme, title="SelamGPT Image Generator") as demo:
|
107 |
gr.Markdown("""
|
108 |
+
# 🎨 SelamGPT Image Generator
|
109 |
+
*Powered by Stable Diffusion XL (1024x1024 PNG output)*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
""")
|
111 |
|
112 |
with gr.Row():
|
113 |
with gr.Column(scale=3):
|
114 |
prompt_input = gr.Textbox(
|
115 |
+
label="Describe your image",
|
116 |
+
placeholder="A futuristic Ethiopian city with flying cars...",
|
117 |
+
lines=3,
|
118 |
+
max_lines=5
|
119 |
)
|
120 |
with gr.Row():
|
121 |
+
generate_btn = gr.Button("Generate Image", variant="primary")
|
122 |
clear_btn = gr.Button("Clear")
|
123 |
|
124 |
gr.Examples(
|
125 |
examples=[
|
126 |
+
["An ancient Aksumite warrior in cyberpunk armor, 4k detailed"],
|
127 |
+
["Traditional Ethiopian coffee ceremony in zero gravity"],
|
128 |
+
["Portrait of a Habesha queen with golden jewelry"]
|
129 |
],
|
130 |
inputs=prompt_input
|
131 |
)
|