File size: 5,118 Bytes
35fe345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import gradio as gr
import requests
import random
from geopy.geocoders import Nominatim
import os
from huggingface_hub import InferenceClient

HF_TOKEN = os.environ.get("HF_TOKEN")
client = InferenceClient(api_key=HF_TOKEN)

# 餐點推薦資料庫(根據情緒和天氣)
meal_recommendations = {
    "開心": {
        "cold": ["小籠包", "燉牛肉", "泡麵", "玉米濃湯"],
        "hot": ["雪花冰", "氣水", "涼麵", "西瓜"],
        "normal": ["炸雞配汽水", "壽喜燒", "韓式烤肉"]
    },
    "羞愧": {
        "cold": ["抹茶", "清淡的米粥", "唐心蛋"],
        "hot": ["蔬菜沙拉", "冷飲", "玉米餅"],
        "normal": ["全麥麵包", "藍莓、草莓、橙子", "蒸包子"]
    },
    "憤怒": {
        "cold": ["暖呼呼的火鍋", "熱可可", "麻辣湯"],
        "hot": ["冰沙", "涼拌黃瓜", "水果"],
        "normal": ["炸雞", "巧克力", "薰衣草茶"]
    },
    "悲傷": {
        "cold": ["雞湯", "清淡的米粥", "餅乾"],
        "hot": ["冷湯", "冰棒", "三明治"],
        "normal": ["牛排", "雞蛋", "波士頓派"]
    },
    "忌妒": {
        "cold": ["熱狗","拉麵","泡麵"],
        "hot": ["蔬菜沙拉", "冷飲",],
        "normal": ["奶酥麵包","橙子","pizza"]
    },
    "恐懼": {
        "cold": ["湯泡飯", "泡麵", "湯麵"],
        "hot": ["涼麵", "飲料", "冰淇淋"],
        "normal": ["爆米花", "薯片", "牛排"]
    }
}

# 主功能函式
def recommend_meal(emotion, city):
    temp, weather_info = get_weather(city)
    
    if temp is None:
        return "Unable to fetch weather details. Please check if the city name is correct.", "", ""

    # 根據情緒和氣候選擇餐點
    # 請自行完成挑選餐點的邏輯
    if temp < 15:
       climate = "cold"
    elif temp > 28:
       climate = "hot"
    else:
       climate = "normal"

    meals = meal_recommendations.get(emotion, {}).get(climate, ["隨意料理"])
    meal= random.choice(meals)

    # 生成暖心話語
    comforting_message = generate_comforting_message(emotion)

    recommendation = f"Today's Top Pick: {meal}"

    return f"Current Weather:\n{weather_info}", recommendation, comforting_message

def get_weather(city):
    geolocator = Nominatim(user_agent="geoapi")
    location = geolocator.geocode(city)

    if location:
        lat, lon = location.latitude, location.longitude
        # 使用 Open-Meteo API 取得天氣數據
        weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&current_weather=true"
        weather_response = requests.get(weather_url)
        if weather_response.status_code == 200:
            weather_data = weather_response.json()
            temp, temp_unit = weather_data['current_weather']['temperature'], weather_data['current_weather_units']['temperature']
            windspeed, windspeed_unit = weather_data['current_weather']['windspeed'], weather_data['current_weather_units']['windspeed']
            weather_desc = f"{temp}{temp_unit},Wind speed: {windspeed} {windspeed_unit}"
            return temp, weather_desc
    else:
        return None, None

# 生成暖心話語的函式
def generate_comforting_message(emotion):
    temperature, top_p = random.uniform(0.6, 0.75), random.uniform(0.7, 1.0)
    completion = client.chat.completions.create(
        model="mistralai/Mistral-Nemo-Instruct-2407",
        messages=[{
            "role": "system",
            "content": "你是一位善解人意且富有同理心的 AI 助理,專門為人們提供鼓勵和安慰。無論使用者的情緒如何,你都能給予真摯、溫暖且鼓舞人心的話語,讓他們感到被理解和支持。請用溫柔、真誠且富有啟發性的語氣回應,並確保所有回覆都以**繁體中文**撰寫。"
        }, {
            "role": "user",
            "content": f"我現在感到{emotion},請給我一句鼓勵的話。\n"
        }],
        temperature=temperature,
        max_tokens=2048,
        top_p=top_p,
    )

    return completion.choices[0].message.content

# Gradio 介面
with gr.Blocks() as app:
    gr.Markdown("## 🌤️🍽️ Meal Matchmaker: Food for Your Mood and Weather! 🍽️🌤️")

    with gr.Row():
        with gr.Column():
            emotion = gr.Dropdown(
                ["開心", "羞愧", "憤怒", "悲傷", "忌妒", "恐懼"],
                label="🎭 Pick Your Mood "
            )
        with gr.Column():
            city = gr.Textbox(label="📍 Enter Your Location (e.g., 台北、Okinawa)")

    submit_btn = gr.Button("Serve Me a Meal! ✨")

    with gr.Row():
            weather_output = gr.Textbox(label="☁️ Weather Check", interactive=False)
            meal_output = gr.Textbox(label="🎉 Your Perfect Meal", interactive=False)
            message_output = gr.Textbox(label="💖 A Little Boost of Encouragement", interactive=False)

    submit_btn.click(
        recommend_meal,
        inputs=[emotion, city],
        outputs=[weather_output, meal_output, message_output]
    )

# 啟動應用
app.launch(debug=False)