Skip to content

Commit

Permalink
Feat : #6 Create AI service file (ai_conifg.py)
Browse files Browse the repository at this point in the history
  • Loading branch information
gilyeon00 committed Sep 23, 2022
1 parent d23fa31 commit f8d91d3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
File renamed without changes.
8 changes: 8 additions & 0 deletions config/ai_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

# RABBITMQ_HOST=os.environ.get('RABBITMQ_HOST')
# RABBITMQ_PORT=os.environ.get('RABBITMQ_PORT')
# RABBITMQ_USER=os.environ.get('RABBITMQ_USER')
# RABBITMQ_PASSWORD=os.environ.get('RABBITMQ_PASSWORD')

RBMQ_CONNECTION_URI=f'amqp://darling:0829@rabbitmq:5672//'
5 changes: 5 additions & 0 deletions entity/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ def edit_instance(model, user_id, **kwargs):
setattr(instance, attr, new_value)
commit_changes()


def session_execute():
db.session.execute()


def commit_changes():
db.session.commit()
9 changes: 7 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ gunicorn
sqlalchemy_utils
urllib3
pandas
werkzeug
werkzeug==2.1.2
boto3
Flask-Mail
celery
amqp
amqp
flask_jwt_extended
flask_bcrypt
flask_restx
redis
50 changes: 50 additions & 0 deletions service/image_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from celery import Celery
from flask import jsonify

from s3bucket import s3_connect
from api import s3_api
from entity.model import Image
from entity import database

from datetime import datetime as dt
from s3bucket.s3_connect import s3
from s3bucket.s3_upload import s3_put_result_image, s3_put_origin_image
from config.ai_config import RBMQ_CONNECTION_URI


app = Celery('tasks',
broker=RBMQ_CONNECTION_URI)


# 사진 받아오는 함수
def saveOriginImage(file, email) :

# 이메일 받아오면 user_id 찾기
sql = f"SELECT user_id \
FROM user \
WHERE email='{email}'"
cursor = database.session_execute(sql)
user_id = cursor.fetchall()[0][0]

# 파일 이름 지정
filename = file.filename.split('.')[0]
image_type = file.filename.split('.')[-1]
image_created = dt.now().strftime('%Y-%m-%d-%H-%M-%S')
image_name = f"{image_created}--{filename}.{image_type}"

# s3버킷에 업로드
s3_put_result_image(s3, 'ladder-s3-bucket', file, image_name)

# postgres image table에 origin_url 업로드
origin_url = "https://ladder-s3-bucket.s3.ap-northeast-2.amazonaws.com/origin/"+image_name
origin_url = origin_url.replace(" ","/")
database.add_instance(Image, user_id = user_id, origin_url = origin_url, is_deleted = False)

print("성공적으로 사진이 S3에 저장되었습니다.")


# celery가 처리할 거
# api요청 (ai 서버)


# upload s3 .. (이건 만들어진 api로 보내도 됨 아니면 그냥 여기에 함수 쓰기)

0 comments on commit f8d91d3

Please sign in to comment.