jree423 commited on
Commit
88dea80
·
verified ·
1 Parent(s): d87b721

Fix: Update Dockerfile to use torch 2.0.0 and cairosvg

Browse files
Files changed (1) hide show
  1. Dockerfile +13 -28
Dockerfile CHANGED
@@ -2,23 +2,24 @@ FROM python:3.8-slim
2
 
3
  WORKDIR /code
4
 
5
- # Install system dependencies
6
  RUN apt-get update && apt-get install -y \
7
  build-essential \
8
  python3-dev \
9
- libfreetype6-dev \
10
  pkg-config \
11
  libpng-dev \
12
- libcairo2-dev \
13
  libffi-dev \
14
- cmake \
15
- git \
16
  && rm -rf /var/lib/apt/lists/*
17
 
18
- # Install base Python packages with fixed versions
 
 
 
 
 
 
19
  RUN pip install --no-cache-dir \
20
- torch==1.13.1 \
21
- torchvision==0.14.1 \
22
  diffusers==0.15.1 \
23
  transformers==4.27.4 \
24
  accelerate==0.18.0 \
@@ -30,22 +31,6 @@ RUN pip install --no-cache-dir \
30
  uvicorn==0.22.0 \
31
  python-multipart==0.0.6
32
 
33
- # Install SVG dependencies
34
- RUN pip install --no-cache-dir \
35
- cairosvg==2.7.0 \
36
- cairocffi==1.5.1 \
37
- cssselect2==0.7.0 \
38
- defusedxml==0.7.1 \
39
- tinycss2==1.2.1 \
40
- svgwrite==1.4.3 \
41
- svgpathtools==1.6.0
42
-
43
- # Install image processing libraries
44
- RUN pip install --no-cache-dir \
45
- opencv-python-headless==4.7.0.72 \
46
- scikit-image==0.20.0 \
47
- matplotlib==3.7.1
48
-
49
  # Create mock diffvg package
50
  RUN mkdir -p /tmp/mock_diffvg/pydiffvg && \
51
  echo '# Mock diffvg package\nimport numpy as np\nimport torch\n\ndef render(shapes, shape_groups, width, height, samples=2, seed=None):\n return torch.zeros((height, width, 3), dtype=torch.float32)\n\ndef render_shape_group(canvas, shape_group_id, shapes, shape_groups, shape_ids, samples=2, seed=None):\n pass\n\ndef save_svg(shapes, shape_groups, filename, width, height, use_gamma=False, background=None):\n with open(filename, "w") as f:\n f.write(f"<svg width=\\"{width}\\" height=\\"{height}\\" xmlns=\\"http://www.w3.org/2000/svg\\"><rect width=\\"100%\\" height=\\"100%\\" fill=\\"white\\"/></svg>")\n\ndef svg_path_to_shapes(path_string):\n return [], []\n\ndef from_svg(filename):\n return [], []\n\nclass Circle:\n def __init__(self, radius=1.0, center=None):\n self.radius = radius\n self.center = center if center is not None else torch.tensor([0.0, 0.0])\n\nclass Ellipse:\n def __init__(self, radius=None, center=None):\n self.radius = radius if radius is not None else torch.tensor([1.0, 1.0])\n self.center = center if center is not None else torch.tensor([0.0, 0.0])\n\nclass Path:\n def __init__(self, points=None, is_closed=True):\n self.points = points if points is not None else torch.tensor([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]])\n self.is_closed = is_closed\n\nclass Rect:\n def __init__(self, p_min=None, p_max=None):\n self.p_min = p_min if p_min is not None else torch.tensor([0.0, 0.0])\n self.p_max = p_max if p_max is not None else torch.tensor([1.0, 1.0])\n\nclass ShapeGroup:\n def __init__(self, shape_ids=None, fill_color=None, stroke_color=None):\n self.shape_ids = shape_ids if shape_ids is not None else []\n self.fill_color = fill_color if fill_color is not None else torch.tensor([1.0, 1.0, 1.0, 1.0])\n self.stroke_color = stroke_color if stroke_color is not None else torch.tensor([0.0, 0.0, 0.0, 1.0])' > /tmp/mock_diffvg/pydiffvg/__init__.py && \
@@ -53,11 +38,11 @@ RUN mkdir -p /tmp/mock_diffvg/pydiffvg && \
53
  cd /tmp/mock_diffvg && \
54
  pip install .
55
 
56
- # Create a simple handler.py if it doesn't exist
57
- RUN echo 'import os\nimport io\nimport base64\nimport json\nimport torch\nimport numpy as np\nfrom PIL import Image\nimport cairosvg\n\nclass ModelHandler:\n def __init__(self):\n self.initialized = False\n self.model = None\n self.device = "cuda" if torch.cuda.is_available() else "cpu"\n \n def initialize(self, model_dir):\n """Initialize the model"""\n # Placeholder for model initialization\n self.initialized = True\n return self.initialized\n \n def preprocess(self, data):\n """Preprocess the input data"""\n inputs = data.get("inputs", {})\n \n if isinstance(inputs, str):\n # Text-to-image case\n prompt = inputs\n image = None\n else:\n # Image-to-image case\n prompt = inputs.get("text", "")\n image_b64 = inputs.get("image", None)\n \n if image_b64:\n image_data = base64.b64decode(image_b64)\n image = Image.open(io.BytesIO(image_data))\n else:\n image = None\n \n return {"prompt": prompt, "image": image}\n \n def inference(self, inputs):\n """Run inference with the model"""\n prompt = inputs["prompt"]\n image = inputs["image"]\n \n # Generate a placeholder SVG\n width, height = 512, 512\n svg_content = f"""<svg width="{width}" height="{height}" xmlns="http://www.w3.org/2000/svg">\n <rect width="100%" height="100%" fill="#f0f0f0"/>\n <text x="50%" y="50%" font-family="Arial" font-size="20" text-anchor="middle">{prompt}</text>\n </svg>"""\n \n return svg_content\n \n def postprocess(self, inference_output):\n """Postprocess the model output"""\n # Convert SVG to base64 for response\n svg_bytes = inference_output.encode("utf-8")\n svg_base64 = base64.b64encode(svg_bytes).decode("utf-8")\n \n # Convert SVG to PNG using cairosvg\n try:\n png_data = cairosvg.svg2png(bytestring=svg_bytes)\n png_base64 = base64.b64encode(png_data).decode("utf-8")\n except Exception as e:\n print(f"Error converting SVG to PNG: {e}")\n # Return a transparent 1x1 pixel PNG as fallback\n png_base64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="\n \n return {\n "svg": inference_output,\n "svg_base64": svg_base64,\n "png_base64": png_base64\n }\n \n def handle(self, data):\n """Handle a request to the model"""\n try:\n if not self.initialized:\n self.initialize("model")\n \n if data is None:\n return {"error": "No input data provided"}\n \n # Preprocess\n inputs = self.preprocess(data)\n \n # Inference\n outputs = self.inference(inputs)\n \n # Postprocess\n processed_outputs = self.postprocess(outputs)\n \n return processed_outputs\n except Exception as e:\n return {"error": str(e)}\n\ndef handler(inputs, **kwargs):\n """Handler function for the API"""\n model_handler = ModelHandler()\n return model_handler.handle(inputs)' > /code/handler.py
58
 
59
- # Copy application code
60
- COPY . .
61
 
62
  # Set up the API
63
  CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "7860"]
 
2
 
3
  WORKDIR /code
4
 
5
+ # Install system dependencies for Cairo
6
  RUN apt-get update && apt-get install -y \
7
  build-essential \
8
  python3-dev \
9
+ libcairo2-dev \
10
  pkg-config \
11
  libpng-dev \
 
12
  libffi-dev \
 
 
13
  && rm -rf /var/lib/apt/lists/*
14
 
15
+ # Install torch 2.0.0 and torchvision 0.15.1
16
+ RUN pip install --no-cache-dir torch==2.0.0 torchvision==0.15.1
17
+
18
+ # Install cairosvg and its dependencies
19
+ RUN pip install --no-cache-dir cairosvg==2.7.0 cairocffi==1.5.1 cssselect2==0.7.0 defusedxml==0.7.1 tinycss2==1.2.1
20
+
21
+ # Install other dependencies
22
  RUN pip install --no-cache-dir \
 
 
23
  diffusers==0.15.1 \
24
  transformers==4.27.4 \
25
  accelerate==0.18.0 \
 
31
  uvicorn==0.22.0 \
32
  python-multipart==0.0.6
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  # Create mock diffvg package
35
  RUN mkdir -p /tmp/mock_diffvg/pydiffvg && \
36
  echo '# Mock diffvg package\nimport numpy as np\nimport torch\n\ndef render(shapes, shape_groups, width, height, samples=2, seed=None):\n return torch.zeros((height, width, 3), dtype=torch.float32)\n\ndef render_shape_group(canvas, shape_group_id, shapes, shape_groups, shape_ids, samples=2, seed=None):\n pass\n\ndef save_svg(shapes, shape_groups, filename, width, height, use_gamma=False, background=None):\n with open(filename, "w") as f:\n f.write(f"<svg width=\\"{width}\\" height=\\"{height}\\" xmlns=\\"http://www.w3.org/2000/svg\\"><rect width=\\"100%\\" height=\\"100%\\" fill=\\"white\\"/></svg>")\n\ndef svg_path_to_shapes(path_string):\n return [], []\n\ndef from_svg(filename):\n return [], []\n\nclass Circle:\n def __init__(self, radius=1.0, center=None):\n self.radius = radius\n self.center = center if center is not None else torch.tensor([0.0, 0.0])\n\nclass Ellipse:\n def __init__(self, radius=None, center=None):\n self.radius = radius if radius is not None else torch.tensor([1.0, 1.0])\n self.center = center if center is not None else torch.tensor([0.0, 0.0])\n\nclass Path:\n def __init__(self, points=None, is_closed=True):\n self.points = points if points is not None else torch.tensor([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]])\n self.is_closed = is_closed\n\nclass Rect:\n def __init__(self, p_min=None, p_max=None):\n self.p_min = p_min if p_min is not None else torch.tensor([0.0, 0.0])\n self.p_max = p_max if p_max is not None else torch.tensor([1.0, 1.0])\n\nclass ShapeGroup:\n def __init__(self, shape_ids=None, fill_color=None, stroke_color=None):\n self.shape_ids = shape_ids if shape_ids is not None else []\n self.fill_color = fill_color if fill_color is not None else torch.tensor([1.0, 1.0, 1.0, 1.0])\n self.stroke_color = stroke_color if stroke_color is not None else torch.tensor([0.0, 0.0, 0.0, 1.0])' > /tmp/mock_diffvg/pydiffvg/__init__.py && \
 
38
  cd /tmp/mock_diffvg && \
39
  pip install .
40
 
41
+ # Create a simple handler.py
42
+ COPY handler.py /code/handler.py
43
 
44
+ # Create a simple API file
45
+ COPY api.py /code/api.py
46
 
47
  # Set up the API
48
  CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "7860"]