Spaces:
Runtime error
Runtime error
Add application file
Browse files- ai_functions.py +66 -0
- main.py +73 -0
- requirements.txt +1 -0
ai_functions.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
|
3 |
+
client = None
|
4 |
+
|
5 |
+
Meal2Json = """
|
6 |
+
對於每道菜的描述,請提取關鍵資訊並轉化為JSON格式。請確保輸出包含菜品名稱、主要成分、估計的卡路里含量,以及可能的飲食限制。飲食限制請基於成分進行推斷,如海鮮過敏、紅肉限制、乳糖不耐症、麩質過敏等。如果沒有特別的飲食限制,請標注為"無"。在評估卡路里時,請考慮到菜品的主要成分和準備方式,提供一個合理的估計值。
|
7 |
+
|
8 |
+
例如,給定菜品描述:
|
9 |
+
|
10 |
+
"櫻花漂浮壽司 - 描述:精選當季最新鮮的生魚片,搭配上以櫻花葉醃製的特製醋飯,壽司表面點綴以食用櫻花花瓣,呈現春天的氣息與美麗。"
|
11 |
+
|
12 |
+
根據以上描述,請生成以下JSON輸出:
|
13 |
+
|
14 |
+
```Json
|
15 |
+
{
|
16 |
+
"name": "櫻花漂浮壽司",
|
17 |
+
"ingredients": ["生魚片", "醋飯", "櫻花葉", "食用櫻花花瓣"],
|
18 |
+
"calories": 200,
|
19 |
+
"dietary_restrictions": ["海鮮過敏"]
|
20 |
+
}
|
21 |
+
```
|
22 |
+
請直接輸出 Json 本體 不需要多餘的內容,並且請確保輸出的Json格式是正確的。請注意,輸出的Json格式必須與上述範例一致,並且
|
23 |
+
請依此格式處理以下菜品描述:
|
24 |
+
"""
|
25 |
+
|
26 |
+
AnylizeJson = """
|
27 |
+
給定一位使用者的個人信息、飲食偏好、健康目標和餐飲計畫,請計算推薦的每日卡路里攝入量,並根據使用者的飲食偏好和餐飲計畫推薦合適的餐點。請確保推薦的餐點遵守使用者的飲食限制並考慮其偏好。最後,請提供一份綜合考量後的飲食計畫的推薦。
|
28 |
+
|
29 |
+
請使用以下格式輸出結果:
|
30 |
+
|
31 |
+
```json
|
32 |
+
{
|
33 |
+
"recommended_daily_calories": "XXXX kcal",
|
34 |
+
"meal_recommendations": {
|
35 |
+
"meal": ["推薦的配餐1", "推薦的配餐2"],
|
36 |
+
},
|
37 |
+
"nutrition_notes": "根據使用者的目標和限制,這裡是一些營養上的注意事項和建議。"
|
38 |
+
}
|
39 |
+
```
|
40 |
+
請注意,輸出的Json格式必須與上述範例一致,並且請確保輸出的Json格式是正確的。不要包含多餘的內容。尤其是,meal_recommendations 中的餐點名稱必須是具體的菜品名稱,並且請確保輸出僅有Json。
|
41 |
+
根據這些使用者資訊和菜品資料,請首先計算出適合該使用者的每日推薦卡路里攝入量。接著,請檢視使用者的飲食偏好和餐飲計畫,從菜品資料中選擇符合使用者需求的餐點,並提出具體的餐點推薦。最後,請提出一些營養上的注意事項和建議,幫助使用者達成其健康目標。
|
42 |
+
"""
|
43 |
+
|
44 |
+
def ai_function(Mode, Description):
|
45 |
+
system_message = {
|
46 |
+
"role": "system",
|
47 |
+
"content": (
|
48 |
+
"You are an AI that assists the user in generating meal recommendations."
|
49 |
+
)
|
50 |
+
}
|
51 |
+
|
52 |
+
user_message = {
|
53 |
+
"role": "user",
|
54 |
+
"content": (
|
55 |
+
f"{Mode == 'Meal2Json' and Meal2Json or AnylizeJson} {Description}"
|
56 |
+
)
|
57 |
+
}
|
58 |
+
|
59 |
+
messages = [system_message, user_message]
|
60 |
+
|
61 |
+
response = client.chat.completions.create(model="gpt-4",
|
62 |
+
messages=messages,
|
63 |
+
temperature=0.2,
|
64 |
+
max_tokens=2000)
|
65 |
+
|
66 |
+
return response.choices[0].message.content
|
main.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import ai_functions
|
3 |
+
import os
|
4 |
+
from openai import OpenAI
|
5 |
+
import json
|
6 |
+
|
7 |
+
def transform_meal_description_to_json(description):
|
8 |
+
return ai_functions.ai_function("Meal2Json", description)
|
9 |
+
|
10 |
+
def analyze_user_data(likes, dislikes, allergens, diet_plan, calorie_intake, meal_description_json):
|
11 |
+
user_data = {
|
12 |
+
"likes": likes,
|
13 |
+
"dislikes": dislikes,
|
14 |
+
"allergens": allergens,
|
15 |
+
"diet_plan": diet_plan,
|
16 |
+
"calorie_intake": calorie_intake
|
17 |
+
}
|
18 |
+
description = json.dumps({"meal_data": meal_description_json, "user_data": user_data})
|
19 |
+
return ai_functions.ai_function("AnylizeJson", description)
|
20 |
+
|
21 |
+
def verify_api_key(api_key):
|
22 |
+
try:
|
23 |
+
if os.environ.get("PASSWORD") == api_key:
|
24 |
+
ai_functions.client = OpenAI(api_key=os.environ.get("API_KEY"))
|
25 |
+
else:
|
26 |
+
ai_functions.client = OpenAI(api_key=api_key)
|
27 |
+
|
28 |
+
return f"OpenAI API is verified."
|
29 |
+
except Exception as e:
|
30 |
+
return f"OpenAI API isn't verified."
|
31 |
+
|
32 |
+
with gr.Blocks() as app:
|
33 |
+
with gr.Tab("OpenAI API Settings"):
|
34 |
+
with gr.Row():
|
35 |
+
api_key_input = gr.Textbox(label="OpenAI API Key",password=True)
|
36 |
+
api_key_ioutput = gr.Textbox(label="OpenAI API isn't verified yet. Please enter the password to verify.",interactive=False)
|
37 |
+
Verify_button = gr.Button("Verify")
|
38 |
+
|
39 |
+
Verify_button.click(
|
40 |
+
verify_api_key,
|
41 |
+
inputs=[api_key_input],
|
42 |
+
outputs=[api_key_ioutput]
|
43 |
+
)
|
44 |
+
|
45 |
+
with gr.Tab("添加餐點描述"):
|
46 |
+
with gr.Row():
|
47 |
+
meal_description_input = gr.Textbox(label="餐點描述", placeholder="請輸入餐點描述...")
|
48 |
+
meal_description_json_output = gr.Textbox(label="餐點 Json", interactive=False)
|
49 |
+
transform_button = gr.Button("轉換為 JSON 格式")
|
50 |
+
|
51 |
+
transform_button.click(
|
52 |
+
transform_meal_description_to_json,
|
53 |
+
inputs=[meal_description_input],
|
54 |
+
outputs=[meal_description_json_output]
|
55 |
+
)
|
56 |
+
|
57 |
+
with gr.Tab("使用者資訊"):
|
58 |
+
with gr.Column():
|
59 |
+
likes_input = gr.Textbox(label="喜歡的餐點")
|
60 |
+
dislikes_input = gr.Textbox(label="不喜歡的餐點")
|
61 |
+
allergens_checklist = gr.CheckboxGroup(label="選擇過敏原", choices=["海鮮", "麩質", "堅果", "乳糖"])
|
62 |
+
diet_plan_input = gr.Textbox(label="飲食計畫")
|
63 |
+
calorie_intake_input = gr.Textbox(label="卡路里攝取標準")
|
64 |
+
analyze_output = gr.Textbox(label="分析結果", interactive=False)
|
65 |
+
analyze_button = gr.Button("分析並推薦餐點")
|
66 |
+
|
67 |
+
analyze_button.click(
|
68 |
+
analyze_user_data,
|
69 |
+
inputs=[likes_input, dislikes_input, allergens_checklist, diet_plan_input, calorie_intake_input, meal_description_json_output],
|
70 |
+
outputs=[analyze_output]
|
71 |
+
)
|
72 |
+
|
73 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
openai
|