Spaces:
Sleeping
Sleeping
Alex commited on
Commit ·
f8b9c38
1
Parent(s): 86cf2f4
update endpoint path
Browse files- app.py +10 -7
- gradio_app.py +56 -0
- response.json +0 -0
app.py
CHANGED
|
@@ -105,35 +105,38 @@ class ModelManager:
|
|
| 105 |
app = FastAPI()
|
| 106 |
model_manager = ModelManager()
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
|
|
|
| 110 |
try:
|
| 111 |
response = requests.get(image_data.url, stream=True)
|
| 112 |
if response.status_code != 200:
|
| 113 |
raise HTTPException(status_code=400, detail="Could not download image from URL")
|
| 114 |
|
| 115 |
image = Image.open(response.raw).convert("RGB")
|
| 116 |
-
return model_manager.
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
logging.error(f"Error processing URL: {str(e)}")
|
| 120 |
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
|
|
|
| 124 |
try:
|
| 125 |
response = requests.get(image_data.url, stream=True)
|
| 126 |
if response.status_code != 200:
|
| 127 |
raise HTTPException(status_code=400, detail="Could not download image from URL")
|
| 128 |
|
| 129 |
image = Image.open(response.raw).convert("RGB")
|
| 130 |
-
return model_manager.
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
logging.error(f"Error processing URL: {str(e)}")
|
| 134 |
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
| 135 |
|
| 136 |
-
|
|
|
|
| 137 |
async def segment_endpoint(file: UploadFile = File(...)):
|
| 138 |
try:
|
| 139 |
image_data = await file.read()
|
|
|
|
| 105 |
app = FastAPI()
|
| 106 |
model_manager = ModelManager()
|
| 107 |
|
| 108 |
+
|
| 109 |
+
@app.post("/segment-clothes-url")
|
| 110 |
+
async def segment_clothes_url_endpoint(image_data: ImageURL):
|
| 111 |
try:
|
| 112 |
response = requests.get(image_data.url, stream=True)
|
| 113 |
if response.status_code != 200:
|
| 114 |
raise HTTPException(status_code=400, detail="Could not download image from URL")
|
| 115 |
|
| 116 |
image = Image.open(response.raw).convert("RGB")
|
| 117 |
+
return model_manager.process_clothes_image(image)
|
| 118 |
|
| 119 |
except Exception as e:
|
| 120 |
logging.error(f"Error processing URL: {str(e)}")
|
| 121 |
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
| 122 |
|
| 123 |
+
|
| 124 |
+
@app.post("/segment-fashion-url")
|
| 125 |
+
async def segment_url_endpoint(image_data: ImageURL):
|
| 126 |
try:
|
| 127 |
response = requests.get(image_data.url, stream=True)
|
| 128 |
if response.status_code != 200:
|
| 129 |
raise HTTPException(status_code=400, detail="Could not download image from URL")
|
| 130 |
|
| 131 |
image = Image.open(response.raw).convert("RGB")
|
| 132 |
+
return model_manager.process_fashion_image(image)
|
| 133 |
|
| 134 |
except Exception as e:
|
| 135 |
logging.error(f"Error processing URL: {str(e)}")
|
| 136 |
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
| 137 |
|
| 138 |
+
|
| 139 |
+
@app.post("/segment-fashion-file")
|
| 140 |
async def segment_endpoint(file: UploadFile = File(...)):
|
| 141 |
try:
|
| 142 |
image_data = await file.read()
|
gradio_app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import base64
|
| 6 |
+
import logging
|
| 7 |
+
from app import ModelManager
|
| 8 |
+
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
def process_image(url: str):
|
| 13 |
+
try:
|
| 14 |
+
# Initialize model manager (will load models if not already loaded)
|
| 15 |
+
model_manager = ModelManager()
|
| 16 |
+
|
| 17 |
+
# Download image from URL
|
| 18 |
+
response = requests.get(url, stream=True)
|
| 19 |
+
if response.status_code != 200:
|
| 20 |
+
raise ValueError("Could not download image from URL")
|
| 21 |
+
|
| 22 |
+
# Process image
|
| 23 |
+
image = Image.open(response.raw).convert("RGB")
|
| 24 |
+
result = model_manager.process_clothes_image(image)
|
| 25 |
+
|
| 26 |
+
# Convert base64 mask back to image
|
| 27 |
+
mask_data = result["mask"].split(",")[1]
|
| 28 |
+
mask_bytes = base64.b64decode(mask_data)
|
| 29 |
+
mask_image = Image.open(io.BytesIO(mask_bytes))
|
| 30 |
+
|
| 31 |
+
return image, mask_image, f"Processed image size: {result['size']}"
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
logger.error(f"Error processing image: {str(e)}")
|
| 35 |
+
return None, None, f"Error: {str(e)}"
|
| 36 |
+
|
| 37 |
+
# Create Gradio interface
|
| 38 |
+
iface = gr.Interface(
|
| 39 |
+
fn=process_image,
|
| 40 |
+
inputs=gr.Textbox(label="Image URL", placeholder="Enter the URL of the image"),
|
| 41 |
+
outputs=[
|
| 42 |
+
gr.Image(label="Original Image"),
|
| 43 |
+
gr.Image(label="Segmentation Mask"),
|
| 44 |
+
gr.Textbox(label="Processing Info")
|
| 45 |
+
],
|
| 46 |
+
title="Clothes Segmentation",
|
| 47 |
+
description="Enter an image URL to generate a segmentation mask for clothing items.",
|
| 48 |
+
examples=[
|
| 49 |
+
["https://example.com/path/to/clothing/image.jpg"],
|
| 50 |
+
["https://another-example.com/fashion/photo.jpg"]
|
| 51 |
+
],
|
| 52 |
+
allow_flagging="never"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
iface.launch(server_port=7861) # Using different port than FastAPI
|
response.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|