imagetalking / app.py
englissi's picture
Update app.py
31447f5 verified
raw
history blame
1.26 kB
from PIL import Image
import traceback
def generate_caption(filepaths, choice_index):
try:
idx = int(choice_index)
img_path = filepaths[idx]
print(f"[DEBUG] Selected image path: {img_path}")
# 1) ์ด๋ฏธ์ง€ ๋กœ๋“œ
img = Image.open(img_path).convert("RGB")
print("[DEBUG] Image loaded")
# 2) ์บก์…˜ ์ƒ์„ฑ
out = captioner(img)
print(f"[DEBUG] captioner output: {out!r}")
first = out[0] if isinstance(out, list) else out
raw = first.get("generated_text") or first.get("text") or str(first)
raw = raw.strip()
print(f"[DEBUG] raw caption: {raw!r}")
# 3) ์žฅ๋ฉด ๋ถ„๋ฅ˜
cls = scene_classifier(img, candidate_labels=SCENE_LABELS)
print(f"[DEBUG] scene_classifier output: {cls!r}")
scene = cls["labels"][0]
# 4) ํ…œํ”Œ๋ฆฟ ๋งคํ•‘
template = TEMPLATES.get(scene, "In this picture, {caption}.")
result = template.format(caption=raw)
print(f"[DEBUG] Final result: {result}")
return result
except Exception as e:
# ์—๋Ÿฌ ๋ฉ”์‹œ์ง€์™€ ์Šคํƒ ํŠธ๋ ˆ์ด์Šค ๋ฆฌํ„ด
error_msg = f"๐Ÿ”ด Error:\n{e}\n\n{traceback.format_exc()}"
print(error_msg)
return error_msg