# stable-diffusion-starter **Repository Path**: osheyx/stable-diffusion-starter ## Basic Information - **Project Name**: stable-diffusion-starter - **Description**: stable-diffusion 模型部署 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-06-18 - **Last Updated**: 2025-06-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Stable Diffusion 3.5 HTTP服务 这是一个基于Stable Diffusion 3.5的HTTP API服务,可以通过HTTP请求生成图片。 ## 文件说明 - `sd-3.5-start.py` - 原始的单次生成脚本 - `sd-3.5-server.py` - HTTP服务主程序 - `test_client.py` - 测试客户端 - `requirements.txt` - 依赖包列表 ## 安装依赖 ```bash pip install -r requirements.txt ``` ## 启动服务 ```bash python sd-3.5-server.py ``` 服务将在 `http://localhost:5000` 启动。 ## API接口 ### 1. 健康检查 **GET** `/health` 返回服务状态和模型加载情况。 **响应示例:** ```json { "status": "healthy", "model_loaded": true } ``` ### 2. 生成图片(文件流) **POST** `/generate` 生成图片并直接返回PNG文件流。 **请求参数:** ```json { "prompt": "A beautiful sunset over mountains", "num_inference_steps": 4, "guidance_scale": 0.0 } ``` **响应:** PNG图片文件流 ### 3. 生成图片(JSON base64) **POST** `/generate_json` 生成图片并返回base64编码的JSON响应。 **请求参数:** ```json { "prompt": "A cute cat playing with a ball", "num_inference_steps": 4, "guidance_scale": 0.0 } ``` **响应示例:** ```json { "success": true, "image": "iVBORw0KGgoAAAANSUhEUgAA...", "format": "png", "prompt": "A cute cat playing with a ball" } ``` ## 参数说明 - `prompt` (必需): 图片生成的提示词 - `num_inference_steps` (可选): 推理步数,默认为4 - `guidance_scale` (可选): 引导比例,默认为0.0 ## 测试 运行测试客户端: ```bash python test_client.py ``` ## 使用示例 ### curl命令示例 ```bash # 生成图片(文件流) curl -X POST http://localhost:5000/generate \ -H "Content-Type: application/json" \ -d '{"prompt": "A beautiful landscape"}' \ --output generated_image.png # 生成图片(JSON) curl -X POST http://localhost:5000/generate_json \ -H "Content-Type: application/json" \ -d '{"prompt": "A beautiful landscape"}' ``` ### Python客户端示例 ```python import requests # 生成图片(文件流) response = requests.post( "http://localhost:5000/generate", json={"prompt": "A beautiful sunset"} ) if response.status_code == 200: with open("image.png", "wb") as f: f.write(response.content) ``` ## 注意事项 1. 首次启动时需要下载模型,可能需要较长时间 2. 需要CUDA支持的GPU 3. 建议至少8GB显存 4. 服务启动后模型会常驻内存,重复请求响应更快 ## 错误处理 服务会返回适当的HTTP状态码和错误信息: - `400` - 请求参数错误 - `500` - 服务器内部错误(如模型未加载、生成失败等) 错误响应格式: ```json { "error": "错误描述" } ```