-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
591 lines (498 loc) · 21.6 KB
/
app.py
File metadata and controls
591 lines (498 loc) · 21.6 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
from flask import Flask, request, jsonify, send_from_directory, render_template_string
import json
import os
import requests
import uuid
import threading
import re
from markupsafe import escape, Markup
from mcp.server.fastmcp import FastMCP
import dashscope
from dashscope.audio.qwen_tts import SpeechSynthesizer
app = Flask(__name__, static_folder='static')
# MCP服务实例
mcp = FastMCP(name="mood-route-service")
# 路线页面保存目录
ROUTES_DIR = 'routes'
# 配置文件路径
CONFIG_FILE = 'config.json'
# 预设路线文件路径
ROUTE_FILE = 'line_common.md'
# 心情标签列表
MOOD_TAGS = ['历史', '文化', '美食', '购物', '建筑', '自然', '夜间', '亲子', '摄影', '艺术']
def load_config():
"""加载配置文件"""
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
return {"api_key": "", "api_url": "", "model_name": ""}
def load_route():
"""加载预设路线内容"""
if os.path.exists(ROUTE_FILE):
with open(ROUTE_FILE, 'r', encoding='utf-8') as f:
return f.read()
return "路线信息暂未提供"
def text_to_speech(text):
"""使用千问TTS将文本转换为语音"""
config = load_config()
api_key = config.get('tts_api_key')
model_name = config.get('tts_model_name')
voice = config.get('tts_voice')
if not api_key or not model_name or not voice:
print("千问TTS配置不完整")
return None
try:
# 调用千问TTS服务
print(f"调用千问TTS服务,文本长度: {len(text)}")
response = SpeechSynthesizer.call(
model=model_name,
api_key=api_key,
text=text,
voice=voice
)
if response.status_code == 200:
# 提取音频URL
# 打印响应结构以便调试
print(f"千问TTS响应: {response}")
# 正确访问字典结构
audio_url = response['output']['audio']['url']
print(f"千问TTS调用成功,音频URL: {audio_url}")
return audio_url
else:
print(f"千问TTS调用失败,状态码: {response.status_code}")
return None
except Exception as e:
print(f"千问TTS调用错误: {e}")
return None
def identify_landmark_with_llm(image_base64):
"""使用多模态LLM识别景点"""
config = load_config()
api_key = config.get('multimodal_api_key', config.get('api_key'))
api_url = config.get('multimodal_api_url')
model_name = config.get('multimodal_model_name')
if not api_key or not api_url or not model_name:
return {
'name': '无法识别景点',
'description': 'API配置不完整,无法进行景点识别。'
}
try:
# 构建请求头
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
# 构建请求体 - 百炼模型格式
data = {
'model': model_name,
'messages': [
{
'role': 'system',
'content': '你是一个景点识别专家,请识别图片中的景点并提供简短介绍。请按照以下格式回答\n景点名称:[名称]\n景点介绍:[介绍内容]'
},
{
'role': 'user',
'content': [
{'type': 'text', 'text': '这是什么景点?请提供简短介绍。'},
{'type': 'image_url', 'image_url': {'url': f'data:image/jpeg;base64,{image_base64}'}}
]
}
],
'temperature': 0.7,
'max_tokens': 500
}
# 发送请求
print(f"发送多模态请求到: {api_url}")
# 禁用SSL验证以测试是否是证书问题
response = requests.post(api_url, headers=headers, json=data, verify=False)
response.raise_for_status()
# 解析响应
result = response.json()
print(f"多模态响应: {result}")
content = result.get('choices', [{}])[0].get('message', {}).get('content', '')
# 提取景点名称和描述
name = '未知景点'
description = '无法获取景点介绍'
# 尝试从格式化的回答中提取信息
name_match = re.search(r'景点名称:(.+)', content)
desc_match = re.search(r'景点介绍:(.+)', content, re.DOTALL)
if name_match:
name = name_match.group(1).strip()
if desc_match:
description = desc_match.group(1).strip()
# 如果没有找到格式化的回答,尝试直接使用全部内容
if name == '未知景点' and description == '无法获取景点介绍' and content:
lines = content.split('\n')
if lines:
name = lines[0].strip()
description = '\n'.join(lines[1:]).strip() if len(lines) > 1 else '无描述'
return {
'name': name,
'description': description
}
except Exception as e:
print(f"多模态LLM API调用错误: {e}")
return {
'name': '识别失败',
'description': f'在识别过程中发生错误: {str(e)}'
}
def analyze_mood(mood_description):
"""分析心情,匹配标签"""
config = load_config()
api_key = config.get('api_key')
api_url = config.get('api_url')
# 如果配置为空,使用简单的关键词匹配(模拟LLM API)
model_name = config.get('model_name')
if not api_key or not api_url or not model_name:
return simple_mood_analysis(mood_description)
# 实际调用LLM API
try:
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
data = {
'model': model_name,
'messages': [
{'role': 'system', 'content': f'你是一个心情分析专家。请分析用户的心情描述,并从以下标签中选择最匹配的1-3个标签:{", ".join(MOOD_TAGS)}。只返回标签名称,用逗号分隔。'},
{'role': 'user', 'content': mood_description}
],
'temperature': 0.7,
'max_tokens': 50
}
response = requests.post(api_url, headers=headers, json=data)
response.raise_for_status()
result = response.json()
tags = result.get('choices', [{}])[0].get('message', {}).get('content', '').split(',')
tags = [tag.strip() for tag in tags if tag.strip() in MOOD_TAGS]
# 确保至少返回一个标签
if not tags:
tags = ['文化'] # 默认标签
return tags
except Exception as e:
print(f"API调用错误: {e}")
return simple_mood_analysis(mood_description)
def get_route_for_mood(mood_description):
"""根据心情描述获取推荐路线"""
# 分析心情,获取标签
tags = analyze_mood(escape(mood_description))
# 获取路线内容
route_content = load_route()
return {
'tags': tags,
'route': route_content
}
def simple_mood_analysis(mood_description):
"""简单的关键词匹配(当API不可用时使用)"""
mood_description = mood_description.lower()
matched_tags = []
# 简单的关键词映射
keyword_map = {
'历史': ['历史', '古代', '传统', '过去', '年代', '古老'],
'文化': ['文化', '艺术', '传统', '习俗', '民俗'],
'美食': ['美食', '吃', '餐厅', '美味', '小吃', '饭店', '食物'],
'购物': ['购物', '买', '商场', '店铺', '市场', '商店'],
'建筑': ['建筑', '房子', '楼', '塔', '结构', '设计', '古建筑'],
'自然': ['自然', '公园', '花园', '树', '植物', '风景', '户外'],
'夜间': ['夜间', '夜晚', '夜生活', '灯光', '夜景'],
'亲子': ['亲子', '孩子', '家庭', '儿童', '小朋友', '亲子活动'],
'摄影': ['摄影', '拍照', '照片', '相机', '风景'],
'艺术': ['艺术', '展览', '博物馆', '画廊', '创意']
}
for tag, keywords in keyword_map.items():
for keyword in keywords:
if keyword in mood_description:
matched_tags.append(tag)
break
# 确保至少返回一个标签
if not matched_tags:
matched_tags = ['文化'] # 默认标签
# 最多返回3个标签
return matched_tags[:3]
@app.route('/')
def index():
"""提供前端页面"""
return send_from_directory('.', 'index.html')
@app.route('/static/<path:filename>')
def serve_static(filename):
"""提供静态文件"""
return send_from_directory('.', filename)
@app.route('/api/default_image')
def api_default_image():
"""提供默认图片数据"""
try:
with open('fiu.jpg', 'rb') as f:
image_data = f.read()
import base64
image_base64 = base64.b64encode(image_data).decode('utf-8')
return jsonify({
'image': image_base64
})
except Exception as e:
print(f"获取默认图片数据失败: {e}")
return jsonify({'error': '获取默认图片数据失败'}), 500
@app.route('/api/analyze_mood', methods=['POST'])
def api_analyze_mood():
"""心情分析API"""
data = request.json
mood = data.get('mood', '')
if not mood:
return jsonify({'error': '心情描述不能为空'}), 400
# 获取路线信息
result = get_route_for_mood(mood)
# 生成并保存路线页面
route_id = str(uuid.uuid4())
save_route_page(route_id, result['route'])
return jsonify({
'tags': result['tags'],
'route': result['route'],
'route_url': f'http://127.0.0.1:6001/routes/{route_id}'
})
@app.route('/routes/<route_id>')
def view_route(route_id):
"""查看保存的路线页面"""
route_file = os.path.join(ROUTES_DIR, f"{route_id}.html")
if not os.path.exists(route_file):
return "路线不存在", 404
with open(route_file, 'r', encoding='utf-8') as f:
return f.read()
@app.route('/api/identify_landmark', methods=['POST'])
def api_identify_landmark():
"""景点识别API"""
data = request.json
image_base64 = data.get('image', '')
if not image_base64:
return jsonify({'error': '图片数据不能为空'}), 400
# 调用多模态LLM识别景点
result = identify_landmark_with_llm(image_base64)
# 生成景点介绍文本
text = f"{result['name']}。{result['description']}"
# 调用TTS服务
audio_url = text_to_speech(text)
# 添加音频URL到结果中
result['audio_url'] = audio_url
return jsonify({
'landmark_name': result['name'],
'description': result['description'],
'audio_url': audio_url
})
def save_route_page(route_id, route_content):
"""保存路线页面"""
# 确保路线目录存在
os.makedirs(ROUTES_DIR, exist_ok=True)
# 创建HTML页面
html_template = '''
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WeWalk心情旅行 - 专属路线</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&display=swap');
body {
font-family: 'Noto Serif SC', 'Microsoft YaHei', Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #faf7f2; /* 优雅米色背景 */
color: #2c2417;
background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='%23d4bc8a' fill-opacity='0.05' fill-rule='evenodd'/%3E%3C/svg%3E"), linear-gradient(to bottom right, #faf7f2, #f1ede4);
min-height: 100vh;
}
.container {
max-width: 800px;
margin: 30px auto;
padding: 25px;
background-color: rgba(252, 249, 240, 0.95); /* 半透明背景 */
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1), 0 3px 10px rgba(0, 0, 0, 0.05);
border-radius: 12px;
border: 1px solid rgba(232, 225, 211, 0.7); /* 边框 */
backdrop-filter: blur(10px);
position: relative;
overflow: hidden;
}
.container::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
height: 3px;
background: linear-gradient(90deg, transparent, #d4bc8a, transparent);
opacity: 0.7;
}
h1 {
text-align: center;
color: #1a1207; /* 深黑色 */
margin-bottom: 30px;
font-weight: 600;
letter-spacing: 2px;
font-size: 2.2rem;
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
position: relative;
padding-bottom: 15px;
}
h1::after {
content: '';
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 100px;
height: 2px;
background: linear-gradient(to right, transparent, #d4bc8a, transparent);
}
.route-container {
margin-top: 30px;
border: 1px solid #d4bc8a; /* 金色边框 */
padding: 28px;
border-radius: 10px;
background-color: rgba(255, 253, 247, 0.8); /* 更浅的米色 */
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.05), inset 0 1px 2px rgba(255, 255, 255, 0.7);
background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='%23d4bc8a' fill-opacity='0.03' fill-rule='evenodd'/%3E%3C/svg%3E");
}
.route-content {
line-height: 1.8;
color: #2c2417;
}
h2 {
color: #1a1207;
border-bottom: 1px solid #d4bc8a;
padding-bottom: 10px;
margin-top: 25px;
font-weight: 600;
letter-spacing: 1px;
}
h3 {
color: #3a3225;
margin-top: 20px;
font-weight: 600;
padding-left: 5px;
border-left: 3px solid #d4bc8a;
}
strong {
color: #5a4e3a;
font-weight: 600;
}
ul {
padding-left: 20px;
}
li {
margin-bottom: 10px;
color: #2c2417;
}
p {
margin-bottom: 15px;
text-align: justify;
}
a.back-btn {
display: inline-block;
background: linear-gradient(135deg, #2c2417 0%, #1a1207 100%);
color: #f8f5ed;
text-decoration: none;
padding: 12px 25px;
border-radius: 10px;
margin-top: 20px;
text-align: center;
font-weight: 500;
letter-spacing: 1px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
transition: all 0.3s;
}
a.back-btn:hover {
background: linear-gradient(135deg, #3a3225 0%, #2c2417 100%);
transform: translateY(-3px);
box-shadow: 0 6px 15px rgba(0, 0, 0, 0.2);
}
.btn-container {
text-align: center;
margin-top: 30px;
}
</style>
</head>
<body>
<div class="container">
<h1>WeWalk心情旅行</h1>
<div class="route-container">
<div class="route-content">
{{ route_html }}
</div>
</div>
<div class="btn-container">
<a href="/" class="back-btn">返回首页</a>
</div>
</div>
</body>
</html>
'''
# 将Markdown转换为简单HTML
route_html = convert_markdown_to_html(route_content)
# 在应用上下文中渲染HTML模板
with app.app_context():
# 使用Markup标记HTML内容为安全,避免被转义
html_content = render_template_string(html_template, route_html=Markup(route_html))
# 保存HTML文件
route_file = os.path.join(ROUTES_DIR, f"{route_id}.html")
with open(route_file, 'w', encoding='utf-8') as f:
f.write(html_content)
def convert_markdown_to_html(markdown):
"""简单的Markdown转HTML函数"""
if not markdown:
return ''
# 转换标题
html = markdown
html = re.sub(r'^# (.*)$', r'<h1>\1</h1>', html, flags=re.MULTILINE)
html = re.sub(r'^## (.*)$', r'<h2>\1</h2>', html, flags=re.MULTILINE)
html = re.sub(r'^### (.*)$', r'<h3>\1</h3>', html, flags=re.MULTILINE)
html = re.sub(r'^#### (.*)$', r'<h4>\1</h4>', html, flags=re.MULTILINE)
# 转换粗体
html = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', html)
# 转换列表项
html = re.sub(r'^- (.*)$', r'<li>\1</li>', html, flags=re.MULTILINE)
# 将连续的列表项包裹在ul标签中
lines = html.split('\n')
in_list = False
result_lines = []
for line in lines:
if line.startswith('<li>'):
if not in_list:
result_lines.append('<ul>')
in_list = True
result_lines.append(line)
else:
if in_list:
result_lines.append('</ul>')
in_list = False
result_lines.append(line)
if in_list:
result_lines.append('</ul>')
html = '\n'.join(result_lines)
# 转换段落
html = re.sub(r'^(?!<[hul]|<li)(.+)$', r'<p>\1</p>', html, flags=re.MULTILINE)
return html
# MCP工具函数
@mcp.tool(name="plan_route_by_mood", description="根据心情描述规划城市路线,以url形式返回,请用户打开url查看内容")
def plan_route_by_mood(mood_description: str) -> str:
"""根据心情描述规划城市路线,返回路线页面URL"""
with app.app_context():
# 获取路线信息
result = get_route_for_mood(mood_description)
# 生成并保存路线页面
route_id = str(uuid.uuid4())
save_route_page(route_id, result['route'])
# 返回路线页面URL
return f"规划已完成,请访问提取:http://127.0.0.1:6001/routes/{route_id}"
def run_flask_app():
"""运行Flask应用"""
app.run(host='0.0.0.0', port=6001, debug=False)
def run_mcp_service():
"""运行MCP服务"""
mcp.run(transport='sse')
if __name__ == '__main__':
# 确保路线目录存在
os.makedirs(ROUTES_DIR, exist_ok=True)
# 创建并启动Flask线程
flask_thread = threading.Thread(target=run_flask_app)
flask_thread.daemon = True
flask_thread.start()
# 运行MCP服务(主线程)
run_mcp_service()