728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

https://school.programmers.co.kr/learn/courses/30/lessons/43165

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

 

๋‚ด ํ’€๐Ÿฆท

class Solution {
    public int solution(int[] numbers, int target) {
        return dfs(numbers, target, 0, 0);
    }
    
    private int dfs(int[] numbers, int target, int index, int sum) {
        // ๋ชจ๋“  ์ˆซ์ž๋ฅผ ๋‹ค ์‚ฌ์šฉํ–ˆ์„ ๋–„
        if(index == numbers.length) {
            // ๋ชฉํ‘œ ์ˆซ์ž์™€ ํ•ฉ์ด ๊ฐ™์œผ๋ฉด ๊ฒฝ์šฐ์˜ ์ˆ˜ 1 ์ฆ๊ฐ€
            return sum == target ? 1 : 0;
        }
        // ํ˜„์žฌ ์ˆซ์ž๋ฅผ ๋”ํ•˜๋Š” ๊ฒฝ์šฐ์™€ ๋นผ๋Š” ๊ฒฝ์šฐ๋กœ ์žฌ๊ท€ ํ˜ธ์ถฉ..
        return dfs(numbers, target, index + 1, sum + numbers[index]) + dfs(numbers, target, index + 1, sum - numbers[index]);
    }
}

 

์ด๋ฏธ ํ•œ๋ฒˆ ํ’€์—ˆ๋˜ dfs ๋ฌธ์ œ!!

๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ๋ฅผ ๋‹ค ํƒ์ƒ‰ํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์„ ์ฐพ์•„์•ผํ•˜๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ dfs๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค

if๋ฌธ์„ ์“ฐ๋Š”๊ฒƒ๊นŒ์ง€๋Š” ์‰ฌ์šด๋ฐ ๋งˆ์ง€๋ง‰์— ์žฌ๊ท€๋ฅผ ์–ด๋–ป๊ฒŒ ์‹œ์ผœ์•ผํ•˜๋Š”์ง€๋Š” ํ•ญ์ƒ ๊ณ ๋ฏผ๋˜๋Š” ๋ถ€๋ถ„์ธ๊ฑฐ ๊ฐ™๋‹ค..

ํ•˜์ง€๋งŒ ์ด๋ฒˆ๋ฌธ์ œ๋Š” ์ˆ˜์›”ํ•˜๊ฒŒ ํ†ต๊ณผ~~

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

 

 DFS๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ํšจ๊ณผ์ ์ธ ๋ฌธ์ œ ์œ ํ˜•

  1. ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์„ ์ฐพ๋Š” ๊ฒฝ์šฐ : ์ฃผ์–ด์ง„ ์ˆซ์ž ๋ฐฐ์—ด์„ ๋”ํ•˜๊ณ  ๋นผ์„œ ํŠน์ • ๊ฐ’์„ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ
  2. ํ•ด๋‹ค ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒฝ์šฐ : ๋ฏธ๋กœ์—์„œ ์ถœ๊ตฌ๊นŒ์ง€ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•  ๋•Œ
  3. ์กฐํ•ฉ๊ณผ ์ˆœ์—ด์„ ์ƒ์„ฑํ•˜๋Š” ๋ฌธ์ œ : ํŠน์ • ๊ธธ์ด์˜ ์กฐํ•ฉ์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜, ์ˆœ์—ด์„ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ
  4. ๋ฐฑํŠธ๋ž˜ํ‚น๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ : N-Queens ๋ฌธ์ œ์ฒ˜๋Ÿผ ์ œํ•œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ์„ ๋•Œ

 

DFS ์‚ฌ์šฉ ์‹œ ์ฃผ์˜์‚ฌํ•ญ

- ๋ฌดํ•œ ๋ฃจํ”„ ์ฃผ์˜ : ์žฌ๊ท€ ํ˜ธ์ถœ์—์„œ ์ข…๋ฃŒ ์กฐ๊ฑด์„ ๋ช…ํ™•ํžˆ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค

- ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ : ๊นŠ์ด๊ฐ€ ๊นŠ์–ด์งˆ ๊ฒฝ์šฐ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ปค์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋„ˆ๋ฌด ๊นŠ์€ ์žฌ๊ท€ ํ˜ธ์ถœ์ด ํ•„์š”ํ•œ ๋ฌธ์ œ์—์„œ๋Š” ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ์— ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค

- ํƒ์ƒ‰ ์ตœ์ ํ™” : ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ๊ฒฝ๋กœ๋Š” ์กฐ๊ธฐ์— ์ค‘๋‹จํ•˜์—ฌ ํšจ์œจ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

 

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

https://school.programmers.co.kr/learn/courses/30/lessons/42748

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

 

๋‚ด ํ’€๐Ÿฆท

import java.util.*;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        
        for(int i = 0; i < commands.length; i++) { 
            int start = commands[i][0] -1;
            int end = commands[i][1];
            int k = commands[i][2] - 1;
            
            // ๋ฐฐ์—ด์˜ ํŠน์ • ๊ตฌ๊ฐ„์„ ๋ณต์‚ฌํ•˜๊ณ  ์ •๋ ฌ
            int[] arr = Arrays.copyOfRange(array, start, end);
            Arrays.sort(arr);
            
            // k๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์— ์ถ”๊ฐ€
            answer[i] = arr[k];
            
            }
        return answer;
    }
}

 

์˜ค๋žœ๋งŒ์— ํ’€์–ด๋ณด๋Š” ์ •๋ ฌ๋ฌธ์ œ๋ผ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ง€๋งŒ ๋‹นํ™ฉํ–ˆ๋‹ค ใ…Žใ…Ž

์–ด๋–ป๊ฒŒ ๋ฐฐ์—ด์„ ๊ฐ€์ ธ์˜ค๋Š”๊ฒƒ์ด ์ข‹์„์ง€ for๋ฌธ ๋Œ๋ฉด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ๋„ฃ์–ด์ค„๊นŒํ–ˆ๋Š”๋ฐ ์ฐพ์•„๋ณด๋‹ˆ copyOfRange๋ผ๋Š” ๋ฐฐ์—ด ๋ณต์‚ฌ ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•˜์—ฌ ์‚ฌ์šฉํ•ด์„œ ์“ฐ๋‹ˆ๊นŒ ๊ธˆ๋ฐฉ ํ•ด๊ฒฐ !!!

copyOfRange ๊ผญ ๊ธฐ์–ตํ•ด ๋‘๊ฒ ์Œ~

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

Arrays.copyOfRange(์›๋ณธ๋ฐฐ์—ด, ์‹œ์ž‘์ธ๋ฑ์Šค, ๋์ธ๋ฑ์Šค);

์›๋ณธ๋ฐฐ์—ด : ๋ณต์‚ฌํ•˜๋ ค๋Š” ๋ฐฐ์—ด
์‹œ์ž‘ ์ธ๋ฑ์Šค : ๋ณต์‚ฌ๋ฅผ ์‹œ์ž‘ํ•  ์ธ๋ฑ์Šค(ํฌํ•จ)
๋์ธ๋ฑ์Šค : ๋ณต์‚ฌ๋ฅผ ์ข…๋ฃŒํ•  ์ธ๋ฑ์Šค(๋ฏธํฌํ•จ)

int[] array = {1, 5, 2, 6, 3, 7, 4};
int[] slicedArray = Arrays.copyOfRange(array, 1, 5); // {5, 2, 6, 3}
728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

# 1. ์Œ์„ฑ ํŒŒ์ผ ๋ณ€ํ™˜์— ํ•„์š”ํ•œ ffmpeg ๋‹ค์šด ๋ฐ›๊ธฐ!

 

https://yeko90.tistory.com/entry/FFmpeg-%EB%8B%A4%EC%9A%B4%EB%A1%9C%EB%93%9C-%EC%84%A4%EC%B9%98-%EC%84%B8%ED%8C%85

 

FFmpeg ๋‹ค์šด๋กœ๋“œ ์„ค์น˜ / ํ•„์ˆ˜ ๊ธฐ๋ณธ ์„ธํŒ… ๋ฐฉ๋ฒ•

์—ฌ๋Ÿฌ๋ถ„ ํ˜น์‹œ FFmpeg์— ๋Œ€ํ•ด ๋“ค์–ด๋ณด์‹  ์  ์žˆ๋‚˜์š”?FFmpeg ์‚ฌ์šฉ๋ฒ•๋งŒ ์ตํžŒ๋‹ค๋ฉด ๋‹ค์–‘ํ•œ ํฌ๋งท์˜ ๋น„๋””์˜ค์™€ ์˜ค๋””์˜คํŒŒ์ผ์„ ์†์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  ์‹ฌ์ง€์–ด ๋ฌด๋ฃŒ๊ตฌ์š”!๋งŽ์€ ์œ /๋ฌด๋ฃŒ ์†Œํ”„ํŠธ์›จ์–ด

yeko90.tistory.com

 

๋‹ค์šด๋กœ๋“œ ํ•˜๊ณ  ๋„ˆ๋ฌด ์–ต๊นŒ ๋‹นํ–ˆ์ง€๋งŒ.. ์ปดํ“จํ„ฐ ๋ฆฌ๋ถ€ํŒ… ํ•ด์ฃผ๋‹ˆ๊นŒ ํ•ด๊ฒฐ ^^;;;; ์™„์ „ ๋Ÿฌํ‚ค๋น„ํ‚ค๋‹ค

 

 

# 2. ์ฒ˜์Œ ๋ณด๋Š” ์—๋Ÿฌ ์ •๋ฆฌ

 

libiomp5md.dll ํŒŒ์ผ์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์ดˆ๊ธฐํ™”๋˜์–ด ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๋Š” OpenMP๋ผ๋Š” ๋ณ‘๋ ฌ ์ปดํ“จํŒ… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋กœ๋“œ๋˜๋ฉด์„œ ์ƒ๊ธฐ๋Š” ์ถฉ๋Œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋Š” ์ฃผ๋กœ PyTorch๋‚˜ NumPy ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ ๋งํ•ด์„œ, ํ”„๋กœ๊ทธ๋žจ์—์„œ OpenMP๋ฅผ ๋‘ ๋ฒˆ ์ด์ƒ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ๋˜์–ด ์ถฉ๋Œ์ด ์ƒ๊ธด ๊ฒ๋‹ˆ๋‹ค.

์ด ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์—ฌ OpenMP๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋กœ๋“œ๋˜๋”๋ผ๋„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด KMP_DUPLICATE_LIB_OK๋ผ๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ TRUE๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• (Windows)

1. ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์—์„œ ์ผ์‹œ์ ์œผ๋กœ ์„ค์ •ํ•˜๊ธฐ

์ด ๋ฐฉ๋ฒ•์€ ํ˜„์žฌ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ ์„ธ์…˜์—์„œ๋งŒ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜๋ฉฐ, ์ปดํ“จํ„ฐ๋ฅผ ์žฌ๋ถ€ํŒ…ํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ๋ฅผ ์—ด๋ฉด ์ด ์„ค์ •์€ ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค.

  1. ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ๋ฅผ ์—ด๊ณ , ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค:
  2. bash
  3. ์ด ์ƒํƒœ์—์„œ ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•ด ์˜ค๋ฅ˜๊ฐ€ ํ•ด๊ฒฐ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์„ธ์š”.
set KMP_DUPLICATE_LIB_OK=TRUE

2. Python ์ฝ”๋“œ ๋‚ด์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •ํ•˜๊ธฐ

Python ์ฝ”๋“œ ์•ˆ์—์„œ ์ง์ ‘ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋ฉด, ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋™์•ˆ๋งŒ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

  1. import ๋ฌธ ์•„๋ž˜์— ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค:
  2. python
     
  3. ์ด ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ ํ›„์— ๋‹ค์‹œ ์‹คํ–‰ํ•ด ๋ณด์„ธ์š”. Python์ด ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์ด ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.
import os os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"

3. ์‹œ์Šคํ…œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ์˜๊ตฌ ์„ค์ •ํ•˜๊ธฐ (Windows)

์ด ๋ฐฉ๋ฒ•์€ ์ปดํ“จํ„ฐ์˜ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— KMP_DUPLICATE_LIB_OK๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ, ๋ชจ๋“  ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์™€ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ด ์„ค์ •์ด ์œ ์ง€๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

  1. Windows ๊ฒ€์ƒ‰์ฐฝ์— "ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ํŽธ์ง‘"์ด๋ผ๊ณ  ์ž…๋ ฅํ•˜๊ณ  ์‹œ์Šคํ…œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ํŽธ์ง‘์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
  2. ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ฐฝ์—์„œ ์•„๋ž˜ ๋‹จ๊ณ„๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค:
    • ์‹œ์Šคํ…œ ๋ณ€์ˆ˜ ๋ถ€๋ถ„์—์„œ "์ƒˆ๋กœ ๋งŒ๋“ค๊ธฐ" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.
    • ๋ณ€์ˆ˜ ์ด๋ฆ„์— KMP_DUPLICATE_LIB_OK์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.
    • ๋ณ€์ˆ˜ ๊ฐ’์— TRUE๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.
  3. ํ™•์ธ์„ ๋ˆŒ๋Ÿฌ ๋ชจ๋“  ์ฐฝ์„ ๋‹ซ๊ณ  ์ปดํ“จํ„ฐ๋ฅผ ์žฌ๋ถ€ํŒ…ํ•˜๋ฉด, ์ด ์„ค์ •์ด ๋ชจ๋“  ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์™€ ํ”„๋กœ๊ทธ๋žจ์— ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

 

# 3. MongoDB Atlas Cloud Database ์„ค์น˜ํ›„์— fastapi์™€ ์—ฐ๊ฒฐํ•˜๋Š” ์ฝ”๋“œ

 

from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
import os
from dotenv import load_dotenv
from datetime import datetime


# .env ํŒŒ์ผ ๋กœ๋“œ
load_dotenv()

uri = os.getenv("MONGO_URI")

# Create a new client and connect to the server
client = MongoClient(uri, server_api=ServerApi('1'))

# ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฐ ์ปฌ๋ ‰์…˜ ์„ ํƒ
db = client["maeubom"]
collection = db["maeubom"]

# ์‚ฝ์ž…ํ•  ๋ฐ์ดํ„ฐ์˜ ์†์„ฑ ์ •์˜
# ๊ฐ ์ปฌ๋ ‰์…˜ ์ •์˜
emotion_analysis_col = db["EmotionAnalysis"]
summary_col = db["Summary"]
generated_image_col = db["GeneratedImage"]
generated_music_col = db["GeneratedMusic"]
quote_col = db["Quote"]
analysis_session_col = db["AnalysisSession"]

def get_database():
    return db

'''
# ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ database.py์˜ db ์—ฐ๊ฒฐ ๊ฐ€์ ธ์˜ค๊ธฐ
from database import get_database

db = get_database()  # ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ
collection = db["maeumbom"]  # ์ปฌ๋ ‰์…˜ ์„ ํƒ
'''

 

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

 

# ํ…์ŠคํŠธ ์š”์•ฝ ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ 

 

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import nltk
from fastapi import FastAPI, Form
import torch

# NLTK ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ
nltk.download('punkt')

# ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ € ์„ค์ •
model_dir = "lcw99/t5-base-korean-text-summary"
tokenizer = AutoTokenizer.from_pretrained(model_dir)
model = AutoModelForSeq2SeqLM.from_pretrained(model_dir)

# CUDA ์žฅ์น˜ ์„ค์ •
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)  # ๋ชจ๋ธ์„ CUDA๋กœ ์ด๋™

app = FastAPI()

@app.post("/text_sum/")
async def text_sum(input_text: str = Form(...)):
    max_input_length = 512

    # ์ž…๋ ฅ ํ…์ŠคํŠธ ์„ค์ • ๋ฐ ์ „์ฒ˜๋ฆฌ
    text = input_text
    inputs = ["summarize: " + text]
    inputs = tokenizer(inputs, max_length=max_input_length, truncation=True, return_tensors="pt").to(device)  # ์ž…๋ ฅ์„ CUDA๋กœ ์ด๋™

    # ์š”์•ฝ ์ƒ์„ฑ
    with torch.no_grad():
        output = model.generate(**inputs, num_beams=8, do_sample=True, min_length=10, max_length=100)

    # ์š”์•ฝ๋œ ํ…์ŠคํŠธ ๋””์ฝ”๋”ฉ ๋ฐ ๋ฌธ์žฅํ™”
    decoded_output = tokenizer.batch_decode(output, skip_special_tokens=True)[0]
    predicted_summary = nltk.sent_tokenize(decoded_output.strip())[0]

    return {"result": predicted_summary}

 

 

# ๋ช…์–ธ ์ถ”์ฒœ ๋ชจ๋ธ ์‚ฌ์šฉ ๋ชปํ•˜๊ณ  ๋žœ๋ค ๋Œ๋ฆฌ๊ธฐ;;;

 

from transformers import OPTForCausalLM, GPT2Tokenizer
from fastapi import FastAPI, Form
import torch
import random

# ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ € ์„ค์ •
model = OPTForCausalLM.from_pretrained("facebook/opt-350m", torch_dtype=torch.float16)
tokenizer = GPT2Tokenizer.from_pretrained("facebook/opt-350m")

# ๋ชจ๋ธ์ด ์‚ฌ์šฉํ•  ์žฅ์น˜ ์„ค์ •
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

# FastAPI ์•ฑ ์ดˆ๊ธฐํ™”
app = FastAPI()

# ๋ช…์–ธ ํŒŒ์ผ์—์„œ ๋ช…์–ธ์„ ์ฝ์–ด ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ
def load_quotes(file_path="wise_saying.txt"):
    with open(file_path, "r", encoding="utf-8") as file:
        return [line.strip() for line in file.readlines() if line.strip()]

quotes = load_quotes()  # wise_saying.txt์—์„œ ๋ช…์–ธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

# FastAPI ์—”๋“œํฌ์ธํŠธ: ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ช…์–ธ ์ƒ์„ฑ
@app.post("/create_text/")
async def create_text(input_text: str = Form(...)):
    # ๋ช…์–ธ ๋ฆฌ์ŠคํŠธ์—์„œ ๋ฌด์ž‘์œ„๋กœ ํ•˜๋‚˜ ์„ ํƒ
    selected_quote = random.choice(quotes)
    selected_quote = selected_quote.split('.', 1)[-1].strip()
    return {"result": selected_quote}


# # ์ž…๋ ฅ ํ…์ŠคํŠธ์™€ ๊ฐ€์žฅ ๊ด€๋ จ ์žˆ๋Š” ๋ช…์–ธ์„ ์ฐพ๋Š” ํ•จ์ˆ˜
# def get_best_quote(input_text, quotes):
#     best_quote = ""
#     highest_score = -float("inf")  # ์ดˆ๊ธฐ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ์Œ์˜ ๋ฌดํ•œ๋Œ€๋กœ ์„ค์ •

#     for quote in quotes:
#         prompt = f"๋‹ค์Œ ์ž…๋ ฅ ๋ฌธ๊ตฌ์™€ ๊ฐ€์žฅ ์ž˜ ๋งž๋Š” ๋ช…์–ธ์„ ๊ณ ๋ฅด์‹œ์˜ค: '{input_text}'\n๋ช…์–ธ: '{quote}'"
        
#         # ์ž…๋ ฅ ํ…์ŠคํŠธ์™€ ๋ช…์–ธ์„ ํ•จ๊ป˜ ํ† ํฐํ™”ํ•˜์—ฌ ๋ชจ๋ธ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ
#         inputs = tokenizer(prompt, return_tensors="pt").to(device)
#         with torch.no_grad():
#             # ํ…์ŠคํŠธ์™€ ๋ช…์–ธ์˜ ๊ด€๋ จ์„ฑ์„ ํ‰๊ฐ€ํ•˜๋„๋ก ๋ชจ๋ธ ์‚ฌ์šฉ
#             output = model(**inputs).logits.mean().item()
        
#         # ๋ชจ๋ธ ์ถœ๋ ฅ์ด ๋†’์„์ˆ˜๋ก ๋†’์€ ์—ฐ๊ด€์„ฑ์œผ๋กœ ๊ฐ„์ฃผ
#         if output > highest_score:
#             highest_score = output
#             best_quote = quote

#     return best_quote

# # FastAPI ์—”๋“œํฌ์ธํŠธ: ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐ€์žฅ ์ ํ•ฉํ•œ ๋ช…์–ธ ์„ ํƒ
# @app.post("/create_text/")
# async def create_text(input_text: str = Form(...)):
#     # ์ž…๋ ฅ ํ…์ŠคํŠธ์™€ ๊ฐ€์žฅ ๊ด€๋ จ ์žˆ๋Š” ๋ช…์–ธ์„ ์ฐพ์Œ
#     selected_quote = get_best_quote(input_text, quotes)
#     selected_quote = selected_quote.split('.', 1)[-1].strip()
#     return {"result": selected_quote}

 

 

# ํ…์ŠคํŠธ ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋‹ค์‹œ ํ•ด๋ณด๊ธฐ..

 

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

model_id = "CompVis/stable-diffusion-v1-4"
device = "cuda"

pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to(device)


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

# ์ด๋ฏธ์ง€ ์ƒ์„ฑ ์—”๋“œํฌ์ธํŠธ
@app.post("/createimage/")
async def create_image(input_text: str = Form(...)):       

    # ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฏธ์ง€ ์ƒ์„ฑ
    image = pipe(
        prompt=input_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}
    return FileResponse(image_path, media_type="image/png")




# ์‹œ๊ฐ„ ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ ค์„œ ์ค„์ด๋Š” ์ค‘...

from diffusers import StableDiffusionPipeline
from fastapi import FastAPI, Form
from fastapi.responses import FileResponse
import torch

model_id = "runwayml/stable-diffusion-v1-5"
device = "cuda"

# Stable Diffusion Pipeline ์„ค์ • (FP16 ์‚ฌ์šฉ)
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to(device)

app = FastAPI()

@app.post("/createimage/")
async def create_image(input_text: str = Form(...)):       
    # ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฏธ์ง€ ์ƒ์„ฑ (์†๋„ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐ์ •)
    image = pipe(
        prompt=input_text,
        num_inference_steps=20,  # ์ƒ์„ฑ ์†๋„ ๊ฐœ์„ ์„ ์œ„ํ•ด 20๋‹จ๊ณ„๋กœ ์„ค์ •
        guidance_scale=3.0       # ํ”„๋กฌํ”„ํŠธ ๋”ฐ๋ฅด๋Š” ๊ฐ•๋„ ์กฐ์ •
    ).images[0]

    # ์ด๋ฏธ์ง€ ์ €์žฅ ๋ฐ ๋ฐ˜ํ™˜
    image_path = "generated_image.png"
    image.save(image_path)
    return FileResponse(image_path, media_type="image/png")

 

 

์–ด์ œ ์“ด StableDiffusion ๋กœ๊ทธ์ธ ํ•˜๋Š” ๋ฒ• ์„ฑ๋ฒ”๋‹˜์ด ์•Œ๋ ค์ฃผ์‹ฌ...

 

1. token ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๊ณ 

2. ํ„ฐ๋ฏธ๋„์— huggingface-cli login ์ž…๋ ฅํ•˜๋ฉด

3. ํ† ํฐ ๋„ฃ์–ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ... ์•ˆ ๋ณด์ด๋‹ˆ๊นŒ ํ•œ๋ฒˆ๋งŒ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜์‚ผ..

# '''
# 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 StableDiffusionPipeline
from fastapi import FastAPI, Form
from fastapi.responses import FileResponse
import torch

# ํ˜ธํ™˜ ๊ฐ€๋Šฅํ•œ ๋ชจ๋ธ ID๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ ๋กœ๋“œ
model_id = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(
    model_id,
    torch_dtype=torch.float16,
    safety_checker=None  # ์•ˆ์ „ ๊ฒ€์‚ฌ๊ธฐ๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•˜์—ฌ ์ผ๋ถ€ ์˜ค๋ฅ˜ ๋ฐฉ์ง€
).to("cuda")
pipe.enable_attention_slicing()

# 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 FileResponse(image_path, media_type="image/png")
728x90
๋ฐ˜์‘ํ˜•
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
๋ฐ˜์‘ํ˜•

728x90
๋ฐ˜์‘ํ˜•

 

์‹œํ๋ฆฌํ‹ฐ ๋ฒ„์ „๋ณ„ ํŠน์„ฑ

 

: ์Šคํ”„๋ง์€ ๋ฒ„์ „์— ๋”ฐ๋ผ ๊ตฌํ˜„ ๋ฐฉ์‹์ด ๋ณ€๊ฒฝ๋˜๋Š”๋ฐ ์‹œํ๋ฆฌํ‹ฐ์˜ ๊ฒฝ์šฐ ํŠนํžˆ ์„ธ๋ถ€ ๋ฒ„์ „๋ณ„๋กœ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์ด ๋งŽ์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋ฒ„์ „๋งˆ๋‹ค ๊ตฌํ˜„ ํŠน์ง•์„ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค

 

์ƒˆ๋กœ์šด ๋ฒ„์ „์ด ์ถœ์‹œ๋  ๋•Œ๋งˆ๋‹ค GitHub์˜ Spring ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ Security์˜ Release ํ•ญ๋ชฉ์„ ํ†ตํ•ด ๋ณ€๊ฒฝ๋œ ์ ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค

Releases · spring-projects/spring-security (github.com)

 

Releases · spring-projects/spring-security

Spring Security. Contribute to spring-projects/spring-security development by creating an account on GitHub.

github.com

 

 

 

์ฃผ์š” ๋ฒ„์ „๋ณ„ ๊ตฌํ˜„

 

- ์Šคํ”„๋ง ๋ถ€ํŠธ 2.X.X ~ 2.6.X (์Šคํ”„๋ง 5.X.X ~ 5.6.X)

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

				http
	        .authorizeRequests()
            .antMatchers("/").authenticated()
            .anyRequest().permitAll();

    }
}

 

- ์Šคํ”„๋ง ๋ถ€ํŠธ 2.7.X ~ 3,0.X (์Šคํ”„๋ง 5.7.X M2 ~ 6.0.X)

public class SpringSecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

        http
						.authorizeHttpRequests()
			            .requestMatchers("/admin").hasRole("ADMIN")
			            .anyRequest().authenticated();

        return http.build();
    }
}

 

- ์Šคํ”„๋ง ๋ถ€ํŠธ 3.1.X ~ (์Šคํ”„๋ง 6.1.X ~) 

public class SpringSecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

        http
            .authorizeHttpRequests((auth) -> auth
                  .requestMatchers("/login", "/join").permitAll()
                  .anyRequest().authenticated()
        );

        return http.build();
    }
}

 

** 3.1.X ๋ฒ„์ „๋ถ€ํ„ฐ ๋žŒ๋‹คํ˜•์‹ ํ‘œํ˜„ ํ•„์ˆ˜...

 

 

Config ์„ค์ • ํ›„ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€

 

์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ Config ํด๋ž˜์Šค ์„ค์ • ํ›„ ํŠน์ • ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ์ ‘๊ทผ ๊ถŒํ•œ์ด ์—†๋Š” ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŒ… ๋˜์ง€ ์•Š๊ณ  ์˜ค๋ฅ˜ ํŽ˜์ด์ง€๊ฐ€ ๋ฐœ์ƒ....

 

์œ„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Config ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•˜๋ฉด ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ์„ค์ •๋„ ์ง„ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

 

 

์ปค์Šคํ…€ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ :  mustache

 

- login.mustache

- ๋กœ๊ทธ์ธ : ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ POST ์š”์ฒญ ๊ฒฝ๋กœ

 

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    login page
    <hr>
    <form action="/loginProc" method="post" name="loginForm">
        <input id="username" type="text" name="username" placeholder="id"/>
        <input id="password" type="password" name="password" placeholder="password"/>
        <input type="submit" value="login"/>
    </form>
</body>
</html>

 

 

LoginController

package com.example.testsecurity.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class LoginController {

    @GetMapping("/login")
    public String loginP() {

        return "login";
    }
}

 

 

Security Config ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ์„ค์ • ๋ฐ ๋กœ๊ทธ์ธ ๊ฒฝ๋กœ

 

 

admin ํŽ˜์ด์ง€๋กœ ๊ฐ€๋„ ๋ฐ”๋กœ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋ฅผ ๋„์šธ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •..

package com.example.testsecurity.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{

        http
                .authorizeHttpRequests((auth) -> auth
                        .requestMatchers("/", "/login", "/loginProc").permitAll()
                        .requestMatchers("/admin").hasRole("ADMIN")
                        .requestMatchers("/my/**").hasAnyRole("ADMIN", "USER")
                        .anyRequest().authenticated()
                );

		
        // ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์ ‘๊ทผ ์ถ”๊ฐ€ํ•˜๊ธฐ
        http
                .formLogin((auth) -> auth.loginPage("/login")
                        .loginProcessingUrl("/loginProc")
                        .permitAll()
                );
		
        // ํ† ํฐ์„ ๋ณด๋‚ด์ง€ ์•Š์œผ๋ฉด ๋กœ๊ทธ์ธ์ด ์ง„ํ–‰์ด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ถ”ํ›„์— ํ‘ผ๋‹ค๊ณ  ํ•˜์‹ฌ...
        http
                .csrf((auth) -> auth.disable());


        return http.build();
    }
}

 

 

๊ทผ๋ฐ mustache ์™œ ์”€??

=> https://velog.io/@qowl880/SpringBoot-Mustache

 

[SpringBoot(2)] Mustache

์ด์ „๊นŒ์ง€ ์ง„ํ–‰ํ–ˆ๋˜ ์‹ค์Šต์€ ๊ธฐ๋Šฅ๋งŒ ๋„ฃ์–ด APIํ†ต์‹ ์„ swagger๋ฅผ ํ†ตํ•ด ์ง„ํ–‰ํ–ˆ๋‹ค๋ฉด ์ด์ œ๋Š” ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ €๊ฐ€ ๋ณผ์ˆ˜์žˆ๋Š” viewํ™”๋ฉด์œผ๋กœ ์ถœ๋ ฅ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋„๋ก ์ง„ํ–‰ํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค

velog.io

 

 

์‹œํ๋ฆฌํ‹ฐ ์•”ํ˜ธํ™”

 

์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ๋Š” ์‚ฌ์šฉ์ž ์ธ์ฆ(๋กœ๊ทธ์ธ) ์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ์— ๋Œ€ํ•ด ๋‹จ๋ฐฉํ–ฅ ํ•ด์‹œ ์•”ํ˜ธํ™”๋ฅผ ์ง„ํ–‰ํ•˜์—ฌ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๋Œ€์กฐ๋œ๋‹ค. 

๋”ฐ๋ผ์„œ ํšŒ์›๊ฐ€์ž…์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด์„œ ์•”ํ˜ธํ™”๋ฅผ ์ง„ํ–‰ํ•ด์•ผ ํ•œ๋‹ค

 

์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ๋Š” ์•”ํ˜ธํ™”๋ฅผ ์œ„ํ•ด BCrypt Password Encorder๋ฅผ ์ œ๊ณตํ•˜๊ณ  ๊ถŒ์žฅํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ return ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด @Bean ์œผ๋กœ ๋“ฑ๋กํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค

 

 

๋‹จ๋ฐฉํ–ฅ ํ•ด์‹œ ์•”ํ˜ธํ™”

 

- ์–‘๋ฐฉํ–ฅ(๋Œ€์นญํ‚ค, ๋น„๋Œ€์นญํ‚ค)

- ๋‹จ๋ฐฉํ–ฅ(ํ—ค์‹œ)

 

 

Security Config ์•”ํ˜ธํ™” Bean ์ถ”๊ฐ€

@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {

    return new BCryptPasswordEncoder();
}

 

 

 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ข…๋ฅ˜์™€ ORM

 

ํšŒ์› ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” MYSQL ์—”์ง„์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ ‘๊ทผ์€ Spring Date JPA ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค

 

 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์˜์กด์„ฑ

dependencies {

    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'com.mysql:mysql-connector-j'
}

 

 

๋ณ€์ˆ˜ ์„ค์ •

  • application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://์•„์ดํ”ผ:3306/๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true
spring.datasource.username=์•„์ด๋””
spring.datasource.password=๋น„๋ฐ€๋ฒˆํ˜ธ

 

 

ํšŒ์›๊ฐ€์ž… ๋กœ์ง

 

ํšŒ์›์ •๋ณด๋ฅผ ํ†ตํ•ด ์ธ์ฆ ์ธ๊ฐ€ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ํšŒ์› ๊ฐ€์ž…์„ ์ง„ํ–‰ํ•œ ๋’ค ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ํšŒ์› ์ •๋ณด๋ฅผ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค

 

 

ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€ : mustache

 

- join.mustache

<form action="/joinProc" method="post" name="joinForm">
    <input type="text" name="username" placeholder="Username"/>
    <input type="password" name="password" placeholder="Password"/>
		<input type="submit" value="Join"/>
</form>

 

- JoinDTO

package com.example.testsecurity.dto;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class JoinDTO {

    private String username;
    private String password;
}

 

- JoinController

package com.example.testsecurity.controller;

import com.example.testsecurity.dto.JoinDTO;
import com.example.testsecurity.service.JoinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class JoinController {

    @Autowired
    private JoinService joinService;


    @GetMapping("/join")
    public String joinP() {

        return "join";
    }


    @PostMapping("/joinProc")
    public String joinProcess(JoinDTO joinDTO) {

        System.out.println(joinDTO.getUsername());

        joinService.joinProcess(joinDTO);


        return "redirect:/login";
    }
}

 

- JoinService

package com.example.testsecurity.service;

import com.example.testsecurity.dto.JoinDTO;
import com.example.testsecurity.entity.UserEntity;
import com.example.testsecurity.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class JoinService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    public void joinProcess(JoinDTO joinDTO) {


        //db์— ์ด๋ฏธ ๋™์ผํ•œ username์„ ๊ฐ€์ง„ ํšŒ์›์ด ์กด์žฌํ•˜๋Š”์ง€?


        UserEntity data = new UserEntity();

        data.setUsername(joinDTO.getUsername());
        data.setPassword(bCryptPasswordEncoder.encode(joinDTO.getPassword()));
        data.setRole("ROLE_USER");


        userRepository.save(data);
    }
}

 

 

 

-UserEntity

package com.example.testsecurity.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;

@Entity
@Setter
@Getter
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String username;
    private String password;

    private String role;
}

 

 

Table ์ƒ์„ฑ : Hibernate ddl ์„ค์ •

 

- application properties

spring.jpa.hibernate.ddl-auto=none		// none์œผ๋กœ ๋ฐ”๊พธ๋ฉด ์ด๋ฏธ ๋งŒ๋“ค์–ด์ง„ ํ…Œ์ด๋ธ”์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์ง€๋กฑ
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

 

- UserRepository

package com.example.testsecurity.repository;

import com.example.testsecurity.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<UserEntity, Integer> {

}

 

- SecurityConfig ์ ‘๊ทผ ๊ถŒํ•œ

http
          .authorizeHttpRequests((auth) -> auth
                  .requestMatchers("/", "/login", "/loginProc", "/join", "/joinProc").permitAll()
                  .requestMatchers("/admin").hasRole("ADMIN")
                  .requestMatchers("/my/**").hasAnyRole("ADMIN", "USER")
                  .anyRequest().authenticated()
          );

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts