Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -64,9 +64,9 @@ CLOTHES_DB = {
|
|
| 64 |
|
| 65 |
avatars = {}
|
| 66 |
API_KEY = os.getenv("API_KEY", "hf-sexy-outfits-2026-V2")
|
| 67 |
-
executor = ThreadPoolExecutor(max_workers=
|
| 68 |
|
| 69 |
-
# Load model
|
| 70 |
pipe = None
|
| 71 |
def load_model():
|
| 72 |
global pipe
|
|
@@ -75,13 +75,16 @@ def load_model():
|
|
| 75 |
"runwayml/stable-diffusion-v1-5",
|
| 76 |
torch_dtype=torch.float16,
|
| 77 |
safety_checker=None,
|
| 78 |
-
use_safetensors=True
|
|
|
|
| 79 |
)
|
| 80 |
pipe.scheduler = EulerADiscreteScheduler.from_config(pipe.scheduler.config)
|
|
|
|
|
|
|
| 81 |
pipe = pipe.to("cuda")
|
| 82 |
return pipe
|
| 83 |
|
| 84 |
-
# API ENDPOINTS
|
| 85 |
@flask_app.route('/health', methods=['GET'])
|
| 86 |
def health():
|
| 87 |
return jsonify({"status": "HD OUTFIT API V2 READY π₯", "clothes": len(CLOTHES_DB)})
|
|
@@ -94,13 +97,11 @@ def get_avatar(user_id):
|
|
| 94 |
if user_id not in avatars:
|
| 95 |
avatars[user_id] = get_default_outfit()
|
| 96 |
|
| 97 |
-
|
| 98 |
-
image_b64 = future.result(timeout=120)
|
| 99 |
-
|
| 100 |
return jsonify({
|
| 101 |
**avatars[user_id],
|
| 102 |
"image": image_b64,
|
| 103 |
-
"url": f"/api/avatar/{user_id}"
|
| 104 |
})
|
| 105 |
|
| 106 |
@flask_app.route('/api/avatar/<user_id>/clothes/<category>', methods=['PATCH'])
|
|
@@ -108,8 +109,13 @@ def change_clothes(user_id, category):
|
|
| 108 |
if request.headers.get('x-owner-key') != API_KEY:
|
| 109 |
return jsonify({"error": "Owner key required"}), 401
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
if user_id not in avatars:
|
| 115 |
avatars[user_id] = get_default_outfit()
|
|
@@ -117,10 +123,7 @@ def change_clothes(user_id, category):
|
|
| 117 |
available = list(CLOTHES_DB.get(category, {}).keys())
|
| 118 |
if item in available:
|
| 119 |
avatars[user_id][category] = item
|
| 120 |
-
|
| 121 |
-
future = executor.submit(generate_hd_image, avatars[user_id])
|
| 122 |
-
image_b64 = future.result(timeout=120)
|
| 123 |
-
|
| 124 |
return jsonify({
|
| 125 |
**avatars[user_id],
|
| 126 |
"image": image_b64,
|
|
@@ -138,10 +141,7 @@ def random_outfit(user_id):
|
|
| 138 |
return jsonify({"error": "Owner key required"}), 401
|
| 139 |
|
| 140 |
avatars[user_id] = generate_random_outfit()
|
| 141 |
-
|
| 142 |
-
future = executor.submit(generate_hd_image, avatars[user_id])
|
| 143 |
-
image_b64 = future.result(timeout=120)
|
| 144 |
-
|
| 145 |
return jsonify({**avatars[user_id], "image": image_b64})
|
| 146 |
|
| 147 |
@flask_app.route('/api/avatar/<user_id>/strip', methods=['POST'])
|
|
@@ -157,10 +157,7 @@ def strip_all(user_id):
|
|
| 157 |
"shoes": "none",
|
| 158 |
"pose": "standing"
|
| 159 |
}
|
| 160 |
-
|
| 161 |
-
future = executor.submit(generate_hd_image, avatars[user_id])
|
| 162 |
-
image_b64 = future.result(timeout=120)
|
| 163 |
-
|
| 164 |
return jsonify({**avatars[user_id], "image": image_b64, "action": "FULLY STRIPPED"})
|
| 165 |
|
| 166 |
@flask_app.route('/api/clothes', methods=['GET'])
|
|
@@ -193,10 +190,8 @@ def generate_random_outfit():
|
|
| 193 |
return outfit
|
| 194 |
|
| 195 |
def generate_hd_image(outfit):
|
| 196 |
-
global pipe
|
| 197 |
pipe = load_model()
|
| 198 |
|
| 199 |
-
# Build ULTIMATE prompt
|
| 200 |
prompt_parts = [
|
| 201 |
"photorealistic, 8k uhd, hyper detailed, professional photography",
|
| 202 |
"beautiful model, perfect face, flawless skin, seductive expression",
|
|
@@ -210,18 +205,20 @@ def generate_hd_image(outfit):
|
|
| 210 |
prompt = ", ".join(p for p in prompt_parts if p)
|
| 211 |
negative = "blurry, lowres, deformed, extra limbs, ugly, fat, old, watermark, text"
|
| 212 |
|
|
|
|
| 213 |
image = pipe(
|
| 214 |
-
prompt,
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
| 218 |
).images[0]
|
| 219 |
|
| 220 |
buffer = io.BytesIO()
|
| 221 |
image.save(buffer, format="PNG", optimize=True)
|
| 222 |
return f"data:image/png;base64,{base64.b64encode(buffer.getvalue()).decode()}"
|
| 223 |
|
| 224 |
-
# Gradio UI
|
| 225 |
def gradio_generate(top, bra, boobs, bottom, shoes, pose):
|
| 226 |
outfit = {
|
| 227 |
k: v if v and v != "None" else "none"
|
|
@@ -250,9 +247,8 @@ with gr.Blocks(title="π₯ HD Outfit Strip Generator V2") as demo:
|
|
| 250 |
random_btn = gr.Button("π² RANDOM OUTFIT", variant="secondary")
|
| 251 |
generate_btn = gr.Button("π¨ GENERATE HD", variant="primary")
|
| 252 |
|
| 253 |
-
output_img = gr.Image(label="HD
|
| 254 |
|
| 255 |
-
# Event handlers
|
| 256 |
generate_btn.click(gradio_generate, [top,bra,boobs,bottom,shoes,pose], output_img)
|
| 257 |
random_btn.click(
|
| 258 |
lambda: {k: np.random.choice(list(v.keys())) for k,v in CLOTHES_DB.items()},
|
|
@@ -264,26 +260,21 @@ with gr.Blocks(title="π₯ HD Outfit Strip Generator V2") as demo:
|
|
| 264 |
)
|
| 265 |
|
| 266 |
gr.Markdown("""
|
| 267 |
-
### π‘ **API READY**
|
| 268 |
```
|
| 269 |
-
# Get image
|
| 270 |
curl -H "x-owner-key: hf-sexy-outfits-2026-V2" \\
|
| 271 |
https://yourspace.hf.space/api/avatar/model1
|
| 272 |
|
| 273 |
-
# Remove bra
|
| 274 |
-
curl -X PATCH -H "x-owner-key: KEY" -d '{"item":"none"}' \\
|
| 275 |
-
https://space/api/avatar/model1/clothes/bra
|
| 276 |
-
|
| 277 |
-
# Strip everything
|
| 278 |
curl -X POST -H "x-owner-key: KEY" \\
|
| 279 |
https://space/api/avatar/model1/strip
|
| 280 |
```
|
| 281 |
""")
|
| 282 |
|
| 283 |
def run_flask():
|
| 284 |
-
flask_app.run(host="0.0.0.0", port=7860)
|
| 285 |
|
| 286 |
if __name__ == "__main__":
|
| 287 |
flask_thread = threading.Thread(target=run_flask, daemon=True)
|
| 288 |
flask_thread.start()
|
| 289 |
-
|
|
|
|
|
|
| 64 |
|
| 65 |
avatars = {}
|
| 66 |
API_KEY = os.getenv("API_KEY", "hf-sexy-outfits-2026-V2")
|
| 67 |
+
executor = ThreadPoolExecutor(max_workers=2) # Reduced for HF Spaces
|
| 68 |
|
| 69 |
+
# Load model - HF Spaces optimized
|
| 70 |
pipe = None
|
| 71 |
def load_model():
|
| 72 |
global pipe
|
|
|
|
| 75 |
"runwayml/stable-diffusion-v1-5",
|
| 76 |
torch_dtype=torch.float16,
|
| 77 |
safety_checker=None,
|
| 78 |
+
use_safetensors=True,
|
| 79 |
+
variant="fp16" # HF cached version
|
| 80 |
)
|
| 81 |
pipe.scheduler = EulerADiscreteScheduler.from_config(pipe.scheduler.config)
|
| 82 |
+
pipe.enable_attention_slicing() # Memory optimization
|
| 83 |
+
pipe.enable_model_cpu_offload() # HF Spaces memory fix
|
| 84 |
pipe = pipe.to("cuda")
|
| 85 |
return pipe
|
| 86 |
|
| 87 |
+
# API ENDPOINTS - Fixed request parsing
|
| 88 |
@flask_app.route('/health', methods=['GET'])
|
| 89 |
def health():
|
| 90 |
return jsonify({"status": "HD OUTFIT API V2 READY π₯", "clothes": len(CLOTHES_DB)})
|
|
|
|
| 97 |
if user_id not in avatars:
|
| 98 |
avatars[user_id] = get_default_outfit()
|
| 99 |
|
| 100 |
+
image_b64 = generate_hd_image(avatars[user_id])
|
|
|
|
|
|
|
| 101 |
return jsonify({
|
| 102 |
**avatars[user_id],
|
| 103 |
"image": image_b64,
|
| 104 |
+
"url": f"/api/avatar/{user_id}"
|
| 105 |
})
|
| 106 |
|
| 107 |
@flask_app.route('/api/avatar/<user_id>/clothes/<category>', methods=['PATCH'])
|
|
|
|
| 109 |
if request.headers.get('x-owner-key') != API_KEY:
|
| 110 |
return jsonify({"error": "Owner key required"}), 401
|
| 111 |
|
| 112 |
+
# Fixed JSON parsing
|
| 113 |
+
try:
|
| 114 |
+
data = request.get_json()
|
| 115 |
+
except:
|
| 116 |
+
return jsonify({"error": "Invalid JSON"}), 400
|
| 117 |
+
|
| 118 |
+
item = data.get('item') if data else None
|
| 119 |
|
| 120 |
if user_id not in avatars:
|
| 121 |
avatars[user_id] = get_default_outfit()
|
|
|
|
| 123 |
available = list(CLOTHES_DB.get(category, {}).keys())
|
| 124 |
if item in available:
|
| 125 |
avatars[user_id][category] = item
|
| 126 |
+
image_b64 = generate_hd_image(avatars[user_id])
|
|
|
|
|
|
|
|
|
|
| 127 |
return jsonify({
|
| 128 |
**avatars[user_id],
|
| 129 |
"image": image_b64,
|
|
|
|
| 141 |
return jsonify({"error": "Owner key required"}), 401
|
| 142 |
|
| 143 |
avatars[user_id] = generate_random_outfit()
|
| 144 |
+
image_b64 = generate_hd_image(avatars[user_id])
|
|
|
|
|
|
|
|
|
|
| 145 |
return jsonify({**avatars[user_id], "image": image_b64})
|
| 146 |
|
| 147 |
@flask_app.route('/api/avatar/<user_id>/strip', methods=['POST'])
|
|
|
|
| 157 |
"shoes": "none",
|
| 158 |
"pose": "standing"
|
| 159 |
}
|
| 160 |
+
image_b64 = generate_hd_image(avatars[user_id])
|
|
|
|
|
|
|
|
|
|
| 161 |
return jsonify({**avatars[user_id], "image": image_b64, "action": "FULLY STRIPPED"})
|
| 162 |
|
| 163 |
@flask_app.route('/api/clothes', methods=['GET'])
|
|
|
|
| 190 |
return outfit
|
| 191 |
|
| 192 |
def generate_hd_image(outfit):
|
|
|
|
| 193 |
pipe = load_model()
|
| 194 |
|
|
|
|
| 195 |
prompt_parts = [
|
| 196 |
"photorealistic, 8k uhd, hyper detailed, professional photography",
|
| 197 |
"beautiful model, perfect face, flawless skin, seductive expression",
|
|
|
|
| 205 |
prompt = ", ".join(p for p in prompt_parts if p)
|
| 206 |
negative = "blurry, lowres, deformed, extra limbs, ugly, fat, old, watermark, text"
|
| 207 |
|
| 208 |
+
# HF Spaces optimized params
|
| 209 |
image = pipe(
|
| 210 |
+
prompt,
|
| 211 |
+
negative_prompt=negative,
|
| 212 |
+
width=768, height=1152, # Reduced for memory
|
| 213 |
+
num_inference_steps=25, # Faster
|
| 214 |
+
guidance_scale=7.5
|
| 215 |
).images[0]
|
| 216 |
|
| 217 |
buffer = io.BytesIO()
|
| 218 |
image.save(buffer, format="PNG", optimize=True)
|
| 219 |
return f"data:image/png;base64,{base64.b64encode(buffer.getvalue()).decode()}"
|
| 220 |
|
| 221 |
+
# Gradio UI
|
| 222 |
def gradio_generate(top, bra, boobs, bottom, shoes, pose):
|
| 223 |
outfit = {
|
| 224 |
k: v if v and v != "None" else "none"
|
|
|
|
| 247 |
random_btn = gr.Button("π² RANDOM OUTFIT", variant="secondary")
|
| 248 |
generate_btn = gr.Button("π¨ GENERATE HD", variant="primary")
|
| 249 |
|
| 250 |
+
output_img = gr.Image(label="HD Image", height=600)
|
| 251 |
|
|
|
|
| 252 |
generate_btn.click(gradio_generate, [top,bra,boobs,bottom,shoes,pose], output_img)
|
| 253 |
random_btn.click(
|
| 254 |
lambda: {k: np.random.choice(list(v.keys())) for k,v in CLOTHES_DB.items()},
|
|
|
|
| 260 |
)
|
| 261 |
|
| 262 |
gr.Markdown("""
|
| 263 |
+
### π‘ **API READY**
|
| 264 |
```
|
|
|
|
| 265 |
curl -H "x-owner-key: hf-sexy-outfits-2026-V2" \\
|
| 266 |
https://yourspace.hf.space/api/avatar/model1
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
curl -X POST -H "x-owner-key: KEY" \\
|
| 269 |
https://space/api/avatar/model1/strip
|
| 270 |
```
|
| 271 |
""")
|
| 272 |
|
| 273 |
def run_flask():
|
| 274 |
+
flask_app.run(host="0.0.0.0", port=7860, debug=False)
|
| 275 |
|
| 276 |
if __name__ == "__main__":
|
| 277 |
flask_thread = threading.Thread(target=run_flask, daemon=True)
|
| 278 |
flask_thread.start()
|
| 279 |
+
time.sleep(2) # Give Flask time to start
|
| 280 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|