Spaces:
Sleeping
Sleeping
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import random
|
4 |
+
from geopy.geocoders import Nominatim
|
5 |
+
import os
|
6 |
+
from huggingface_hub import InferenceClient
|
7 |
+
|
8 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
9 |
+
client = InferenceClient(api_key=HF_TOKEN)
|
10 |
+
|
11 |
+
# 餐點推薦資料庫(根據情緒和天氣)
|
12 |
+
meal_recommendations = {
|
13 |
+
"開心": {
|
14 |
+
"cold": ["小籠包", "燉牛肉", "泡麵", "玉米濃湯"],
|
15 |
+
"hot": ["雪花冰", "氣水", "涼麵", "西瓜"],
|
16 |
+
"normal": ["炸雞配汽水", "壽喜燒", "韓式烤肉"]
|
17 |
+
},
|
18 |
+
"羞愧": {
|
19 |
+
"cold": ["抹茶", "清淡的米粥", "唐心蛋"],
|
20 |
+
"hot": ["蔬菜沙拉", "冷飲", "玉米餅"],
|
21 |
+
"normal": ["全麥麵包", "藍莓、草莓、橙子", "蒸包子"]
|
22 |
+
},
|
23 |
+
"憤怒": {
|
24 |
+
"cold": ["暖呼呼的火鍋", "熱可可", "麻辣湯"],
|
25 |
+
"hot": ["冰沙", "涼拌黃瓜", "水果"],
|
26 |
+
"normal": ["炸雞", "巧克力", "薰衣草茶"]
|
27 |
+
},
|
28 |
+
"悲傷": {
|
29 |
+
"cold": ["雞湯", "清淡的米粥", "餅乾"],
|
30 |
+
"hot": ["冷湯", "冰棒", "三明治"],
|
31 |
+
"normal": ["牛排", "雞蛋", "波士頓派"]
|
32 |
+
},
|
33 |
+
"忌妒": {
|
34 |
+
"cold": ["熱狗","拉麵","泡麵"],
|
35 |
+
"hot": ["蔬菜沙拉", "冷飲",],
|
36 |
+
"normal": ["奶酥麵包","橙子","pizza"]
|
37 |
+
},
|
38 |
+
"恐懼": {
|
39 |
+
"cold": ["湯泡飯", "泡麵", "湯麵"],
|
40 |
+
"hot": ["涼麵", "飲料", "冰淇淋"],
|
41 |
+
"normal": ["爆米花", "薯片", "牛排"]
|
42 |
+
}
|
43 |
+
}
|
44 |
+
|
45 |
+
# 主功能函式
|
46 |
+
def recommend_meal(emotion, city):
|
47 |
+
temp, weather_info = get_weather(city)
|
48 |
+
|
49 |
+
if temp is None:
|
50 |
+
return "Unable to fetch weather details. Please check if the city name is correct.", "", ""
|
51 |
+
|
52 |
+
# 根據情緒和氣候選擇餐點
|
53 |
+
# 請自行完成挑選餐點的邏輯
|
54 |
+
if temp < 15:
|
55 |
+
climate = "cold"
|
56 |
+
elif temp > 28:
|
57 |
+
climate = "hot"
|
58 |
+
else:
|
59 |
+
climate = "normal"
|
60 |
+
|
61 |
+
meals = meal_recommendations.get(emotion, {}).get(climate, ["隨意料理"])
|
62 |
+
meal= random.choice(meals)
|
63 |
+
|
64 |
+
# 生成暖心話語
|
65 |
+
comforting_message = generate_comforting_message(emotion)
|
66 |
+
|
67 |
+
recommendation = f"Today's Top Pick: {meal}"
|
68 |
+
|
69 |
+
return f"Current Weather:\n{weather_info}", recommendation, comforting_message
|
70 |
+
|
71 |
+
def get_weather(city):
|
72 |
+
geolocator = Nominatim(user_agent="geoapi")
|
73 |
+
location = geolocator.geocode(city)
|
74 |
+
|
75 |
+
if location:
|
76 |
+
lat, lon = location.latitude, location.longitude
|
77 |
+
# 使用 Open-Meteo API 取得天氣數據
|
78 |
+
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true"
|
79 |
+
weather_response = requests.get(weather_url)
|
80 |
+
if weather_response.status_code == 200:
|
81 |
+
weather_data = weather_response.json()
|
82 |
+
temp, temp_unit = weather_data['current_weather']['temperature'], weather_data['current_weather_units']['temperature']
|
83 |
+
windspeed, windspeed_unit = weather_data['current_weather']['windspeed'], weather_data['current_weather_units']['windspeed']
|
84 |
+
weather_desc = f"{temp}{temp_unit},Wind speed: {windspeed} {windspeed_unit}"
|
85 |
+
return temp, weather_desc
|
86 |
+
else:
|
87 |
+
return None, None
|
88 |
+
|
89 |
+
# 生成暖心話語的函式
|
90 |
+
def generate_comforting_message(emotion):
|
91 |
+
temperature, top_p = random.uniform(0.6, 0.75), random.uniform(0.7, 1.0)
|
92 |
+
completion = client.chat.completions.create(
|
93 |
+
model="mistralai/Mistral-Nemo-Instruct-2407",
|
94 |
+
messages=[{
|
95 |
+
"role": "system",
|
96 |
+
"content": "你是一位善解人意且富有同理心的 AI 助理,專門為人們提供鼓勵和安慰。無論使用者的情緒如何,你都能給予真摯、溫暖且鼓舞人心的話語,讓他們感到被理解和支持。請用溫柔、真誠且富有啟發性的語氣回應,並確保所有回覆都以**繁體中文**撰寫。"
|
97 |
+
}, {
|
98 |
+
"role": "user",
|
99 |
+
"content": f"我現在感到{emotion},請給我一句鼓勵的話。\n"
|
100 |
+
}],
|
101 |
+
temperature=temperature,
|
102 |
+
max_tokens=2048,
|
103 |
+
top_p=top_p,
|
104 |
+
)
|
105 |
+
|
106 |
+
return completion.choices[0].message.content
|
107 |
+
|
108 |
+
# Gradio 介面
|
109 |
+
with gr.Blocks() as app:
|
110 |
+
gr.Markdown("## 🌤️🍽️ Meal Matchmaker: Food for Your Mood and Weather! 🍽️🌤️")
|
111 |
+
|
112 |
+
with gr.Row():
|
113 |
+
with gr.Column():
|
114 |
+
emotion = gr.Dropdown(
|
115 |
+
["開心", "羞愧", "憤怒", "悲傷", "忌妒", "恐懼"],
|
116 |
+
label="🎭 Pick Your Mood "
|
117 |
+
)
|
118 |
+
with gr.Column():
|
119 |
+
city = gr.Textbox(label="📍 Enter Your Location (e.g., 台北、Okinawa)")
|
120 |
+
|
121 |
+
submit_btn = gr.Button("Serve Me a Meal! ✨")
|
122 |
+
|
123 |
+
with gr.Row():
|
124 |
+
weather_output = gr.Textbox(label="☁️ Weather Check", interactive=False)
|
125 |
+
meal_output = gr.Textbox(label="🎉 Your Perfect Meal", interactive=False)
|
126 |
+
message_output = gr.Textbox(label="💖 A Little Boost of Encouragement", interactive=False)
|
127 |
+
|
128 |
+
submit_btn.click(
|
129 |
+
recommend_meal,
|
130 |
+
inputs=[emotion, city],
|
131 |
+
outputs=[weather_output, meal_output, message_output]
|
132 |
+
)
|
133 |
+
|
134 |
+
# 啟動應用
|
135 |
+
app.launch(debug=False)
|