-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from boostcampaitech5/develop
Develop
- Loading branch information
Showing
9 changed files
with
152 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Genre Translation | ||
|
||
|
||
def translate_genre_to_english(genre): | ||
# Define the translation dictionary | ||
genre_translation = { | ||
"발라드": "ballad", | ||
"댄스": "dance", | ||
"트로트": "trot", | ||
"랩/힙합": "rap&hiphop", | ||
"인디음악": "indie-music", | ||
"록/메탈": "rock&metal", | ||
"포크/블루스": "folk&blues" | ||
# Add more genre translations here if needed | ||
} | ||
|
||
# Use the get() method to handle cases where the genre is not in the dictionary | ||
return genre_translation.get(genre, genre) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,17 +48,23 @@ | |
login(token=huggingface_config["token"], add_to_git_credential=True) | ||
|
||
# Initialize Celery | ||
celery_app = Celery( | ||
"tasks", | ||
broker=redis_config["redis_server_ip"], | ||
backend=redis_config["redis_server_ip"], | ||
dream_app = Celery( | ||
"tasks_dream", | ||
broker="redis://kimseungki1011:[email protected]:6379/0", | ||
backend="redis://kimseungki1011:[email protected]:6379/1", | ||
timezone="Asia/Seoul", # Set the time zone to KST | ||
enable_utc=False, | ||
worker_heartbeat=280, | ||
) | ||
celery_app.conf.worker_pool = "solo" | ||
dream_app.conf.worker_pool = "solo" | ||
|
||
# Set Celery Time-zone | ||
dream_app.conf.timezone = "Asia/Seoul" | ||
|
||
device = "cuda" if cuda.is_available() else "cpu" | ||
|
||
|
||
@celery_app.task(name="save_image") | ||
@dream_app.task(name="save_image", queue="dreambooth") | ||
def save_image(filename, image_content, token): | ||
# Define the directory where to save the image | ||
image_dir = Path("src/scratch/dreambooth/data/users") / token | ||
|
@@ -75,7 +81,7 @@ def save_image(filename, image_content, token): | |
return {"image_url": str(image_dir / filename)} | ||
|
||
|
||
@celery_app.task(name="train_inference") | ||
@dream_app.task(name="train_inference", queue="dreambooth") | ||
def train_inference(input, token, request_id): | ||
try: | ||
global model | ||
|
@@ -198,4 +204,4 @@ def train_inference(input, token, request_id): | |
|
||
|
||
if __name__ == "__main__": | ||
celery_app.worker_main(["-l", "info"]) | ||
dream_app.worker_main(["-l", "info"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
from .gcp.cloud_storage import GCSUploader | ||
from .gcp.bigquery import BigQueryLogger | ||
from .utils import load_yaml | ||
from .translation import translate_genre_to_english | ||
|
||
|
||
# Load config | ||
|
@@ -39,12 +40,18 @@ | |
# Initialize Celery | ||
celery_app = Celery( | ||
"tasks", | ||
broker=redis_config["redis_server_ip"], | ||
backend=redis_config["redis_server_ip"], | ||
broker="redis://kimseungki1011:[email protected]:6379/0", | ||
backend="redis://kimseungki1011:[email protected]:6379/1", | ||
timezone="Asia/Seoul", # Set the time zone to KST | ||
enable_utc=False, | ||
worker_heartbeat=280, | ||
) | ||
|
||
celery_app.conf.worker_pool = "solo" | ||
|
||
# Set Celery Time-zone | ||
celery_app.conf.timezone = "Asia/Seoul" | ||
|
||
gcs_uploader = GCSUploader(gcp_config) | ||
bigquery_logger = BigQueryLogger(gcp_config) | ||
|
||
|
@@ -57,7 +64,7 @@ def setup_worker_init(*args, **kwargs): | |
model.get_model() | ||
|
||
|
||
@celery_app.task(name="generate_cover") | ||
@celery_app.task(name="generate_cover", queue="sdxl") | ||
def generate_cover(input, request_id): | ||
os.environ["CUDA_LAUNCH_BLOCKING"] = "1" | ||
device = "cuda" if cuda.is_available() else "cpu" | ||
|
@@ -85,20 +92,24 @@ def generate_cover(input, request_id): | |
input["song_name"], | ||
input["genre"], | ||
) | ||
prompt = f"A photo of a {input['genre']} album cover with a {vibe} atmosphere visualized and {summarization} on it" | ||
genre = translate_genre_to_english(input["genre"]) | ||
|
||
prompt = f"Korean music album photo of a {get_translation(input['artist_name'])} who sang {get_translation(input['song_name'])}, full body, on {summarization}, Bounced lighting, dutch angle, Aaton LTR" | ||
new_prompt = f"A photo or picture of a {genre} album cover that has a {vibe} vibe visualzied and has {summarization} on it" | ||
else: | ||
prompt = f"A photo of a {input['genre']} album cover with a {vibe} atmosphere visualized and {summarization} on it" | ||
prompt = f"Korean music album photo of a {get_translation(input['artist_name'])} who sang {get_translation(input['song_name'])}, full body, Bounced lighting, dutch angle, Aaton LTR" | ||
new_prompt = f"A photo or picture of a {genre} album cover that has a {vibe} vibe visualzied and has {summarization} on it" | ||
|
||
prompt = re.sub("\n", ", ", prompt) | ||
prompt = re.sub("[ㄱ-ㅎ가-힣]+", " ", prompt) | ||
prompt = re.sub("[()-]", " ", prompt) | ||
prompt = re.sub("\s+", " ", prompt) | ||
|
||
if len(prompt) <= 150: | ||
if len(prompt) <= 200: | ||
break | ||
|
||
if model is None: | ||
time.sleep(20) | ||
# if model is None: | ||
# time.sleep(20) | ||
|
||
seeds = np.random.randint( | ||
public_config["generate"]["max_seed"], size=public_config["generate"]["n_gen"] | ||
|
@@ -111,10 +122,10 @@ def generate_cover(input, request_id): | |
with torch.no_grad(): | ||
image = model.pipeline( | ||
prompt=prompt, | ||
prompt_2=negative_prompt, | ||
prompt_2=prompt, | ||
height=public_config["generate"]["height"], | ||
width=public_config["generate"]["width"], | ||
num_inference_steps=20, | ||
num_inference_steps=100, | ||
generator=generator, | ||
).images[0] | ||
|
||
|
@@ -132,6 +143,34 @@ def generate_cover(input, request_id): | |
) | ||
images.append(byte_arr) | ||
|
||
for i, seed in enumerate(seeds): | ||
generator = torch.Generator(device=device).manual_seed(int(seed)) | ||
|
||
# Generate Images | ||
with torch.no_grad(): | ||
image = model.pipeline( | ||
prompt=new_prompt, | ||
prompt_2=new_prompt, | ||
height=public_config["generate"]["height"], | ||
width=public_config["generate"]["width"], | ||
num_inference_steps=100, | ||
generator=generator, | ||
).images[0] | ||
|
||
# Convert to base64-encoded string | ||
byte_arr = io.BytesIO() | ||
image.save(byte_arr, format=public_config["generate"]["save_format"]) | ||
byte_arr = byte_arr.getvalue() | ||
|
||
# Upload to GCS | ||
urls.append( | ||
[ | ||
byte_arr, | ||
f"{request_id}_image_{i+2}.{public_config['generate']['save_format']}", | ||
] | ||
) | ||
images.append(byte_arr) | ||
|
||
# Upload to GCS | ||
image_urls = gcs_uploader.save_image_to_gcs(urls) | ||
|
||
|