This repository provides an example of how to deploy a Stable Diffusion based image defect generator on CentOS 7. The service exposes a simple HTTP API that accepts an input image and a defect type (0, 1 or 2) and returns an image with the requested defect applied. If the uploaded image matches one of the pre-generated samples (checked via MD5), the server returns the pre-generated result instead of creating a new image.
The repository contains:
requirements.txt– Python package requirements.server.py– FastAPI service that runs the model and performs MD5 checking.dataset_md5.py– helper script to build a JSON mapping of MD5 hashes to existing defect images.package_env.sh– optional script showing how to download all required Python wheels for offline installation.
-
Install system packages:
sudo yum install -y epel-release sudo yum install -y git python3 python3-venv
-
(Optional) Install NVIDIA drivers and CUDA if GPU acceleration is needed. Refer to the official NVIDIA documentation for CentOS 7.
- Clone this repository and enter it:
git clone <repo-url> cd Defect
- Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate - Install the required Python packages:
pip install -r requirements.txt
If running on a machine without internet access, execute package_env.sh on a
machine that does have internet. It will download all required wheels into
wheels/. Copy that directory to the target machine and install with
pip install wheels/*.
Use dataset_md5.py to create a JSON file containing MD5 hashes of the normal
images mapped to the corresponding defect images. Example:
python dataset_md5.py data/md5_map.json \
--pairs "/path/to/设备破损/ref" "/path/to/设备破损/def" \
--pairs "/path/to/设备烟火/ref" "/path/to/设备烟火/def" \
--pairs "/path/to/渗漏油/ref" "/path/to/渗漏油/def"The resulting data/md5_map.json will be loaded by server.py at runtime.
Start the API with:
uvicorn server:app --host 0.0.0.0 --port 8000Request format:
- URL:
/generate - Method:
POST - Form fields:
defect_type– integer0,1or2.file– image file to process.
Example using curl:
curl -F defect_type=1 -F file=@input.jpg http://localhost:8000/generate --output result.pngThe service will respond with the defect image. If the uploaded image matches an
entry in md5_map.json, the corresponding pre-generated image is returned.
Otherwise the model generates a new image using Stable Diffusion.
- On a machine with internet access, run
package_env.sh. This downloads all Python wheels specified inrequirements.txtinto thewheels/directory. - Copy the repository and the
wheels/directory to the target server. - Create a virtual environment and install the wheels:
python3 -m venv venv source venv/bin/activate pip install wheels/*
- Copy or recreate the
data/md5_map.jsonfile on each server. - Start the API as shown above.
With this approach you can replicate the same environment on multiple CentOS 7 servers without re-downloading packages from the internet.