Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,6 +25,8 @@ import matplotlib as mpl
|
|
| 25 |
import matplotlib.font_manager as fm
|
| 26 |
import subprocess
|
| 27 |
|
|
|
|
|
|
|
| 28 |
NANUM_PATH = "/usr/share/fonts/truetype/nanum/NanumGothic.ttf" # ์์คํ
์ค์น ๊ฒฝ๋ก
|
| 29 |
|
| 30 |
# matplotlib์ฉ ๊ธ๊ผด ๋ฑ๋ก
|
|
@@ -703,42 +705,59 @@ def generate_flux_prompt(title: str, content: str, style_key: str) -> str:
|
|
| 703 |
|
| 704 |
|
| 705 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 706 |
def generate_flux_image_via_api(prompt: str) -> Optional[str]:
|
| 707 |
-
"""FLUX API๋ฅผ ํตํด ์ด๋ฏธ์ง ์์ฑ"""
|
| 708 |
if not FLUX_API_ENABLED or not flux_api_client:
|
| 709 |
return None
|
| 710 |
-
|
| 711 |
try:
|
| 712 |
-
logger.info(f"Generating FLUX image with prompt: {prompt[:100]}...")
|
| 713 |
-
|
| 714 |
result = flux_api_client.predict(
|
| 715 |
prompt=prompt,
|
| 716 |
width=768,
|
| 717 |
height=768,
|
| 718 |
guidance=3.5,
|
| 719 |
inference_steps=8,
|
| 720 |
-
seed=random.randint(1,
|
| 721 |
do_img2img=False,
|
| 722 |
-
|
| 723 |
-
image2image_strength=0.8,
|
| 724 |
-
resize_img=True,
|
| 725 |
-
api_name="/generate_image"
|
| 726 |
)
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 740 |
except Exception as e:
|
| 741 |
-
logger.error(f"
|
| 742 |
return None
|
| 743 |
|
| 744 |
##############################################################################
|
|
@@ -1951,8 +1970,9 @@ def create_advanced_ppt_from_content(
|
|
| 1951 |
|
| 1952 |
# ์ฌ๋ผ์ด๋์ ์ฝ์
|
| 1953 |
if selected_image and os.path.exists(selected_image):
|
|
|
|
| 1954 |
try:
|
| 1955 |
-
slide.shapes.add_picture(
|
| 1956 |
selected_image,
|
| 1957 |
Inches(5.2), Inches(1.5),
|
| 1958 |
width=Inches(4.3), height=Inches(3.0)
|
|
@@ -1960,13 +1980,13 @@ def create_advanced_ppt_from_content(
|
|
| 1960 |
visual_added = True
|
| 1961 |
except Exception as e:
|
| 1962 |
logger.error(f"Failed to add slide image: {e}")
|
| 1963 |
-
|
| 1964 |
-
|
|
|
|
| 1965 |
try:
|
| 1966 |
os.unlink(selected_image)
|
| 1967 |
-
except:
|
| 1968 |
-
|
| 1969 |
-
|
| 1970 |
|
| 1971 |
|
| 1972 |
# ์๊ฐ์ ์์๊ฐ ์ถ๊ฐ๋์ง ์์ ๊ฒฝ์ฐ ํ๋ ์ด์คํ๋ ์ถ๊ฐ
|
|
|
|
| 25 |
import matplotlib.font_manager as fm
|
| 26 |
import subprocess
|
| 27 |
|
| 28 |
+
import base64, io, requests
|
| 29 |
+
|
| 30 |
NANUM_PATH = "/usr/share/fonts/truetype/nanum/NanumGothic.ttf" # ์์คํ
์ค์น ๊ฒฝ๋ก
|
| 31 |
|
| 32 |
# matplotlib์ฉ ๊ธ๊ผด ๋ฑ๋ก
|
|
|
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
+
def _save_temp_image(img_bytes: bytes, suffix=".png") -> str:
|
| 709 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
|
| 710 |
+
tmp.write(img_bytes)
|
| 711 |
+
tmp.close()
|
| 712 |
+
return tmp.name
|
| 713 |
+
|
| 714 |
def generate_flux_image_via_api(prompt: str) -> Optional[str]:
|
|
|
|
| 715 |
if not FLUX_API_ENABLED or not flux_api_client:
|
| 716 |
return None
|
| 717 |
+
|
| 718 |
try:
|
|
|
|
|
|
|
| 719 |
result = flux_api_client.predict(
|
| 720 |
prompt=prompt,
|
| 721 |
width=768,
|
| 722 |
height=768,
|
| 723 |
guidance=3.5,
|
| 724 |
inference_steps=8,
|
| 725 |
+
seed=random.randint(1, 1_000_000),
|
| 726 |
do_img2img=False,
|
| 727 |
+
api_name="/generate_image", # โ ์๋ํฌ์ธํธ ํ์ธ ํ์!
|
|
|
|
|
|
|
|
|
|
| 728 |
)
|
| 729 |
+
|
| 730 |
+
# 1) tuple ํ์ (๊ธฐ์กด)
|
| 731 |
+
if isinstance(result, tuple) and result:
|
| 732 |
+
candidate = result[0]
|
| 733 |
+
|
| 734 |
+
# 2) ๋ฌธ์์ด URL
|
| 735 |
+
elif isinstance(result, str) and result.startswith(("http://", "https://")):
|
| 736 |
+
candidate = _save_temp_image(requests.get(result).content)
|
| 737 |
+
|
| 738 |
+
# 3) base64 ๋ฌธ์์ด
|
| 739 |
+
elif isinstance(result, str) and len(result) > 100:
|
| 740 |
+
try:
|
| 741 |
+
candidate = _save_temp_image(base64.b64decode(result.split(",")[-1]))
|
| 742 |
+
except Exception:
|
| 743 |
+
candidate = None
|
| 744 |
+
|
| 745 |
+
# 4) BytesIO
|
| 746 |
+
elif hasattr(result, "read"):
|
| 747 |
+
candidate = _save_temp_image(result.read())
|
| 748 |
+
|
| 749 |
+
else:
|
| 750 |
+
candidate = None
|
| 751 |
+
|
| 752 |
+
# ์ต์ข
๊ฒ์ฆ
|
| 753 |
+
if candidate and os.path.exists(candidate):
|
| 754 |
+
return candidate
|
| 755 |
+
else:
|
| 756 |
+
logger.error("FLUX ์ด๋ฏธ์ง ๋ฐํ๊ฐ์ ์ฒ๋ฆฌํ์ง ๋ชปํ์ต๋๋ค")
|
| 757 |
+
return None
|
| 758 |
+
|
| 759 |
except Exception as e:
|
| 760 |
+
logger.error(f"FLUX ์ด๋ฏธ์ง ์์ฑ ์คํจ: {e}")
|
| 761 |
return None
|
| 762 |
|
| 763 |
##############################################################################
|
|
|
|
| 1970 |
|
| 1971 |
# ์ฌ๋ผ์ด๋์ ์ฝ์
|
| 1972 |
if selected_image and os.path.exists(selected_image):
|
| 1973 |
+
|
| 1974 |
try:
|
| 1975 |
+
pic = slide.shapes.add_picture(
|
| 1976 |
selected_image,
|
| 1977 |
Inches(5.2), Inches(1.5),
|
| 1978 |
width=Inches(4.3), height=Inches(3.0)
|
|
|
|
| 1980 |
visual_added = True
|
| 1981 |
except Exception as e:
|
| 1982 |
logger.error(f"Failed to add slide image: {e}")
|
| 1983 |
+
|
| 1984 |
+
# ๊ทธ๋ฆผ์ ์ค์ ๋ก ๋ฃ์์ ๋๋ง ์ญ์
|
| 1985 |
+
if visual_added and selected_image and os.path.exists(selected_image):
|
| 1986 |
try:
|
| 1987 |
os.unlink(selected_image)
|
| 1988 |
+
except Exception as e:
|
| 1989 |
+
logger.warning(f"Temp image delete failed: {e}")
|
|
|
|
| 1990 |
|
| 1991 |
|
| 1992 |
# ์๊ฐ์ ์์๊ฐ ์ถ๊ฐ๋์ง ์์ ๊ฒฝ์ฐ ํ๋ ์ด์คํ๋ ์ถ๊ฐ
|