์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

๋ฏธ๋‹ˆ ํ”„๋กœ์ ํŠธ 2์ผ์ฐจ

์ˆ˜ํ˜€์ด0812 2024. 11. 4. 00:58
728x90
๋ฐ˜์‘ํ˜•

# ๋”ฅ๋Ÿฌ๋‹ ํ™˜๊ฒฝ ๊ตฌ์ถ• : CUDA, CuDNN

 

https://developer.nvidia.com/cuda-11.3.0-download-archive?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exe_local

 

 

https://developer.nvidia.com/cudnn-downloads?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exe_local

 

cuDNN 9.5.1 Downloads

 

developer.nvidia.com

 

https://velog.io/@euisuk-chung/%ED%99%98%EA%B2%BD%EA%B5%AC%EC%B6%95-CUDA-CuDNN%EC%95%84-%EB%82%98-%EC%A2%80-%EA%B7%B8%EB%A7%8C-%EA%B4%B4%EB%A1%AD%ED%98%80%EB%9D%BC-%E3%85%A0

 

[Linux] ๋”ฅ๋Ÿฌ๋‹ ํ™˜๊ฒฝ ๊ตฌ์ถ• : CUDA, CuDNN

๋”ฅ๋Ÿฌ๋‹ ํ™˜๊ฒฝ ๊ตฌ์ถ•, CUDA/CUDNN ์„ค์น˜ ๋” ์ด์ƒ ๊ณ ๋ฏผํ•˜์ง€ ๋ง์ž!

velog.io

 

 

# ํ…์ŠคํŠธ -> ์‚ฌ์ง„ (stabilityai/stable-diffusion-3.5-large)

 

๊ฐ€์ด๋“œ..

https://stabilityai.notion.site/Stable-Diffusion-3-5-Large-Fine-tuning-Tutorial-11a61cdcd1968027a15bdbd7c40be8c6

 

์‹คํŒจ ์ฝ”๋“œ...

# '''
# 5. ์Œ์•…, ๋ช…์–ธ, ๊ทธ๋ฆผ ์ถ”์ฒœ/์ƒ์„ฑ: (๋ชจ๋ธ ๋ฏธ์ •) :
#      (ํ›„๋ณด 1) facebook/musicgen-small 
#      ํ”„๋กฌํ”„ํ„ฐ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ์Œ์•…์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. 
#      ๋Œ€์‹  ์ด๊ฑด ์ €ํฌ๊ฐ€ ๋ฐฑ์—”๋“œ ์ชฝ์—์„œ ํ”„๋กฌํ”„ํ„ฐ๋ฅผ ๋ช‡๊ฐ€์ง€ ๋งŒ๋“ค์–ด์„œ ๋„ฃ์–ด์ค˜์•ผ ํ•  ๊ฒƒ ๊ฐ™์•„์š”.
# '''


from diffusers import StableDiffusionPipeline
from fastapi import FastAPI, Form
from PIL import Image
import torch
import json
import requests

# Hugging Face API ํ† ํฐ ์„ค์ •
API_TOKEN = "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"
headers = {"Authorization": f"Bearer {API_TOKEN}"}

# Stable Diffusion ๋ชจ๋ธ ๋กœ๋“œ
pipe = StableDiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-3.5-large",
    torch_dtype=torch.float16,
    use_auth_token=API_TOKEN  # API ํ† ํฐ์„ ์‚ฌ์šฉํ•ด ์ธ์ฆ
)
pipe = pipe.to("cuda")

# FastAPI ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
app = FastAPI()

# ์ด๋ฏธ์ง€ ์ƒ์„ฑ ์—”๋“œํฌ์ธํŠธ
@app.post("/createimage/")
async def create_image(text: str = Form(...)):
    """
    ์ฃผ์–ด์ง„ ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    """
    # ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฏธ์ง€ ์ƒ์„ฑ
    image = pipe(
        prompt=text,
        num_inference_steps=28,
        guidance_scale=3.5
    ).images[0]

    # ์ด๋ฏธ์ง€ ์ €์žฅ
    image_path = "generated_image.png"
    image.save(image_path)

    return {"image_path": image_path}



# from diffusers import BitsAndBytesConfig, StableDiffusionPipeline
# from fastapi import FastAPI, Form
# from PIL import Image
# import torch
# # from huggingface_hub import login

# # ์—‘์„ธ์Šค ํ† ํฐ
# # Hugging Face ๋กœ๊ทธ์ธ (ํ† ํฐ ํ•„์š”)
# # login("your_hugging_face_access_token")

# # ๋ชจ๋ธ ์„ค์ •
# model_id = "stabilityai/stable-diffusion-3.5-large, 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=access_token"
# nf4_config = BitsAndBytesConfig(
#     load_in_4bit=True,
#     bnb_4bit_quant_type="nf4",
#     bnb_4bit_compute_dtype=torch.bfloat16
# )

# # ๋ชจ๋ธ ๋กœ๋“œ
# pipe = StableDiffusionPipeline.from_pretrained(
#     model_id,
#     torch_dtype=torch.bfloat16,
#     quantization_config=nf4_config
# )
# pipe = pipe.to("cuda")
# pipe.enable_model_cpu_offload()

# # FastAPI ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
# app = FastAPI()


# # ์ด๋ฏธ์ง€ ์ƒ์„ฑ ์—”๋“œํฌ์ธํŠธ
# @app.post("/createimage/")
# async def create_image(text: str = Form(...)):
#     """
#     ์ฃผ์–ด์ง„ ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
#     """
#     # ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฏธ์ง€ ์ƒ์„ฑ
#     image = pipe(
#         prompt=text,
#         num_inference_steps=28,
#         guidance_scale=3.5
#     ).images[0]

#     # ์ด๋ฏธ์ง€ ์ €์žฅ ๊ฒฝ๋กœ
#     image_path = "generated_image.png"
#     image.save(image_path)

#     return {"image_path": image_path}

 

 

# ํ…์ŠคํŠธ -> ์˜ค๋””์˜ค (facebook/musicgen-small)

 

from fastapi import FastAPI, Form
from transformers import pipeline, AutoProcessor, MusicgenForConditionalGeneration
import scipy.io.wavfile
import torch
import tempfile
import os
import torch
# print(torch.cuda.is_available())  # True๊ฐ€ ์ถœ๋ ฅ๋˜๋ฉด GPU๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ์ž…๋‹ˆ๋‹ค.
# ๋ชจ๋ธ์ด GPU๋กœ ์ด๋™๋˜์—ˆ๋Š”์ง€ ํ™•์ธ

app = FastAPI()

# # MusicGen ๋ชจ๋ธ ์„ค์ •
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small", torch_dtype=torch.float32).to("cuda")
print(next(model.parameters()).device)  # "cuda:0"์ด ์ถœ๋ ฅ๋˜๋ฉด GPU์— ๋กœ๋“œ๋œ ๊ฒƒ

# ์Œ์•… ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ ํ•จ์ˆ˜
def generate_music(text: str, length: int = 512):
    # ํ…์ŠคํŠธ๋ฅผ ๋ชจ๋ธ์— ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜
    inputs = processor(text=[text], padding=True, return_tensors="pt").to("cuda")
    
    # ๋ชจ๋ธ์„ ํ†ตํ•ด ์˜ค๋””์˜ค ๊ฐ’ ์ƒ์„ฑ
    audio_values = model.generate(**inputs, max_new_tokens=length)
    
    # ์ƒ์„ฑ๋œ ์˜ค๋””์˜ค๋ฅผ WAV ํŒŒ์ผ๋กœ ์ž„์‹œ ์ €์žฅ
    sampling_rate = model.config.audio_encoder.sampling_rate
    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
        scipy.io.wavfile.write(tmp_file.name, rate=sampling_rate, data=audio_values[0, 0].cpu().numpy())
        temp_path = tmp_file.name
    
    return temp_path

# FastAPI ์—”๋“œํฌ์ธํŠธ: ์š”์•ฝ๋œ ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์Œ์•… ์ƒ์„ฑ
@app.post("/recommend_music/")
async def recommend_music(summary_text: str = Form(...), length: int = Form(512)):
    # ์Œ์•… ์ƒ์„ฑ
    music_path = generate_music(summary_text)
    
    # ์ƒ์„ฑ๋œ ์Œ์•… ํŒŒ์ผ ๊ฒฝ๋กœ ๋ฐ˜ํ™˜
    return {"music_path": music_path}

# FastAPI์—์„œ ์ •์  ํŒŒ์ผ๋กœ WAV ํŒŒ์ผ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ • (๊ฐ„๋‹จํ•œ ํ…Œ์ŠคํŠธ์šฉ)
@app.get("/download_music/")
async def download_music(music_path: str):
    if os.path.exists(music_path):
        with open(music_path, "rb") as f:
            data = f.read()
        return {"audio_data": data}
    else:
        return {"error": "File not found"}

 

728x90
๋ฐ˜์‘ํ˜•