import gradio as gr
from openai import OpenAI
import json
client = OpenAI()
tools = [
{
"type": "function",
"function": {
"name": "bmi_analysis",
"description": "사용자가 입력한 키와 몸무게 값을 이용하여 BMI 관련 정보를 제공하는 함수",
"parameters": {
"type": "object",
"properties": {
"bmi_value": {
"type": "string",
"description": "사용자가 입력한 키와 몸무게 값을 이용하여 계산한 BMI 값",
},
"bmi_status": {
"type": "string",
"description": "계산된 BMI 값 기반으로 판단한 비만도 및 정상 수준 ",
},
"food_recommendation": {
"type": "string",
"description": "계산된 BMI 값을 기반으로 추천할 만한 음식을 생성하여 제공",
},
"exercise_recommendation": {
"type": "string",
"description": "계산된 BMI 값을 기반으로 추천할 만한 운동 루틴을 생성하여 제공",
},
},
"required": ["bmi_value", "bmi_status", "food_recommendation", "exercise_recommendation"]
},
},
},
]
# BMI 계산 함수
def calculate_bmi(height, weight):
prompt_text = f"키:{height}, 몸무게:{weight}";
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "아래 사용자의 키와 몸무게 값을 이용하여 BMI, 비만도, 추천음식, 추천운동을 생성한 후, bmi_analysis 함수를 호출해줘."},
{"role": "system", "content": "추천음식에는 1주일 동안 1일 단위로 필요한 음식 식단을 작성해줘."},
{"role": "system", "content": "추천운동에는 1주일 동안 1일 단위로 반복하여 운동할 수 있는 자세한 운동 루틴을 작성해줘."},
{"role": "user", "content": prompt_text}
],
tools=tools,
)
response = completion.choices[0].message;
tool_call = response.tool_calls[0]
arguments = tool_call.function.arguments
arguments_json = json.loads(arguments)
bmi_value = arguments_json.get('bmi_value')
bmi_status = arguments_json.get('bmi_status')
food_recommendation = arguments_json.get('food_recommendation')
exercise_recommendation = arguments_json.get('exercise_recommendation')
return bmi_value, bmi_status, food_recommendation, exercise_recommendation
# Gradio Blocks 레이아웃 구성
with gr.Blocks() as demo:
# HTML로 직접 CSS 삽입하여 스타일을 지정
gr.HTML("""
<style>
body {
background-color: #001f3d; /* dark blue */
color: white; /* 텍스트 색상 흰색 */
font-family: 'Helvetica', sans-serif;
}
h1 {
text-align: center; /* 제목 중앙 정렬 */
font-size: 3em;
margin-top: 50px;
}
.gradio-container {
background-color: #001f3d; /* 전체 컨테이너 배경 색상 */
color: white; /* 텍스트 색상 흰색 */
}
</style>
""")
# HTML로 제목 추가
gr.HTML("<h1 style='color: white'>AI BMI 계산기</h1>")
# 입력 폼 구성
with gr.Row():
height_input = gr.Number(label="키 (cm)")
weight_input = gr.Number(label="몸무게 (kg)")
# 결과 출력
with gr.Row():
bmi_result = gr.Textbox(label="BMI", interactive=False)
bmi_status = gr.Textbox(label="체중 상태", interactive=False)
food_recommendation = gr.Textbox(label="추천 음식", interactive=False, lines=5)
exercise_recommendation = gr.Textbox(label="추천 운동", interactive=False, lines=5)
# 버튼 클릭 시 BMI 계산 함수 실행
calculate_button = gr.Button("BMI 계산")
calculate_button.click(fn=calculate_bmi, inputs=[height_input, weight_input],
outputs=[bmi_result, bmi_status, food_recommendation, exercise_recommendation])
# 인터페이스 실행
demo.launch()