Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- expo 아이폰 오류
- 크로마DB
- VectorDB
- langchain react agent
- rnn gnsfus
- 리액트 네이티브
- AI
- expo 버전 오류
- expo go 오류
- nestjs시큐리티
- expo 아이폰
- 랭체인 툴
- comfyui
- langgraph
- react
- 이미지처리
- 타입스크립트상태관리
- expo 51 버전
- vllmmcp
- 스프링 공부
- 상태관리
- expo 안드로이드
- langchain
- jotai
- 자바공부
- expo 51 오류
- langgraph mcp
- 네스트시큐리티
- 리액트 네이티브 오류
- langchain tools
Archives
- Today
- Total
영리의 테크블로그
ComfyUI API 사용법 구축 (FAST api) 본문
1. 코드 설명
FastAPI를 사용하여 ComfyUI의 이미지 생성 기능을 API로 제공.
주요 구성 요소:
FastAPI앱 설정Workflow모델 정의 (Pydantic 사용)queue_prompt함수: ComfyUI에 워크플로우 전송check_progress함수: 이미지 생성 진행 상황 확인/generate엔드포인트: 이미지 생성 요청 처리
2. 사용 방법
- ComfyUI 서버 실행:
ComfyUI 서버가127.0.0.1:8188에서 실행 중이어야 함. - API 서버 실행:
python your_script_name.py- API 호출:
- URL:
http://localhost:8000/generate - 메소드: POST
- 요청 본문: ComfyUI 워크플로우 JSON
- URL:
import requests workflow = { # ComfyUI 워크플로우 JSON } response = requests.post("http://localhost:8000/generate", json={"workflow": workflow}) print(response.json())- 응답:
{ "status": "completed", "image": "http://127.0.0.1:8188/view?filename=generated_image.png&type=temp" }
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import json
from urllib import request
import asyncio
app = FastAPI()
COMFYUI_IP = "127.0.0.1:8188" # ComfyUI 서버 IP 및 포트
class Workflow(BaseModel):
workflow: dict
def queue_prompt(prompt_workflow, ip):
p = {"prompt": prompt_workflow}
data = json.dumps(p).encode('utf-8')
req = request.Request(f"http://{ip}/prompt", data=data)
try:
res = request.urlopen(req)
if res.code != 200:
raise Exception(f"Error: {res.code} {res.reason}")
return json.loads(res.read().decode('utf-8'))['prompt_id']
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
async def check_progress(prompt_id: str, ip: str):
while True:
try:
req = request.Request(f"http://{ip}/history/{prompt_id}")
res = request.urlopen(req)
if res.code == 200:
history = json.loads(res.read().decode('utf-8'))
if prompt_id in history:
return history[prompt_id]
except Exception as e:
print(f"Error checking progress: {str(e)}")
await asyncio.sleep(1) # 1초 대기
@app.post("/generate")
async def generate(workflow: Workflow):
try:
prompt_id = queue_prompt(workflow.workflow, COMFYUI_IP)
result = await check_progress(prompt_id, COMFYUI_IP)
# 결과에서 마지막 이미지 URL 추출
final_image_url = None
for node_id, node_output in result['outputs'].items():
if 'images' in node_output:
for image in node_output['images']:
final_image_url = f"http://{COMFYUI_IP}/view?filename={image['filename']}&type=temp"
if final_image_url:
return {"status": "completed", "image": final_image_url}
else:
return {"status": "completed", "image": None}
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)'dev > AI' 카테고리의 다른 글
| Vector DB (Chroma DB)을 활용한 LLM 페르소나 부여하기 (0) | 2024.10.09 |
|---|---|
| Stable Diffusion ComfyUI/Web UI fastapi CORS 설정 방법 (0) | 2024.10.01 |
| Stable Diffusion Web UI CORS 설정 방법 (1) | 2024.09.25 |