edit : bing art 추가
This commit is contained in:
81
custom_apps/bingart/bingart.py
Normal file
81
custom_apps/bingart/bingart.py
Normal file
@@ -0,0 +1,81 @@
|
||||
import urllib.request
|
||||
import os
|
||||
from pathlib import Path
|
||||
from bingart import BingArt
|
||||
|
||||
from custom_apps.utils import cookie_manager
|
||||
from rest.app.utils.parsing_utils import prompt_to_filenames
|
||||
from rest.app.utils.date_utils import D
|
||||
from const import OUTPUT_FOLDER
|
||||
|
||||
|
||||
class BingArtGenerator:
|
||||
|
||||
model = 'dalle3'
|
||||
detail = 'art'
|
||||
output_folder = os.path.join(OUTPUT_FOLDER,"dalle","art")
|
||||
|
||||
def __init__(self):
|
||||
self.bing_art = BingArt(auth_cookie_U=cookie_manager.get_cookie())
|
||||
self.prompt = ''
|
||||
self.datetime = D.date_file_name()
|
||||
Path(self.output_folder).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def get_image_links(self, prompt, image_len:int):
|
||||
"""
|
||||
이미지 url값 생성
|
||||
"""
|
||||
results = None
|
||||
try:
|
||||
self.prompt = prompt_to_filenames(prompt)
|
||||
results = self.bing_art.generate_images(prompt)
|
||||
|
||||
finally:
|
||||
self.bing_art.close_session()
|
||||
|
||||
if results == None:
|
||||
raise Exception("result is null")
|
||||
|
||||
images = results.get("images")
|
||||
|
||||
if images != None:
|
||||
_temp = []
|
||||
for i in images:
|
||||
_temp.append(i.get('url'))
|
||||
images = _temp
|
||||
|
||||
#중복제거
|
||||
images = list(set(_temp))
|
||||
|
||||
if len(images) < image_len:
|
||||
return images[0:len(images)]
|
||||
else:
|
||||
return images[0:image_len]
|
||||
else:
|
||||
return None
|
||||
|
||||
def link_to_img(self, img_links:list):
|
||||
"""
|
||||
이미지 링크로 이미지 파일 저장
|
||||
"""
|
||||
jpeg_index = 1
|
||||
|
||||
if img_links == None:
|
||||
raise Exception("result is null")
|
||||
|
||||
for i in img_links:
|
||||
path= f"{self.model}_{self.detail}_{self.prompt}_{jpeg_index}_{self.datetime}.png"
|
||||
|
||||
urllib.request.urlretrieve(i, os.path.join(self.output_folder,path))
|
||||
jpeg_index += 1
|
||||
|
||||
def get_images(self, prompt, image_len):
|
||||
image_links = self.get_image_links(prompt,image_len)
|
||||
|
||||
if image_links == None:
|
||||
return 0
|
||||
|
||||
else:
|
||||
self.link_to_img(img_links=image_links)
|
||||
return len(image_links)
|
||||
|
||||
@@ -1,28 +1,11 @@
|
||||
import nest_asyncio
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from custom_apps.dalle3.BingImageCreator import *
|
||||
from custom_apps.bingimagecreator.BingImageCreator import *
|
||||
from const import OUTPUT_FOLDER
|
||||
|
||||
|
||||
class CookieManager:
|
||||
|
||||
DEFAULT_COOKIE = "15yugVy08XGWEWtpv2SW7-mG_0HsBxbDyBFQDdnKEEK6c-XkjYt4HOk6G_5wY4npT0yKB9yEbl76i7RB5CM_HocUZ-nIOIseVMFVPkg-aTeA82BrEjVSh6ohZaz3rUk9Lw0NpDCV60Mn8s4nyQo8vSZJDlsqVEYXklSyKbEbnrtLPNcXUY5gl9Fmjz5Lxr1CMiBr8ogt3UvmWUBYDA-b4-6SEranbD_2wY_KSVN0djJg"
|
||||
|
||||
def __init__(self):
|
||||
self.cookie = self.DEFAULT_COOKIE
|
||||
|
||||
def get_cookie(self):
|
||||
if not self.cookie:
|
||||
raise
|
||||
else:
|
||||
return self.cookie
|
||||
|
||||
def set_cookie(self,new_cookie):
|
||||
self.cookie = new_cookie
|
||||
|
||||
cookie_manager = CookieManager()
|
||||
from custom_apps.utils import cookie_manager
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -41,7 +24,7 @@ class DallEArgument:
|
||||
U = cookie_manager.get_cookie()
|
||||
prompt: str
|
||||
cookie_file: str|None = None
|
||||
output_dir: str = os.path.join(OUTPUT_FOLDER,"dalle")
|
||||
output_dir: str = os.path.join(OUTPUT_FOLDER,"dalle","img")
|
||||
download_count: int = 1
|
||||
debug_file: str|None = None
|
||||
quiet: bool = False
|
||||
@@ -81,8 +64,7 @@ def dalle3_generate_image(args):
|
||||
|
||||
counter = Counter()
|
||||
|
||||
if not os.path.isdir(args.output_dir):
|
||||
raise FileExistsError(f"FileExistsError: {args.output_dir}")
|
||||
Path(args.output_dir).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
if args.version:
|
||||
print(pkg_resources.get_distribution("BingImageCreator").version)
|
||||
1
custom_apps/dalle3/const.py
Normal file
1
custom_apps/dalle3/const.py
Normal file
@@ -0,0 +1 @@
|
||||
API_KEY = "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"
|
||||
20
custom_apps/dalle3/main.py
Normal file
20
custom_apps/dalle3/main.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import os
|
||||
from openai import OpenAI
|
||||
# from custom_apps.dalle3.const import API_KEY
|
||||
|
||||
#set api key
|
||||
# os.environ['OPENAI_API_KEY'] = "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"
|
||||
|
||||
os.environ['OPENAI_API_KEY'] = "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"
|
||||
|
||||
client = OpenAI()
|
||||
|
||||
response = client.images.generate(
|
||||
model="dall-e-2",
|
||||
prompt="a white siamese cat",
|
||||
size="512x512",
|
||||
quality="standard",
|
||||
n=1,
|
||||
)
|
||||
|
||||
print(response.data[0].url)
|
||||
17
custom_apps/utils.py
Normal file
17
custom_apps/utils.py
Normal file
@@ -0,0 +1,17 @@
|
||||
class CookieManager:
|
||||
|
||||
DEFAULT_COOKIE = "19S_ux18UhHzxHMsY5gTYwqk2YjYahwxssgJyx0AybuAjDa_kZKWFauqMSrtb1a80s89VjmLwKWIGhvpZxLOzkwcMXAuShkgFwGSVlD8ayI7qgQiCabE9-UFByw4QJ_ZSAnOnskn5iPydk4vaZZEayTR--u7-mVglsaANK6rGOQPeu8q-Sa6cjVCUQR9kkjEtz-J4wf2MQ6inXuC41IbCi8QwmZyHkwxOy6U6CqPiREg"
|
||||
|
||||
def __init__(self):
|
||||
self.cookie = self.DEFAULT_COOKIE
|
||||
|
||||
def get_cookie(self):
|
||||
if not self.cookie:
|
||||
raise
|
||||
else:
|
||||
return self.cookie
|
||||
|
||||
def set_cookie(self,new_cookie):
|
||||
self.cookie = new_cookie
|
||||
|
||||
cookie_manager = CookieManager()
|
||||
Reference in New Issue
Block a user