Update app.py
Browse files
app.py
CHANGED
@@ -15,32 +15,43 @@ WATERMARK_TEXT = "SelamGPT"
|
|
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 |
-
|
27 |
try:
|
28 |
-
font = ImageFont.truetype(
|
29 |
except:
|
30 |
-
font = ImageFont.load_default(
|
31 |
|
|
|
32 |
text_width = draw.textlength(WATERMARK_TEXT, font=font)
|
33 |
-
x = image.width - text_width -
|
34 |
-
y = image.height -
|
35 |
|
36 |
-
|
37 |
-
draw.text((x, y), WATERMARK_TEXT, font=font, fill=
|
|
|
38 |
|
39 |
-
|
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,17 +61,29 @@ def generate_image(prompt):
|
|
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":
|
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,37 +110,41 @@ def generate_image(prompt):
|
|
87 |
|
88 |
return None, "⚠️ Failed after multiple attempts. Please try later."
|
89 |
|
90 |
-
# ===== GRADIO THEME =====
|
91 |
-
theme = gr.themes.Default(
|
92 |
-
primary_hue="emerald",
|
93 |
-
secondary_hue="amber",
|
94 |
-
font=[gr.themes.GoogleFont("Poppins"), "Arial", "sans-serif"]
|
95 |
-
)
|
96 |
-
|
97 |
# ===== GRADIO INTERFACE =====
|
98 |
-
with gr.Blocks(theme=theme, title="SelamGPT Image Generator") as demo:
|
|
|
|
|
|
|
99 |
gr.Markdown("""
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
""")
|
103 |
|
104 |
with gr.Row():
|
105 |
with gr.Column(scale=3):
|
106 |
prompt_input = gr.Textbox(
|
107 |
-
label="Describe your image",
|
108 |
-
placeholder="
|
109 |
-
lines=3
|
110 |
-
max_lines=5
|
111 |
)
|
112 |
with gr.Row():
|
113 |
-
generate_btn = gr.Button("Generate
|
114 |
clear_btn = gr.Button("Clear")
|
115 |
|
116 |
gr.Examples(
|
117 |
examples=[
|
118 |
-
["
|
119 |
-
["
|
120 |
-
["
|
121 |
],
|
122 |
inputs=prompt_input
|
123 |
)
|
|
|
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 |
+
# Try Ge'ez font first, then default
|
40 |
try:
|
41 |
+
font = ImageFont.truetype(GEEZ_FONT_PATH, 30)
|
42 |
except:
|
43 |
+
font = ImageFont.load_default(30)
|
44 |
|
45 |
+
# Calculate position
|
46 |
text_width = draw.textlength(WATERMARK_TEXT, font=font)
|
47 |
+
x = image.width - text_width - 20
|
48 |
+
y = image.height - 40
|
49 |
|
50 |
+
# Draw with Ethiopian flag colors (green outline, yellow fill)
|
51 |
+
draw.text((x+2, y+2), WATERMARK_TEXT, font=font, fill="#078C03") # Green shadow
|
52 |
+
draw.text((x, y), WATERMARK_TEXT, font=font, fill="#FCDD09") # Yellow text
|
53 |
|
54 |
+
return image
|
|
|
|
|
|
|
|
|
55 |
except Exception as e:
|
56 |
print(f"Watermark error: {str(e)}")
|
57 |
return Image.open(io.BytesIO(image_bytes))
|
|
|
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": enhanced_prompt,
|
86 |
+
"parameters": params,
|
|
|
|
|
|
|
|
|
87 |
"options": {"wait_for_model": True}
|
88 |
},
|
89 |
timeout=TIMEOUT
|
|
|
110 |
|
111 |
return None, "⚠️ Failed after multiple attempts. Please try later."
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
# ===== GRADIO INTERFACE =====
|
114 |
+
with gr.Blocks(theme=theme, title="SelamGPT - Ethiopian Image Generator") as demo:
|
115 |
+
with gr.Row():
|
116 |
+
gr.Image("ethiopia_flag.png", height=80, show_label=False, show_download_button=False) # Add your flag image
|
117 |
+
|
118 |
gr.Markdown("""
|
119 |
+
<center>
|
120 |
+
<h1 style="color:#078C03">S</h1>
|
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 (English or Amharic)",
|
136 |
+
placeholder="ሰላም! Describe an Ethiopian scene...",
|
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 |
+
["አዲስ አበባ በሌሊት ፣ የኢትዮጵያ ባህላዊ መብራቶች", "Addis Ababa at night with Ethiopian lights"],
|
146 |
+
["Habesha cultural dress with intricate embroidery"],
|
147 |
+
["Simien Mountains landscape with gelada monkeys"]
|
148 |
],
|
149 |
inputs=prompt_input
|
150 |
)
|