Update app.py
Browse files
app.py
CHANGED
|
@@ -5,15 +5,16 @@ import openai
|
|
| 5 |
import os
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
| 8 |
-
from PIL import Image, ImageEnhance
|
| 9 |
from gradio_imageslider import ImageSlider
|
|
|
|
| 10 |
|
| 11 |
######################################
|
| 12 |
# OpenAI API ํด๋ผ์ด์ธํธ ์ค์ (๋ธ๋ก๊ทธ ์์ฑ๊ธฐ์ฉ)
|
| 13 |
######################################
|
| 14 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
if not openai.api_key:
|
| 16 |
-
raise ValueError("OpenAI API ํ ํฐ(OPENAI_API_KEY)
|
| 17 |
|
| 18 |
def call_openai_api(content: str, system_message: str, max_tokens: int, temperature: float, top_p: float) -> str:
|
| 19 |
"""
|
|
@@ -211,7 +212,7 @@ def get_filter_description(filter_type):
|
|
| 211 |
return descriptions.get(filter_type, "")
|
| 212 |
|
| 213 |
######################################
|
| 214 |
-
# GIF ์์ฑ ๊ธฐ๋ฅ
|
| 215 |
######################################
|
| 216 |
def generate_gif(images, duration):
|
| 217 |
"""
|
|
@@ -222,17 +223,15 @@ def generate_gif(images, duration):
|
|
| 222 |
"""
|
| 223 |
if not images:
|
| 224 |
return "์ด๋ฏธ์ง๊ฐ ์ ๊ณต๋์ง ์์์ต๋๋ค."
|
| 225 |
-
# ๋จ์ผ ์ด๋ฏธ์ง์ธ ๊ฒฝ์ฐ ๋ฆฌ์คํธ๋ก ๋ณํ
|
| 226 |
if not isinstance(images, list):
|
| 227 |
images = [images]
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
output_path = "generated.gif"
|
| 235 |
-
|
| 236 |
return output_path
|
| 237 |
|
| 238 |
######################################
|
|
|
|
| 5 |
import os
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
| 8 |
+
from PIL import Image, ImageEnhance
|
| 9 |
from gradio_imageslider import ImageSlider
|
| 10 |
+
from moviepy.editor import ImageSequenceClip # moviepy ์ถ๊ฐ
|
| 11 |
|
| 12 |
######################################
|
| 13 |
# OpenAI API ํด๋ผ์ด์ธํธ ์ค์ (๋ธ๋ก๊ทธ ์์ฑ๊ธฐ์ฉ)
|
| 14 |
######################################
|
| 15 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 16 |
if not openai.api_key:
|
| 17 |
+
raise ValueError("OpenAI API ํ ํฐ(OPENAI_API_KEY)๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 18 |
|
| 19 |
def call_openai_api(content: str, system_message: str, max_tokens: int, temperature: float, top_p: float) -> str:
|
| 20 |
"""
|
|
|
|
| 212 |
return descriptions.get(filter_type, "")
|
| 213 |
|
| 214 |
######################################
|
| 215 |
+
# GIF ์์ฑ ๊ธฐ๋ฅ (moviepy ์ฌ์ฉ)
|
| 216 |
######################################
|
| 217 |
def generate_gif(images, duration):
|
| 218 |
"""
|
|
|
|
| 223 |
"""
|
| 224 |
if not images:
|
| 225 |
return "์ด๋ฏธ์ง๊ฐ ์ ๊ณต๋์ง ์์์ต๋๋ค."
|
|
|
|
| 226 |
if not isinstance(images, list):
|
| 227 |
images = [images]
|
| 228 |
+
# ๋ชจ๋ ์ด๋ฏธ์ง๋ฅผ RGB ๋ชจ๋์ numpy ๋ฐฐ์ด๋ก ๋ณํ
|
| 229 |
+
frames = [np.array(img.convert("RGB")) for img in images]
|
| 230 |
+
# fps๋ ํ๋ ์ ์ง์์๊ฐ(duration)์ ์ด์ฉํด ๊ณ์ฐ (fps = 1000 / duration)
|
| 231 |
+
fps = 1000 / duration if duration > 0 else 1
|
| 232 |
+
clip = ImageSequenceClip(frames, fps=fps)
|
|
|
|
| 233 |
output_path = "generated.gif"
|
| 234 |
+
clip.write_gif(output_path, fps=fps)
|
| 235 |
return output_path
|
| 236 |
|
| 237 |
######################################
|