Commit 2662440d authored by blackheaven's avatar blackheaven
Browse files

Upload New File

parents
Loading
Loading
Loading
Loading

capstone_input.ipynb

0 → 100644
+141 −0
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
import random
```

%% Cell type:code id: tags:

``` python
em = input('') # 어떤 감정인지 input받기

if em == 'happy':
    list_em = [1, 2, 3]
    choice_em = random.choice(list_em)


elif em == 'sad':
    i


else
```

%% Output

    123
    123

%% Cell type:code id: tags:

``` python
pip install flask
```

%% Output

    Requirement already satisfied: flask in c:\users\slfmr\anaconda3\lib\site-packages (1.1.2)
    Requirement already satisfied: click>=5.1 in c:\users\slfmr\anaconda3\lib\site-packages (from flask) (7.1.2)
    Requirement already satisfied: Jinja2>=2.10.1 in c:\users\slfmr\anaconda3\lib\site-packages (from flask) (2.11.2)
    Requirement already satisfied: itsdangerous>=0.24 in c:\users\slfmr\anaconda3\lib\site-packages (from flask) (1.1.0)
    Requirement already satisfied: Werkzeug>=0.15 in c:\users\slfmr\anaconda3\lib\site-packages (from flask) (1.0.1)
    Requirement already satisfied: MarkupSafe>=0.23 in c:\users\slfmr\anaconda3\lib\site-packages (from Jinja2>=2.10.1->flask) (1.1.1)
    Note: you may need to restart the kernel to use updated packages.

%% Cell type:markdown id: tags:

특정 경로에서 mp3파일 가져와서 Flask 애플리케이션을 통해 브라우저에서 재생할 수 있도록

--> 코드 실행시 해당 경로에 mp3파일이 존재해야 함

%% Cell type:code id: tags:

``` python
import os
import random
from flask import Flask, send_from_directory
from werkzeug.serving import run_simple

app = Flask(__name__)

@app.route('/mp3') # mp3파일 경로
def serve_mp3():
    # 디렉토리에서 mp3 파일 목록을 가져옵니다.
    files = [f for f in os.listdir('path/to/mp3/files') if f.endswith('.mp3')]

    # list에서 랜덤하게 고른 순서의 mp3 파일을 선택합니다.
    selected_file = random.choice(files)

    # Flask를 사용하여 선택한 파일을 반환합니다.
    return send_from_directory('path/to/mp3/files', selected_file, as_attachment=False)

if __name__ == '__main__':
    #app.run(debug=True)
    run_simple('localhost', 5000, app)
```

%% Output

     * Running on http://localhost:5000/ (Press CTRL+C to quit)
    127.0.0.1 - - [28/Apr/2023 14:45:57] "GET / HTTP/1.1" 404 -
    127.0.0.1 - - [28/Apr/2023 14:45:58] "GET /favicon.ico HTTP/1.1" 404 -

%% Cell type:code id: tags:

``` python
from IPython.display import Audio

# url에는 flask 애플리케이션에서 반환한 url입력.
#audio 함수는 주피터 노트북에서 오디오 파일을 재생하기 위한 IPython 기능

url = 'http://localhost:5000/mp3'
Audio(url=url)
```

%% Output

    <IPython.lib.display.Audio object>

%% Cell type:code id: tags:

``` python
!pip install pygame
```

%% Output

    Requirement already satisfied: pygame in c:\users\slfmr\anaconda3\lib\site-packages (2.3.0)

%% Cell type:code id: tags:

``` python
import pygame

pygame.init()
pygame.mixer.init()

sound = pygame.mixer.Sound('path/to/mp3/file.mp3')
sound.play()

while pygame.mixer.get_busy():
    continue

pygame.quit()
```

%% Output

    ---------------------------------------------------------------------------
    FileNotFoundError                         Traceback (most recent call last)
    <ipython-input-33-78a860412a99> in <module>
          4 pygame.mixer.init()
          5
    ----> 6 sound = pygame.mixer.Sound('path/to/mp3/file.mp3')
          7 sound.play()
          8
    FileNotFoundError: No file 'path/to/mp3/file.mp3' found in working directory 'C:\Users\slfmr'.

%% Cell type:code id: tags:

``` python
```