Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import base64
|
| 4 |
+
import os
|
| 5 |
+
# ✅ 輸入你的 OpenAI API 金鑰
|
| 6 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 7 |
+
|
| 8 |
+
# ✅ 圖片轉 base64
|
| 9 |
+
def image_to_base64(image_path):
|
| 10 |
+
with open(image_path, "rb") as img_file:
|
| 11 |
+
return base64.b64encode(img_file.read()).decode("utf-8")
|
| 12 |
+
|
| 13 |
+
# ✅ GPT-4o 分析餐點圖片
|
| 14 |
+
def analyze_diet(image_path, goal, gender, age):
|
| 15 |
+
try:
|
| 16 |
+
img_b64 = image_to_base64(image_path)
|
| 17 |
+
messages = [
|
| 18 |
+
{
|
| 19 |
+
"role": "user",
|
| 20 |
+
"content": [
|
| 21 |
+
{
|
| 22 |
+
"type": "text",
|
| 23 |
+
"text": f"這是使用者的餐點圖片。性別:{gender},年齡:{age} 歲,目標:{goal}。請根據圖片中的內容,給出健康分析與一句鼓勵話。格式如下:\n建議:...\n鼓勵:..."
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"type": "image_url",
|
| 27 |
+
"image_url": {
|
| 28 |
+
"url": f"data:image/jpeg;base64,{img_b64}"
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
]
|
| 32 |
+
}
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
response = openai.ChatCompletion.create(
|
| 36 |
+
model="gpt-4o",
|
| 37 |
+
messages=messages,
|
| 38 |
+
temperature=0.7
|
| 39 |
+
)
|
| 40 |
+
result = response.choices[0].message.content.strip()
|
| 41 |
+
|
| 42 |
+
if "建議:" in result and "鼓勵:" in result:
|
| 43 |
+
suggestion = result.split("建議:")[1].split("鼓勵:")[0].strip()
|
| 44 |
+
encouragement = result.split("鼓勵:")[1].strip()
|
| 45 |
+
else:
|
| 46 |
+
suggestion = result
|
| 47 |
+
encouragement = "你的飲食選擇很棒!繼續保持~"
|
| 48 |
+
|
| 49 |
+
return suggestion, encouragement
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return "❌ 分析失敗:" + str(e), ""
|
| 53 |
+
|
| 54 |
+
# ✅ 運動分析(仍為文字輸入)
|
| 55 |
+
def analyze_workout(text, goal, gender, age):
|
| 56 |
+
prompt = f"""
|
| 57 |
+
你是一位健身教練,使用者性別為 {gender},年齡 {age} 歲,目標是「{goal}」。
|
| 58 |
+
以下是他的運動紀錄:
|
| 59 |
+
{text}
|
| 60 |
+
請給出運動分析與一句鼓勵話,格式如下:
|
| 61 |
+
建議:...
|
| 62 |
+
鼓勵:...
|
| 63 |
+
"""
|
| 64 |
+
try:
|
| 65 |
+
response = openai.ChatCompletion.create(
|
| 66 |
+
model="gpt-4o",
|
| 67 |
+
messages=[
|
| 68 |
+
{"role": "system", "content": "你是一位健身教練"},
|
| 69 |
+
{"role": "user", "content": prompt}
|
| 70 |
+
],
|
| 71 |
+
temperature=0.7
|
| 72 |
+
)
|
| 73 |
+
result = response.choices[0].message.content.strip()
|
| 74 |
+
|
| 75 |
+
if "建議:" in result and "鼓勵:" in result:
|
| 76 |
+
suggestion = result.split("建議:")[1].split("鼓勵:")[0].strip()
|
| 77 |
+
encouragement = result.split("鼓勵:")[1].strip()
|
| 78 |
+
else:
|
| 79 |
+
suggestion = result
|
| 80 |
+
encouragement = "保持運動習慣,健康就靠你!"
|
| 81 |
+
|
| 82 |
+
return suggestion, encouragement
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
return "❌ 分析失敗:" + str(e), ""
|
| 86 |
+
|
| 87 |
+
# ✅ Gradio UI
|
| 88 |
+
with gr.Blocks() as health_ai:
|
| 89 |
+
gr.Markdown("# 🧠 我的健康管家 AI")
|
| 90 |
+
gr.Markdown("上傳你的餐點圖片或輸入運動內容,AI 幫你分析健康狀況並給鼓勵 💪")
|
| 91 |
+
|
| 92 |
+
with gr.Tabs():
|
| 93 |
+
with gr.Tab("🍱 飲食分析(圖片)"):
|
| 94 |
+
diet_input = gr.Image(label="上傳你的餐點圖片", type="filepath")
|
| 95 |
+
diet_goal = gr.Radio(["減脂", "增肌", "維持體態"], label="你的目標")
|
| 96 |
+
diet_gender = gr.Radio(["男", "女"], label="你的性別")
|
| 97 |
+
diet_age = gr.Textbox(label="你的年齡", placeholder="例如:22")
|
| 98 |
+
diet_button = gr.Button("開始分析")
|
| 99 |
+
diet_output = gr.Textbox(label="AI 分析建議")
|
| 100 |
+
diet_encourage = gr.Textbox(label="AI 鼓勵語")
|
| 101 |
+
|
| 102 |
+
with gr.Tab("🏃♂️ 運動分析(文字)"):
|
| 103 |
+
workout_input = gr.Textbox(lines=5, placeholder="例如:慢跑30分鐘,核心訓練15分鐘...")
|
| 104 |
+
workout_goal = gr.Radio(["減脂", "增肌", "維持體態"], label="你的目標")
|
| 105 |
+
workout_gender = gr.Radio(["男", "女"], label="你的性別")
|
| 106 |
+
workout_age = gr.Textbox(label="你的年齡", placeholder="例如:22")
|
| 107 |
+
workout_button = gr.Button("開始分析")
|
| 108 |
+
workout_output = gr.Textbox(label="AI 評估與建議")
|
| 109 |
+
workout_encourage = gr.Textbox(label="AI 鼓勵語")
|
| 110 |
+
|
| 111 |
+
diet_button.click(analyze_diet,
|
| 112 |
+
inputs=[diet_input, diet_goal, diet_gender, diet_age],
|
| 113 |
+
outputs=[diet_output, diet_encourage])
|
| 114 |
+
|
| 115 |
+
workout_button.click(analyze_workout,
|
| 116 |
+
inputs=[workout_input, workout_goal, workout_gender, workout_age],
|
| 117 |
+
outputs=[workout_output, workout_encourage])
|
| 118 |
+
|
| 119 |
+
health_ai.launch()
|