Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -719,9 +719,23 @@ def _save_temp_image(img_bytes: bytes, suffix=".png") -> str:
|
|
| 719 |
|
| 720 |
def generate_flux_image_via_api(prompt: str) -> Optional[str]:
|
| 721 |
if not FLUX_API_ENABLED or not flux_api_client:
|
|
|
|
| 722 |
return None
|
| 723 |
|
| 724 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 725 |
result = flux_api_client.predict(
|
| 726 |
prompt=prompt,
|
| 727 |
width=768,
|
|
@@ -730,42 +744,61 @@ def generate_flux_image_via_api(prompt: str) -> Optional[str]:
|
|
| 730 |
inference_steps=8,
|
| 731 |
seed=random.randint(1, 1_000_000),
|
| 732 |
do_img2img=False,
|
| 733 |
-
|
|
|
|
|
|
|
|
|
|
| 734 |
)
|
| 735 |
|
| 736 |
-
logger.info(f"[FLUX]
|
| 737 |
-
|
| 738 |
-
# 1) tuple 형식 (기존)
|
| 739 |
-
if isinstance(result, tuple) and result:
|
| 740 |
-
candidate = result[0]
|
| 741 |
-
|
| 742 |
-
# 2) 문자열 URL
|
| 743 |
-
elif isinstance(result, str) and result.startswith(("http://", "https://")):
|
| 744 |
-
candidate = _save_temp_image(requests.get(result).content)
|
| 745 |
|
| 746 |
-
#
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 751 |
candidate = None
|
| 752 |
-
|
| 753 |
-
#
|
| 754 |
-
elif
|
| 755 |
-
|
| 756 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 757 |
else:
|
| 758 |
candidate = None
|
|
|
|
| 759 |
|
| 760 |
# 최종 검증
|
| 761 |
if candidate and os.path.exists(candidate):
|
|
|
|
| 762 |
return candidate
|
| 763 |
else:
|
| 764 |
-
logger.error("FLUX
|
| 765 |
return None
|
| 766 |
|
| 767 |
except Exception as e:
|
| 768 |
-
logger.error(f"FLUX
|
|
|
|
|
|
|
| 769 |
return None
|
| 770 |
|
| 771 |
##############################################################################
|
|
|
|
| 719 |
|
| 720 |
def generate_flux_image_via_api(prompt: str) -> Optional[str]:
|
| 721 |
if not FLUX_API_ENABLED or not flux_api_client:
|
| 722 |
+
logger.error("FLUX API is not enabled or client not initialized")
|
| 723 |
return None
|
| 724 |
|
| 725 |
try:
|
| 726 |
+
logger.info(f"[FLUX] Calling API with prompt length: {len(prompt)}")
|
| 727 |
+
|
| 728 |
+
# 빈 이미지 데이터 생성 (필수 파라미터를 위한 더미 데이터)
|
| 729 |
+
dummy_image = {
|
| 730 |
+
"path": None,
|
| 731 |
+
"url": None,
|
| 732 |
+
"size": None,
|
| 733 |
+
"orig_name": None,
|
| 734 |
+
"mime_type": None,
|
| 735 |
+
"is_stream": False,
|
| 736 |
+
"meta": {}
|
| 737 |
+
}
|
| 738 |
+
|
| 739 |
result = flux_api_client.predict(
|
| 740 |
prompt=prompt,
|
| 741 |
width=768,
|
|
|
|
| 744 |
inference_steps=8,
|
| 745 |
seed=random.randint(1, 1_000_000),
|
| 746 |
do_img2img=False,
|
| 747 |
+
init_image=dummy_image, # ← 필수 파라미터 추가
|
| 748 |
+
image2image_strength=0.8, # 기본값
|
| 749 |
+
resize_img=True, # 기본값
|
| 750 |
+
api_name="/generate_image",
|
| 751 |
)
|
| 752 |
|
| 753 |
+
logger.info(f"[FLUX] API response type: {type(result)}")
|
| 754 |
+
logger.info(f"[FLUX] API response (first 200 chars): {str(result)[:200]}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 755 |
|
| 756 |
+
# 결과가 튜플인 경우 (generated_image, used_seed)
|
| 757 |
+
if isinstance(result, tuple) and len(result) >= 1:
|
| 758 |
+
generated_image = result[0]
|
| 759 |
+
|
| 760 |
+
# generated_image가 dict 형태인 경우
|
| 761 |
+
if isinstance(generated_image, dict) and 'path' in generated_image:
|
| 762 |
+
candidate = generated_image['path']
|
| 763 |
+
logger.info(f"[FLUX] Extracted path from result dict: {candidate}")
|
| 764 |
+
else:
|
| 765 |
+
candidate = generated_image
|
| 766 |
+
logger.info(f"[FLUX] Using first element of tuple: {candidate}")
|
| 767 |
+
|
| 768 |
+
# 결과가 dict인 경우
|
| 769 |
+
elif isinstance(result, dict):
|
| 770 |
+
if 'path' in result:
|
| 771 |
+
candidate = result['path']
|
| 772 |
+
elif 'url' in result:
|
| 773 |
+
candidate = _save_temp_image(requests.get(result['url']).content)
|
| 774 |
+
else:
|
| 775 |
candidate = None
|
| 776 |
+
|
| 777 |
+
# 문자열인 경우
|
| 778 |
+
elif isinstance(result, str):
|
| 779 |
+
if os.path.exists(result):
|
| 780 |
+
candidate = result
|
| 781 |
+
elif result.startswith(("http://", "https://")):
|
| 782 |
+
candidate = _save_temp_image(requests.get(result).content)
|
| 783 |
+
else:
|
| 784 |
+
candidate = None
|
| 785 |
+
|
| 786 |
else:
|
| 787 |
candidate = None
|
| 788 |
+
logger.error(f"[FLUX] Unexpected result type: {type(result)}")
|
| 789 |
|
| 790 |
# 최종 검증
|
| 791 |
if candidate and os.path.exists(candidate):
|
| 792 |
+
logger.info(f"[FLUX] ✅ Success! Image saved at: {candidate}")
|
| 793 |
return candidate
|
| 794 |
else:
|
| 795 |
+
logger.error(f"[FLUX] ❌ Failed to get valid image path. Candidate: {candidate}")
|
| 796 |
return None
|
| 797 |
|
| 798 |
except Exception as e:
|
| 799 |
+
logger.error(f"[FLUX] ❌ Image generation failed: {str(e)}")
|
| 800 |
+
import traceback
|
| 801 |
+
logger.error(f"[FLUX] Traceback: {traceback.format_exc()}")
|
| 802 |
return None
|
| 803 |
|
| 804 |
##############################################################################
|