接入通义千问api实现生成角色的功能 - Follow Up Deployment
Browse files- index.html +152 -144
index.html
CHANGED
@@ -55,17 +55,6 @@
|
|
55 |
transform: rotateY(0deg);
|
56 |
}
|
57 |
|
58 |
-
.progress-bar {
|
59 |
-
height: 8px;
|
60 |
-
border-radius: 4px;
|
61 |
-
overflow: hidden;
|
62 |
-
}
|
63 |
-
|
64 |
-
.progress-fill {
|
65 |
-
height: 100%;
|
66 |
-
transition: width 0.5s ease;
|
67 |
-
}
|
68 |
-
|
69 |
.generated-content {
|
70 |
animation: fadeIn 0.5s ease forwards;
|
71 |
}
|
@@ -140,94 +129,61 @@
|
|
140 |
<!-- 主内容区 -->
|
141 |
<main class="flex-grow py-8 px-4">
|
142 |
<div class="max-w-7xl mx-auto">
|
143 |
-
<!--
|
144 |
-
<div class="mb-10">
|
145 |
-
<div class="flex justify-between mb-3">
|
146 |
-
<div class="text-sm font-medium">角色设定</div>
|
147 |
-
<div class="text-sm font-medium">文字创作</div>
|
148 |
-
<div class="text-sm font-medium">声音生成</div>
|
149 |
-
<div class="text-sm font-medium">图片生成</div>
|
150 |
-
<div class="text-sm font-medium">视频合成</div>
|
151 |
-
</div>
|
152 |
-
<div class="progress-bar bg-slate-700 w-full">
|
153 |
-
<div class="progress-fill bg-gradient-to-r from-primary to-secondary" style="width: 40%"></div>
|
154 |
-
</div>
|
155 |
-
</div>
|
156 |
-
|
157 |
-
<!-- 步骤内容 -->
|
158 |
<div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
|
159 |
-
<!--
|
160 |
-
<div class="lg:col-span-
|
161 |
-
<
|
162 |
-
|
|
|
|
|
|
|
163 |
|
164 |
-
<div class="
|
165 |
-
<
|
166 |
-
<div class="flex items-center">
|
167 |
-
<
|
168 |
-
<span class="text-white font-bold">1</span>
|
169 |
-
</div>
|
170 |
-
<h3 class="font-semibold">角色设定</h3>
|
171 |
</div>
|
172 |
-
<
|
173 |
-
</
|
174 |
|
175 |
-
<
|
176 |
-
<div class="flex items-center">
|
177 |
-
<
|
178 |
-
<span class="text-slate-300 font-bold">2</span>
|
179 |
-
</div>
|
180 |
-
<h3 class="font-semibold text-slate-400">文字创作</h3>
|
181 |
</div>
|
182 |
-
<
|
183 |
-
</
|
184 |
|
185 |
-
<
|
186 |
-
<div class="flex items-center">
|
187 |
-
<
|
188 |
-
<span class="text-slate-300 font-bold">3</span>
|
189 |
-
</div>
|
190 |
-
<h3 class="font-semibold text-slate-400">声音生成</h3>
|
191 |
</div>
|
192 |
-
<
|
193 |
-
</
|
194 |
|
195 |
-
<
|
196 |
-
<div class="flex items-center">
|
197 |
-
<
|
198 |
-
<span class="text-slate-300 font-bold">4</span>
|
199 |
-
</div>
|
200 |
-
<h3 class="font-semibold text-slate-400">图片生成</h3>
|
201 |
</div>
|
202 |
-
<
|
203 |
-
</
|
204 |
|
205 |
-
<
|
206 |
-
<div class="flex items-center">
|
207 |
-
<
|
208 |
-
<span class="text-slate-300 font-bold">5</span>
|
209 |
-
</div>
|
210 |
-
<h3 class="font-semibold text-slate-400">视频合成</h3>
|
211 |
</div>
|
212 |
-
<
|
213 |
-
</div>
|
214 |
-
</div>
|
215 |
-
|
216 |
-
<div class="mt-8 flex justify-between">
|
217 |
-
<button class="px-5 py-2 rounded-lg bg-slate-700 hover:bg-slate-600 transition">
|
218 |
-
<i class="fas fa-arrow-left mr-2"></i>上一步
|
219 |
</button>
|
220 |
-
|
221 |
-
|
|
|
|
|
|
|
|
|
222 |
</button>
|
223 |
</div>
|
224 |
</div>
|
225 |
-
|
226 |
-
|
227 |
-
<!-- 右侧 - 素材卡片 -->
|
228 |
-
<div class="lg:col-span-2">
|
229 |
-
<h2 class="text-2xl font-bold mb-6">已生成素材</h2>
|
230 |
-
|
231 |
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
232 |
<!-- ���色卡片 -->
|
233 |
<div class="card relative h-80 cursor-pointer">
|
@@ -411,54 +367,6 @@
|
|
411 |
</div>
|
412 |
</div>
|
413 |
|
414 |
-
<!-- 生成新素材区域 -->
|
415 |
-
<div class="mt-8 bg-slate-800/50 backdrop-blur rounded-xl p-6 border border-slate-700">
|
416 |
-
<h3 class="text-xl font-bold mb-4">生成新素材</h3>
|
417 |
-
|
418 |
-
<div class="grid grid-cols-2 md:grid-cols-3 gap-4">
|
419 |
-
<button class="p-4 bg-slate-700/50 hover:bg-slate-700 rounded-lg transition flex flex-col items-center">
|
420 |
-
<div class="w-12 h-12 rounded-full bg-primary/20 flex items-center justify-center mb-2">
|
421 |
-
<i class="fas fa-user text-primary"></i>
|
422 |
-
</div>
|
423 |
-
<span>生成角色</span>
|
424 |
-
</button>
|
425 |
-
|
426 |
-
<button class="p-4 bg-slate-700/50 hover:bg-slate-700 rounded-lg transition flex flex-col items-center">
|
427 |
-
<div class="w-12 h-12 rounded-full bg-secondary/20 flex items-center justify-center mb-2">
|
428 |
-
<i class="fas fa-font text-secondary"></i>
|
429 |
-
</div>
|
430 |
-
<span>生成文字</span>
|
431 |
-
</button>
|
432 |
-
|
433 |
-
<button class="p-4 bg-slate-700/50 hover:bg-slate-700 rounded-lg transition flex flex-col items-center">
|
434 |
-
<div class="w-12 h-12 rounded-full bg-primary/20 flex items-center justify-center mb-2">
|
435 |
-
<i class="fas fa-volume-up text-primary"></i>
|
436 |
-
</div>
|
437 |
-
<span>生成声音</span>
|
438 |
-
</button>
|
439 |
-
|
440 |
-
<button class="p-4 bg-slate-700/50 hover:bg-slate-700 rounded-lg transition flex flex-col items-center">
|
441 |
-
<div class="w-12 h-12 rounded-full bg-secondary/20 flex items-center justify-center mb-2">
|
442 |
-
<i class="fas fa-image text-secondary"></i>
|
443 |
-
</div>
|
444 |
-
<span>生成图片</span>
|
445 |
-
</button>
|
446 |
-
|
447 |
-
<button class="p-4 bg-slate-700/50 hover:bg-slate-700 rounded-lg transition flex flex-col items-center">
|
448 |
-
<div class="w-12 h-12 rounded-full bg-primary/20 flex items-center justify-center mb-2">
|
449 |
-
<i class="fas fa-video text-primary"></i>
|
450 |
-
</div>
|
451 |
-
<span>生成视频</span>
|
452 |
-
</button>
|
453 |
-
|
454 |
-
<button class="p-4 bg-slate-700/50 hover:bg-slate-700 rounded-lg transition flex flex-col items-center">
|
455 |
-
<div class="w-12 h-12 rounded-full bg-secondary/20 flex items-center justify-center mb-2">
|
456 |
-
<i class="fas fa-random text-secondary"></i>
|
457 |
-
</div>
|
458 |
-
<span>随机生成</span>
|
459 |
-
</button>
|
460 |
-
</div>
|
461 |
-
</div>
|
462 |
</div>
|
463 |
</div>
|
464 |
</div>
|
@@ -479,24 +387,124 @@
|
|
479 |
</div>
|
480 |
|
481 |
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
482 |
// 卡片翻转效果
|
483 |
document.querySelectorAll('.card').forEach(card => {
|
484 |
-
card.addEventListener('click', function() {
|
|
|
|
|
|
|
|
|
485 |
this.classList.toggle('flipped');
|
486 |
});
|
487 |
});
|
488 |
|
489 |
-
// 模拟进度更新
|
490 |
-
let progress = 40;
|
491 |
-
const progressBar = document.querySelector('.progress-fill');
|
492 |
-
|
493 |
-
setInterval(() => {
|
494 |
-
if (progress < 100) {
|
495 |
-
progress += 0.5;
|
496 |
-
progressBar.style.width = `${progress}%`;
|
497 |
-
}
|
498 |
-
}, 1000);
|
499 |
-
|
500 |
// 生成内容动画
|
501 |
setTimeout(() => {
|
502 |
document.querySelectorAll('.generated-content').forEach(el => {
|
|
|
55 |
transform: rotateY(0deg);
|
56 |
}
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
.generated-content {
|
59 |
animation: fadeIn 0.5s ease forwards;
|
60 |
}
|
|
|
129 |
<!-- 主内容区 -->
|
130 |
<main class="flex-grow py-8 px-4">
|
131 |
<div class="max-w-7xl mx-auto">
|
132 |
+
<!-- 内容区域 -->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
<div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
|
134 |
+
<!-- 主内容区 -->
|
135 |
+
<div class="lg:col-span-3">
|
136 |
+
<h2 class="text-2xl font-bold mb-6">已生成素材</h2>
|
137 |
+
|
138 |
+
<!-- 生成新素材区域 -->
|
139 |
+
<div class="mb-8 bg-slate-800/50 backdrop-blur rounded-xl p-6 border border-slate-700">
|
140 |
+
<h3 class="text-xl font-bold mb-4">生成新素材</h3>
|
141 |
|
142 |
+
<div class="grid grid-cols-2 md:grid-cols-3 gap-4">
|
143 |
+
<button onclick="generateCharacter()" class="p-4 bg-slate-700/50 hover:bg-slate-700 rounded-lg transition flex flex-col items-center">
|
144 |
+
<div class="w-12 h-12 rounded-full bg-primary/20 flex items-center justify-center mb-2">
|
145 |
+
<i class="fas fa-user text-primary"></i>
|
|
|
|
|
|
|
146 |
</div>
|
147 |
+
<span>生成角色</span>
|
148 |
+
</button>
|
149 |
|
150 |
+
<button onclick="generateText()" class="p-4 bg-slate-700/50 hover:bg-slate-700 rounded-lg transition flex flex-col items-center">
|
151 |
+
<div class="w-12 h-12 rounded-full bg-secondary/20 flex items-center justify-center mb-2">
|
152 |
+
<i class="fas fa-font text-secondary"></i>
|
|
|
|
|
|
|
153 |
</div>
|
154 |
+
<span>生成文字</span>
|
155 |
+
</button>
|
156 |
|
157 |
+
<button onclick="generateAudio()" class="p-4 bg-slate-700/50 hover:bg-slate-700 rounded-lg transition flex flex-col items-center">
|
158 |
+
<div class="w-12 h-12 rounded-full bg-primary/20 flex items-center justify-center mb-2">
|
159 |
+
<i class="fas fa-volume-up text-primary"></i>
|
|
|
|
|
|
|
160 |
</div>
|
161 |
+
<span>生成声音</span>
|
162 |
+
</button>
|
163 |
|
164 |
+
<button onclick="generateImage()" class="p-4 bg-slate-700/50 hover:bg-slate-700 rounded-lg transition flex flex-col items-center">
|
165 |
+
<div class="w-12 h-12 rounded-full bg-secondary/20 flex items-center justify-center mb-2">
|
166 |
+
<i class="fas fa-image text-secondary"></i>
|
|
|
|
|
|
|
167 |
</div>
|
168 |
+
<span>生成图片</span>
|
169 |
+
</button>
|
170 |
|
171 |
+
<button onclick="generateVideo()" class="p-4 bg-slate-700/50 hover:bg-slate-700 rounded-lg transition flex flex-col items-center">
|
172 |
+
<div class="w-12 h-12 rounded-full bg-primary/20 flex items-center justify-center mb-2">
|
173 |
+
<i class="fas fa-video text-primary"></i>
|
|
|
|
|
|
|
174 |
</div>
|
175 |
+
<span>生成视频</span>
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
</button>
|
177 |
+
|
178 |
+
<button onclick="generateRandom()" class="p-4 bg-slate-700/50 hover:bg-slate-700 rounded-lg transition flex flex-col items-center">
|
179 |
+
<div class="w-12 h-12 rounded-full bg-secondary/20 flex items-center justify-center mb-2">
|
180 |
+
<i class="fas fa-random text-secondary"></i>
|
181 |
+
</div>
|
182 |
+
<span>随机生成</span>
|
183 |
</button>
|
184 |
</div>
|
185 |
</div>
|
186 |
+
|
|
|
|
|
|
|
|
|
|
|
187 |
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
188 |
<!-- ���色卡片 -->
|
189 |
<div class="card relative h-80 cursor-pointer">
|
|
|
367 |
</div>
|
368 |
</div>
|
369 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
370 |
</div>
|
371 |
</div>
|
372 |
</div>
|
|
|
387 |
</div>
|
388 |
|
389 |
<script>
|
390 |
+
// 通义千问API配置
|
391 |
+
const TONGYI_API_KEY = 'YOUR_API_KEY'; // 替换为你的API密钥
|
392 |
+
const TONGYI_API_URL = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
393 |
+
|
394 |
+
// 生成角色函数
|
395 |
+
async function generateCharacter() {
|
396 |
+
const loadingIndicator = document.createElement('div');
|
397 |
+
loadingIndicator.className = 'fixed inset-0 bg-black/50 flex items-center justify-center z-50';
|
398 |
+
loadingIndicator.innerHTML = `
|
399 |
+
<div class="bg-slate-800 p-6 rounded-xl flex flex-col items-center">
|
400 |
+
<div class="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-primary mb-4"></div>
|
401 |
+
<p>正在生成角色...</p>
|
402 |
+
</div>
|
403 |
+
`;
|
404 |
+
document.body.appendChild(loadingIndicator);
|
405 |
+
|
406 |
+
try {
|
407 |
+
const prompt = `请生成一个科幻主题的角色,包含以下信息:
|
408 |
+
1. 角色名称
|
409 |
+
2. 年龄
|
410 |
+
3. 职业
|
411 |
+
4. 性格特点
|
412 |
+
5. 简短背景故事
|
413 |
+
请用JSON格式返回数据`;
|
414 |
+
|
415 |
+
const response = await fetch(TONGYI_API_URL, {
|
416 |
+
method: 'POST',
|
417 |
+
headers: {
|
418 |
+
'Content-Type': 'application/json',
|
419 |
+
'Authorization': `Bearer ${TONGYI_API_KEY}`
|
420 |
+
},
|
421 |
+
body: JSON.stringify({
|
422 |
+
"model": "qwen-turbo",
|
423 |
+
"input": {
|
424 |
+
"messages": [
|
425 |
+
{
|
426 |
+
"role": "user",
|
427 |
+
"content": prompt
|
428 |
+
}
|
429 |
+
]
|
430 |
+
}
|
431 |
+
})
|
432 |
+
});
|
433 |
+
|
434 |
+
const data = await response.json();
|
435 |
+
if (data.output && data.output.text) {
|
436 |
+
try {
|
437 |
+
const characterData = JSON.parse(data.output.text);
|
438 |
+
updateCharacterCard(characterData);
|
439 |
+
} catch (e) {
|
440 |
+
console.error('Error parsing response:', e);
|
441 |
+
alert('生成成功但格式解析失败,请查看控制台');
|
442 |
+
}
|
443 |
+
} else {
|
444 |
+
throw new Error('API返回数据格式异常');
|
445 |
+
}
|
446 |
+
} catch (error) {
|
447 |
+
console.error('生成角色失败:', error);
|
448 |
+
alert('生成角色失败,请查看控制台');
|
449 |
+
} finally {
|
450 |
+
document.body.removeChild(loadingIndicator);
|
451 |
+
}
|
452 |
+
}
|
453 |
+
|
454 |
+
function updateCharacterCard(data) {
|
455 |
+
const card = document.querySelector('.card .card-front');
|
456 |
+
if (!card) return;
|
457 |
+
|
458 |
+
card.querySelector('h3').textContent = data.name || '未知角色';
|
459 |
+
card.querySelector('p').textContent = `${data.age || '未知年龄'} | ${data.profession || '未知职业'} | ${data.personality || '未知性格'}`;
|
460 |
+
|
461 |
+
// 触发动画效果
|
462 |
+
card.classList.add('generated-content');
|
463 |
+
setTimeout(() => {
|
464 |
+
card.style.opacity = 1;
|
465 |
+
card.style.transform = 'translateY(0)';
|
466 |
+
}, 100);
|
467 |
+
}
|
468 |
+
|
469 |
+
async function generateText() {
|
470 |
+
// 类似上面的实现
|
471 |
+
}
|
472 |
+
|
473 |
+
async function generateAudio() {
|
474 |
+
// 类似上面的实现
|
475 |
+
}
|
476 |
+
|
477 |
+
async function generateImage() {
|
478 |
+
// 类似上面的实现
|
479 |
+
}
|
480 |
+
|
481 |
+
async function generateVideo() {
|
482 |
+
// 类似上面的实现
|
483 |
+
}
|
484 |
+
|
485 |
+
async function generateRandom() {
|
486 |
+
// 测试用模拟数据
|
487 |
+
const mockData = {
|
488 |
+
name: "测试角色",
|
489 |
+
age: "30",
|
490 |
+
profession: "太空工程师",
|
491 |
+
personality: "严谨务实",
|
492 |
+
background: "负责飞船维护的资深工程师,对机械有着特殊感情"
|
493 |
+
};
|
494 |
+
updateCharacterCard(mockData);
|
495 |
+
}
|
496 |
+
|
497 |
// 卡片翻转效果
|
498 |
document.querySelectorAll('.card').forEach(card => {
|
499 |
+
card.addEventListener('click', function(e) {
|
500 |
+
// 防止按钮点击触发翻转
|
501 |
+
if (e.target.tagName === 'BUTTON' || e.target.closest('button')) {
|
502 |
+
return;
|
503 |
+
}
|
504 |
this.classList.toggle('flipped');
|
505 |
});
|
506 |
});
|
507 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
508 |
// 生成内容动画
|
509 |
setTimeout(() => {
|
510 |
document.querySelectorAll('.generated-content').forEach(el => {
|