Update app.py
Browse files
app.py
CHANGED
@@ -13,12 +13,11 @@ API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
|
13 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
14 |
WATERMARK_TEXT = "SelamGPT"
|
15 |
MAX_RETRIES = 3
|
16 |
-
TIMEOUT = 45
|
17 |
-
EXECUTOR = ThreadPoolExecutor(max_workers=3)
|
18 |
|
19 |
-
# ===== OPTIMIZED WATERMARK FUNCTION
|
20 |
def add_watermark(image_bytes):
|
21 |
-
"""Add watermark with optimized WebP output (85% quality)"""
|
22 |
try:
|
23 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
24 |
draw = ImageDraw.Draw(image)
|
@@ -36,27 +35,25 @@ def add_watermark(image_bytes):
|
|
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 WebP (faster + smaller)
|
40 |
webp_buffer = io.BytesIO()
|
41 |
-
image.save(webp_buffer, format="WEBP", quality=85
|
42 |
webp_buffer.seek(0)
|
43 |
return Image.open(webp_buffer)
|
44 |
except Exception as e:
|
45 |
print(f"Watermark error: {str(e)}")
|
46 |
return Image.open(io.BytesIO(image_bytes))
|
47 |
|
48 |
-
# =====
|
49 |
def generate_image(prompt):
|
50 |
if not prompt.strip():
|
51 |
return None, "⚠️ Please enter a prompt"
|
52 |
|
53 |
-
# Faster generation parameters
|
54 |
params = {
|
55 |
-
"height": 768,
|
56 |
"width": 768,
|
57 |
-
"num_inference_steps": 20,
|
58 |
-
"guidance_scale": 7.0,
|
59 |
-
"options": {"wait_for_model": False}
|
60 |
}
|
61 |
|
62 |
def api_call():
|
@@ -79,8 +76,7 @@ def generate_image(prompt):
|
|
79 |
gen_time = time.time() - start_time
|
80 |
return add_watermark(response.content), f"✔️ Generated in {gen_time:.1f}s"
|
81 |
elif response.status_code == 503:
|
82 |
-
|
83 |
-
time.sleep(5 * (attempt + 1)) # Progressive backoff
|
84 |
continue
|
85 |
else:
|
86 |
return None, f"⚠️ API Error: {response.text[:200]}"
|
@@ -102,7 +98,7 @@ with gr.Blocks(title="SelamGPT Image Generator") as demo:
|
|
102 |
with gr.Column(scale=3):
|
103 |
prompt_input = gr.Textbox(
|
104 |
label="Describe your image",
|
105 |
-
placeholder="A futuristic Ethiopian city
|
106 |
lines=3
|
107 |
)
|
108 |
with gr.Row():
|
@@ -122,7 +118,7 @@ with gr.Blocks(title="SelamGPT Image Generator") as demo:
|
|
122 |
output_image = gr.Image(
|
123 |
label="Generated Image (WebP)",
|
124 |
type="pil",
|
125 |
-
format="webp",
|
126 |
height=512
|
127 |
)
|
128 |
status_output = gr.Textbox(
|
@@ -143,5 +139,10 @@ with gr.Blocks(title="SelamGPT Image Generator") as demo:
|
|
143 |
)
|
144 |
|
145 |
if __name__ == "__main__":
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
147 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
13 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
14 |
WATERMARK_TEXT = "SelamGPT"
|
15 |
MAX_RETRIES = 3
|
16 |
+
TIMEOUT = 45
|
17 |
+
EXECUTOR = ThreadPoolExecutor(max_workers=3)
|
18 |
|
19 |
+
# ===== OPTIMIZED WATERMARK FUNCTION =====
|
20 |
def add_watermark(image_bytes):
|
|
|
21 |
try:
|
22 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
23 |
draw = ImageDraw.Draw(image)
|
|
|
35 |
draw.text((x+1, y+1), WATERMARK_TEXT, font=font, fill=(0, 0, 0, 128))
|
36 |
draw.text((x, y), WATERMARK_TEXT, font=font, fill=(255, 255, 255))
|
37 |
|
|
|
38 |
webp_buffer = io.BytesIO()
|
39 |
+
image.save(webp_buffer, format="WEBP", quality=85)
|
40 |
webp_buffer.seek(0)
|
41 |
return Image.open(webp_buffer)
|
42 |
except Exception as e:
|
43 |
print(f"Watermark error: {str(e)}")
|
44 |
return Image.open(io.BytesIO(image_bytes))
|
45 |
|
46 |
+
# ===== IMAGE GENERATION =====
|
47 |
def generate_image(prompt):
|
48 |
if not prompt.strip():
|
49 |
return None, "⚠️ Please enter a prompt"
|
50 |
|
|
|
51 |
params = {
|
52 |
+
"height": 768,
|
53 |
"width": 768,
|
54 |
+
"num_inference_steps": 20,
|
55 |
+
"guidance_scale": 7.0,
|
56 |
+
"options": {"wait_for_model": False}
|
57 |
}
|
58 |
|
59 |
def api_call():
|
|
|
76 |
gen_time = time.time() - start_time
|
77 |
return add_watermark(response.content), f"✔️ Generated in {gen_time:.1f}s"
|
78 |
elif response.status_code == 503:
|
79 |
+
time.sleep(5 * (attempt + 1))
|
|
|
80 |
continue
|
81 |
else:
|
82 |
return None, f"⚠️ API Error: {response.text[:200]}"
|
|
|
98 |
with gr.Column(scale=3):
|
99 |
prompt_input = gr.Textbox(
|
100 |
label="Describe your image",
|
101 |
+
placeholder="A futuristic Ethiopian city...",
|
102 |
lines=3
|
103 |
)
|
104 |
with gr.Row():
|
|
|
118 |
output_image = gr.Image(
|
119 |
label="Generated Image (WebP)",
|
120 |
type="pil",
|
121 |
+
format="webp",
|
122 |
height=512
|
123 |
)
|
124 |
status_output = gr.Textbox(
|
|
|
139 |
)
|
140 |
|
141 |
if __name__ == "__main__":
|
142 |
+
# Version-compatible queue setup
|
143 |
+
try:
|
144 |
+
demo.queue(concurrency_count=3) # New Gradio
|
145 |
+
except TypeError:
|
146 |
+
demo.queue(max_size=3) # Old Gradio
|
147 |
+
|
148 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|