openfree commited on
Commit
318e1b1
ยท
verified ยท
1 Parent(s): 33aa4f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -29
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, 1000000),
721
  do_img2img=False,
722
- init_image=None,
723
- image2image_strength=0.8,
724
- resize_img=True,
725
- api_name="/generate_image"
726
  )
727
-
728
- if isinstance(result, tuple) and len(result) > 0:
729
- image_path = result[0]
730
- if image_path and os.path.exists(image_path):
731
- # PNG๋กœ ๋ณ€ํ™˜
732
- with Image.open(image_path) as img:
733
- png_tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
734
- img.save(png_tmp.name, format="PNG")
735
- logger.info(f"FLUX image generated and saved to {png_tmp.name}")
736
- return png_tmp.name
737
-
738
- return None
739
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740
  except Exception as e:
741
- logger.error(f"Failed to generate FLUX image: {e}")
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
- finally:
1964
- # ์ž„์‹œ ํŒŒ์ผ ์ •๋ฆฌ
 
1965
  try:
1966
  os.unlink(selected_image)
1967
- except:
1968
- pass
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
  # ์‹œ๊ฐ์  ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ํ”Œ๋ ˆ์ด์Šคํ™€๋” ์ถ”๊ฐ€