Skip to content

Commit

Permalink
chuseok_keyword #1
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeposi committed Sep 11, 2024
1 parent 876f75c commit aa43814
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 1 deletion.
66 changes: 66 additions & 0 deletions diaryclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,69 @@ async def get_weather_playlist(self):
self.thumbnail = search_result["snippet"]["thumbnails"]["default"]["url"]
break
return self.playlist, self.title, self.thumbnail


class ChuseokKeyword(Diary):
def __init__(self, member_id, created_at, updated_at, written_at, content):
super().__init__(member_id, created_at, updated_at, written_at)
self.content = content
self.keyword = None
self.illustration = None

keyword_map = {
"송편": "송편 베어",
"보름달": "보름달 베어",
"한복": "한복 베어",
"곶감": "곶감 베어",
"집콕": "뒹굴 베어",
"여행": "트레블 베어",
"가족": "패밀리 베어"
}

illustration_map = {
"송편 베어": "https://kkoolbee-storage.s3.ap-northeast-2.amazonaws.com/%EC%B6%94%EC%84%9D/%E1%84%8E%E1%85%AE%E1"
"%84%89%E1%85%A5%E1%86%A8+%E1%84%91%E1%85%A9%E1%86%AF%E1%84%83%E1%85%A5/%E1%84%89%E1%85%A9%E1%86%BC"
"%E1%84%91%E1%85%A7%E1%86%AB%E1%84%87%E1%85%A6%E1%84%8B%E1%85%A5.png",
"보름달 베어": "https://kkoolbee-storage.s3.ap-northeast-2.amazonaws.com/%EC%B6%94%EC%84%9D/%E1%84%8E%E1%85%AE%E1"
"%84%89%E1%85%A5%E1%86%A8+%E1%84%91%E1%85%A9%E1%86%AF%E1%84%83%E1%85%A5/%E1%84%87%E1%85%A9%E1%84%85"
"%E1%85%B3%E1%86%B7%E1%84%83%E1%85%A1%E1%86%AF%E1%84%87%E1%85%A6%E1%84%8B%E1%85%A5.png",
"한복 베어": "https://kkoolbee-storage.s3.ap-northeast-2.amazonaws.com/%EC%B6%94%EC%84%9D/%E1%84%8E%E1%85%AE%E1"
"%84%89%E1%85%A5%E1%86%A8+%E1%84%91%E1%85%A9%E1%86%AF%E1%84%83%E1%85%A5/%E1%84%92%E1%85%A1%E1%86%AB"
"%E1%84%87%E1%85%A9%E1%86%A8%E1%84%87%E1%85%A6%E1%84%8B%E1%85%A5.png",
"곶감 베어": "https://kkoolbee-storage.s3.ap-northeast-2.amazonaws.com/%EC%B6%94%EC%84%9D/%E1%84%8E%E1%85%AE%E1"
"%84%89%E1%85%A5%E1%86%A8+%E1%84%91%E1%85%A9%E1%86%AF%E1%84%83%E1%85%A5/%E1%84%80%E1%85%A9%E1%86%BD"
"%E1%84%80%E1%85%A1%E1%86%B7%E1%84%87%E1%85%A6%E1%84%8B%E1%85%A5.png",
"뒹굴 베어": "https://kkoolbee-storage.s3.ap-northeast-2.amazonaws.com/%EC%B6%94%EC%84%9D/%E1%84%8E%E1%85%AE%E1"
"%84%89%E1%85%A5%E1%86%A8+%E1%84%91%E1%85%A9%E1%86%AF%E1%84%83%E1%85%A5/%E1%84%8C%E1%85%B5%E1%86%B8"
"%E1%84%8F%E1%85%A9%E1%86%A8%E1%84%87%E1%85%A6%E1%84%8B%E1%85%A5.png",
"트레블 베어": "hhttps://kkoolbee-storage.s3.ap-northeast-2.amazonaws.com/%EC%B6%94%EC%84%9D/%E1%84%8E%E1%85%AE%E1"
"%84%89%E1%85%A5%E1%86%A8+%E1%84%91%E1%85%A9%E1%86%AF%E1%84%83%E1%85%A5/%E1%84%90%E1%85%B3%E1%84%85"
"%E1%85%A6%E1%84%87%E1%85%B3%E1%86%AF%E1%84%87%E1%85%A6%E1%84%8B%E1%85%A5.png",
"패밀리 베어": "https://kkoolbee-storage.s3.ap-northeast-2.amazonaws.com/%EC%B6%94%EC%84%9D/%E1%84%8E%E1%85%AE%E1"
"%84%89%E1%85%A5%E1%86%A8+%E1%84%91%E1%85%A9%E1%86%AF%E1%84%83%E1%85%A5/%E1%84%91%E1%85%A2%E1%84%86"
"%E1%85%B5%E1%86%AF%E1%84%85%E1%85%B5%E1%84%87%E1%85%A6%E1%84%8B%E1%85%A5.png"
}


async def change_keyword(self, keyword):
return self.keyword_map.get(keyword, None)

async def get_illustration(self, keyword):
return self.illustration_map.get(keyword, None)

async def get_chuseok_keyword(self):
prompt = (Prompt.chuseok_keyword_prompt %
self.content)

client = self.client
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": 'you are a diary writer'},
{"role": "user", "content": prompt},
]
)

self.keyword = completion.choices[0].message.content
self.keyword = await self.change_keyword(self.keyword)
self.illustration = await self.get_illustration(self.keyword)
53 changes: 53 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,59 @@ async def get_weather_playlist(request: Request):
}



@app.get('/api/ai/diary/chuseok/keyword')
async def get_chuseok_keyword(request: Request):

conn = await connect_mysql()
if conn is None:
return Response(status_code=500, content="MySQL 연결에 실패했습니다.")

member_id = request.headers.get('Authorization')

if member_id is None:
return Response(status_code=401, content="토큰이 없습니다.")

data = await request.json()
diary_id = data.get('diaryId')

try:
async with conn.cursor() as cursor:
query = "SELECT content FROM diary WHERE member_id = %s AND diary_id = %s"
await cursor.execute(query, (member_id, diary_id))
content = await cursor.fetchone()
content = content['content']

new_keyword = diary.ChuseokKeyword(
member_id=member_id,
created_at=None,
updated_at=None,
written_at=None,
content=content
)

await new_keyword.get_chuseok_keyword()

keyword = new_keyword.keyword
illustration = new_keyword.illustration

except Exception as e:
error_message = str(e)
traceback_message = traceback.format_exc()
return Response(status_code=500, content="요청이 실패했습니다.")

finally:
conn.close()

return {
"status": 200,
"message": "요청이 성공했습니다.",
"data": {
"keyword": keyword,
"illustration": illustration
}
}

if __name__ == '__main__':
uvicorn.run(app, host="127.0.0.1", port=8080)

43 changes: 42 additions & 1 deletion prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,45 @@
캐릭터는 꿀법 옷을 입고있는 곰을 모티브로 한 일러스트로, 사용자의 diary 내용에 맞는 포즈와 표정을 취하도록 해주세요.
다양한 요소들을 추가하기보단 불필요한 것들을 빼고 단조로운 스타일로 생성해주세요
"""
"""

chuseok_keyword_prompt = """
한국의 추석 특집 diary 분석입니다.
유저의 diary 내용
- diary 내용: %s
유저의 diary 내용을 분석해 diary에서 확인할 수 있는 keyword를 분석해주세요.
분석을 하고 해당 keyword의 특성이 나타나는 비율을 나타내주세요.
keyword의 종류는 다음과 같고, 각 keyword의 특성은 다음과 같습니다.
- 송편 : 먹을걸 많이 먹었을 때
- 보름달 : 보름달과 관련된 내용이거나, 서정적이고 감성적인 내용이 포함되었을 때
- 한복 : 한복과 관련된 내용이거나 추석 놀이등 전통적인 활동에 대한 내용이 포함되었을 때
- 곶감 : 과일을 많이 먹거나 차례를 지냈을 때
- 집콕 : 어디 가지 않고 집에서 추석을 보냈을 때
- 여행 : 추석에 여행을 다녀왔을 때
- 가족 : 가족이나 친인척 집에 방문하여 추석을 보냈거나, 고향 지인들을 만났을 때
예시:
- 다이어리 내용 예시 :
오늘은 기다리던 추석이야! 아침부터 가족들과 함께 송편을 만들기 시작했어. 엄마가 반죽을 치대고, 아빠는 팥소를 준비했지.
나는 주로 송편의 모양을 예쁘게 만드는 역할을 맡았는데, 내가 만든 송편이 제일 예쁘다고 모두 칭찬해 줘서 기분이 좋았어.
점심에는 우리가 만든 송편을 먹었고, 따뜻한 국물과 함께하니 정말 맛있었어.
오후에는 차례를 지냈는데, 그때 곶감과 여러 과일을 올렸어.
특히, 아버지가 고향에서 직접 말린 곶감이 너무 맛있어서 몇 개씩 집어먹었지.
차례를 지내고 나서는 가족들이 둘러앉아 이야기를 나누며 과일을 나눠 먹었어.
저녁이 되고 하늘을 보니 보름달이 환하게 떠 있었어.
그 모습에 감탄하면서 가족들과 함께 밖으로 나갔지. 달빛 아래에서 한복을 입고 사진도 찍고, 전통 놀이인 윷놀이도 했어.
한복을 입고 나니 더 특별한 기분이 들었고, 모두가 웃으며 즐거운 시간을 보냈어.
오늘은 집콕하며 가족과 함께 보낸 시간이었지만, 내일은 고향 친구들을 만나러 여행을 갈 예정이야.
오랜만에 만나는 친구들이라 더 기대돼. 이렇게 따뜻한 추석을 보낼 수 있어 정말 행복해.
- 분석 결과 예시 :
{송편: 33.3, 보름달: 11.1, 한복: 11.1, 곶감: 11.1, 집콕: 11.1, 여행: 11.1, 가족: 44.4}
단, 분석결과는 출력하지 말아주세요
그리고 출력은 분석 결과에서 가장 높은 keyword 한개만 출력해주세요
추가적인 답변이나 말을 덧붙이지 말고 감정만 출력해주세요.
예시 :
가족
"""

0 comments on commit aa43814

Please sign in to comment.