Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import plotly.graph_objects as go
|
3 |
+
import random
|
4 |
+
import numpy as np
|
5 |
+
import requests
|
6 |
+
import os
|
7 |
+
from huggingface_hub import InferenceClient
|
8 |
+
|
9 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
10 |
+
client = InferenceClient(api_key=HF_TOKEN)
|
11 |
+
|
12 |
+
def generate_response(system_prompt, user_prompt):
|
13 |
+
temperature, top_p = random.uniform(0.6, 0.75), random.uniform(0.7, 1.0)
|
14 |
+
completion = client.chat.completions.create(
|
15 |
+
model="mistralai/Mistral-Nemo-Instruct-2407",
|
16 |
+
messages=[{
|
17 |
+
"role": "system",
|
18 |
+
"content": system_prompt
|
19 |
+
}, {
|
20 |
+
"role": "user",
|
21 |
+
"content": user_prompt
|
22 |
+
}],
|
23 |
+
temperature=temperature,
|
24 |
+
max_tokens=1024,
|
25 |
+
top_p=top_p,
|
26 |
+
)
|
27 |
+
|
28 |
+
return completion.choices[0].message.content
|
29 |
+
|
30 |
+
def generate_location_recommendations(user_prompt):
|
31 |
+
|
32 |
+
system_prompt = """
|
33 |
+
你是一個專業的地點推薦助手,專門根據使用者的需求提供相關的知名景點或位置。當使用者詢問某個地點、旅遊目的或活動建議時,請根據指令找出適合的多個景點,並**務必滿足使用者的詢問需求**。回應需遵守以下規則:
|
34 |
+
|
35 |
+
1. **以條列式列出景點名稱**,每行一個景點名稱。
|
36 |
+
2. **不要提供任何額外描述、編號或其他與地點無關的文字**,僅列出名稱即可。
|
37 |
+
3. 提供的景點必須是**真實存在**的,而非虛構或杜撰的。
|
38 |
+
3. 所有回應須使用**繁體中文**。
|
39 |
+
4. **完全滿足**使用者的發問需求。
|
40 |
+
|
41 |
+
### 回應格式範例:
|
42 |
+
|
43 |
+
```
|
44 |
+
景點 1
|
45 |
+
景點 2
|
46 |
+
景點 3
|
47 |
+
```
|
48 |
+
|
49 |
+
請確保提供的景點與使用者查詢密切相關,並確認所有景點真實存在,且涵蓋足夠的選項以滿足使用者的需求。"""
|
50 |
+
|
51 |
+
completion = generate_response(system_prompt, user_prompt)
|
52 |
+
|
53 |
+
locations = []
|
54 |
+
for loc in completion.split('\n'):
|
55 |
+
lat_lon = fetch_location_coordinates(loc)
|
56 |
+
locations.append(lat_lon)
|
57 |
+
|
58 |
+
# 刪除找不到的地點
|
59 |
+
locations = np.array([loc for loc in locations if loc[1] is not None])
|
60 |
+
|
61 |
+
return user_prompt, locations
|
62 |
+
|
63 |
+
def generate_travel_summary(user_prompt, locations):
|
64 |
+
|
65 |
+
system_prompt = """
|
66 |
+
你是一個專業且富有魅力的旅遊與地點推薦助手。你的任務是根據**使用者的原始提問**和提供的**景點或地點清單**,生成一段專業、豐富且引人入勝的結論。這段結論應讓使用者感到興奮、滿足或有所啟發,並進一步激發他們的興趣。
|
67 |
+
|
68 |
+
請遵循以下指引:
|
69 |
+
|
70 |
+
1. **根據使用者的需求和目的**,適當總結景點或地點清單,並提供有趣的背景或亮點描述。
|
71 |
+
2. **保持語氣專業且富有吸引力**,可以適度加入生動的形容詞,讓建議更具畫面感和感染力。
|
72 |
+
3. **結尾可以提出鼓勵性或引導性的語句**,促使用者更期待他們的旅程或任務。
|
73 |
+
4. **回應須使用繁體中文**,並保持流暢易讀。
|
74 |
+
|
75 |
+
輸入格式:
|
76 |
+
|
77 |
+
使用者提問:「問題內容」
|
78 |
+
景點清單:
|
79 |
+
```
|
80 |
+
地點 1
|
81 |
+
地點 2
|
82 |
+
地點 3
|
83 |
+
```
|
84 |
+
|
85 |
+
輸出格式 (輸出內容**僅包含結論**,**無須重複**提問內容或其他無關的文字):
|
86 |
+
你的結論
|
87 |
+
"""
|
88 |
+
|
89 |
+
user_prompt = f"""使用者提問:「{user_prompt}」
|
90 |
+
景點清單:
|
91 |
+
```
|
92 |
+
{locations}
|
93 |
+
```\n\n"""
|
94 |
+
|
95 |
+
completion = generate_response(system_prompt, user_prompt)
|
96 |
+
|
97 |
+
return completion
|
98 |
+
|
99 |
+
def fetch_location_coordinates(location):
|
100 |
+
location_response = requests.get(
|
101 |
+
f"https://nominatim.openstreetmap.org/search?q={location}&format=json&limit=1",
|
102 |
+
headers={
|
103 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
|
104 |
+
"Accept-Language": "zh-TW,zh;q=0.7",
|
105 |
+
"Cache-Control": "max-age=0",
|
106 |
+
"Priority": "u=0, i",
|
107 |
+
"Sec-Ch-Ua-Mobile": "?1",
|
108 |
+
"Sec-Ch-Ua-Platform": "\"Android\"",
|
109 |
+
"Sec-Fetch-Dest": "document",
|
110 |
+
"Sec-Fetch-Mode": "navigate",
|
111 |
+
"Sec-Fetch-Site": "none",
|
112 |
+
"Sec-Fetch-User": "?1",
|
113 |
+
"Sec-Gpc": "1",
|
114 |
+
"Upgrade-Insecure-Requests": "1",
|
115 |
+
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36^"
|
116 |
+
}
|
117 |
+
)
|
118 |
+
if location_response.status_code == 200 and len(location_response.json()) > 0:
|
119 |
+
loc = location_response.json()[0]
|
120 |
+
loc = [location, float(loc['lat']), float(loc['lon'])]
|
121 |
+
return loc
|
122 |
+
else:
|
123 |
+
return [location, None, None]
|
124 |
+
|
125 |
+
def plot_locations(locations):
|
126 |
+
if len(locations) == 0:
|
127 |
+
return "No locations found. Please try another query."
|
128 |
+
|
129 |
+
names = locations[:, 0]
|
130 |
+
lats = locations[:, 1].astype(float)
|
131 |
+
lons = locations[:, 2].astype(float)
|
132 |
+
|
133 |
+
fig = go.Figure(go.Scattermapbox(
|
134 |
+
lat=lats,
|
135 |
+
lon=lons,
|
136 |
+
mode='markers',
|
137 |
+
marker=go.scattermapbox.Marker(size=14, color='red'),
|
138 |
+
text=names,
|
139 |
+
hoverinfo="text"
|
140 |
+
))
|
141 |
+
|
142 |
+
fig.update_layout(
|
143 |
+
mapbox_style="open-street-map",
|
144 |
+
mapbox=dict(
|
145 |
+
center=go.layout.mapbox.Center(
|
146 |
+
# 以台灣中心點為地圖中心
|
147 |
+
lat=23.905987823498418,
|
148 |
+
lon=121.08291334460809
|
149 |
+
),
|
150 |
+
zoom=6
|
151 |
+
),
|
152 |
+
margin={"r": 0, "t": 0, "l": 0, "b": 0}
|
153 |
+
)
|
154 |
+
|
155 |
+
return fig
|
156 |
+
|
157 |
+
def handle_user_request(text):
|
158 |
+
# 生成地點建議
|
159 |
+
suggestions = generate_location_recommendations(text)
|
160 |
+
if len(suggestions[1]) > 0:
|
161 |
+
# 若有找到地點,則使用這份清單,生成一段行程指引
|
162 |
+
output = generate_travel_summary(suggestions[0], "\n".join(suggestions[1][:, 0]))
|
163 |
+
map_figure = plot_locations(suggestions[1])
|
164 |
+
return output, map_figure
|
165 |
+
else:
|
166 |
+
return "No locations found for the given input. Please try another query.", "No locations found."
|
167 |
+
|
168 |
+
# Gradio 介面
|
169 |
+
with gr.Blocks() as app:
|
170 |
+
gr.Markdown("# 🌏 ChatMap: Your AI-Powered Map Companion 🌏")
|
171 |
+
|
172 |
+
with gr.Row():
|
173 |
+
text_input = gr.Textbox(label="Tell me your command", value="我要去宜蘭玩,請推薦我至少5個熱門景點")
|
174 |
+
|
175 |
+
submit_btn = gr.Button("Generate")
|
176 |
+
|
177 |
+
output_summary = gr.Textbox(label="Here’s what I’ve mapped out for you", interactive=False)
|
178 |
+
map_display = gr.Plot(label="Visualizing your journey on the map")
|
179 |
+
|
180 |
+
submit_btn.click(handle_user_request, inputs=text_input, outputs=[output_summary, map_display])
|
181 |
+
|
182 |
+
# 啟動應用
|
183 |
+
app.launch()
|