Upload 3 files
Browse files- Dockerfile +33 -0
- main.py +27 -0
- requirements.txt +4 -0
Dockerfile
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python runtime as a parent image
|
2 |
+
# Using a specific version like 3.10 or 3.11 is recommended over 'latest'
|
3 |
+
FROM python:3.10-slim
|
4 |
+
|
5 |
+
# Set the working directory in the container
|
6 |
+
WORKDIR /app
|
7 |
+
|
8 |
+
# Install system dependencies if needed (e.g., build tools) - add if required later
|
9 |
+
# RUN apt-get update && apt-get install -y --no-install-recommends some-package && rm -rf /var/lib/apt/lists/*
|
10 |
+
|
11 |
+
# Copy the requirements file into the container
|
12 |
+
COPY requirements.txt .
|
13 |
+
|
14 |
+
# Install Python dependencies
|
15 |
+
# --no-cache-dir reduces image size
|
16 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
17 |
+
pip install --no-cache-dir -r requirements.txt
|
18 |
+
|
19 |
+
# Copy the rest of the application code into the container
|
20 |
+
COPY . .
|
21 |
+
|
22 |
+
# Explicitly create cache dir and set open permissions during build
|
23 |
+
RUN mkdir -p /app/.cache && chmod -R 777 /app/.cache
|
24 |
+
|
25 |
+
# Set environment variables for Hugging Face cache directories
|
26 |
+
ENV HF_HOME=/app/.cache/huggingface
|
27 |
+
ENV HF_DATASETS_CACHE=/app/.cache/datasets
|
28 |
+
|
29 |
+
# Expose the port the app runs on
|
30 |
+
EXPOSE 7860
|
31 |
+
|
32 |
+
# Define the command to run the application
|
33 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Response, HTTPException
|
2 |
+
from datasets import load_dataset
|
3 |
+
from io import BytesIO
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
dataset = load_dataset("visionLMsftw/vibe-testing-samples", split="train")
|
9 |
+
id_to_image = {example["ex_id"]: example["image"] for example in dataset}
|
10 |
+
|
11 |
+
print(id_to_image)
|
12 |
+
|
13 |
+
@app.get("/image/{image_id}")
|
14 |
+
def get_image(image_id: int):
|
15 |
+
if image_id not in id_to_image:
|
16 |
+
raise HTTPException(status_code=404, detail="Image not found")
|
17 |
+
|
18 |
+
image: Image.Image = id_to_image[image_id].convert("RGB")
|
19 |
+
buffer = BytesIO()
|
20 |
+
image.save(buffer, format="JPEG", quality=85)
|
21 |
+
buffer.seek(0)
|
22 |
+
return Response(content=buffer.read(), media_type="image/jpeg")
|
23 |
+
|
24 |
+
@app.get("/ids")
|
25 |
+
def list_ids():
|
26 |
+
return list(id_to_image.keys())
|
27 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
datasets
|
4 |
+
pillow
|