335 lines
8.2 KiB
HTML
335 lines
8.2 KiB
HTML
<!doctype html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>AI古诗生成器</title>
|
||
<style>
|
||
* {
|
||
margin: 0;
|
||
padding: 0;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
body {
|
||
font-family: 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
min-height: 100vh;
|
||
padding: 20px;
|
||
color: #333;
|
||
}
|
||
|
||
.container {
|
||
max-width: 600px;
|
||
margin: 0 auto;
|
||
background: rgba(255, 255, 255, 0.95);
|
||
border-radius: 20px;
|
||
padding: 30px;
|
||
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
|
||
backdrop-filter: blur(10px);
|
||
}
|
||
|
||
.header {
|
||
text-align: center;
|
||
margin-bottom: 30px;
|
||
}
|
||
|
||
.title {
|
||
font-size: 2.5rem;
|
||
color: #4a5568;
|
||
margin-bottom: 10px;
|
||
font-weight: 300;
|
||
letter-spacing: 2px;
|
||
}
|
||
|
||
.subtitle {
|
||
color: #718096;
|
||
font-size: 1rem;
|
||
margin-bottom: 20px;
|
||
}
|
||
|
||
.form-group {
|
||
margin-bottom: 25px;
|
||
}
|
||
|
||
.form-label {
|
||
display: block;
|
||
margin-bottom: 8px;
|
||
font-weight: 500;
|
||
color: #4a5568;
|
||
font-size: 1rem;
|
||
}
|
||
|
||
.form-input {
|
||
width: 100%;
|
||
padding: 15px;
|
||
border: 2px solid #e2e8f0;
|
||
border-radius: 12px;
|
||
font-size: 1rem;
|
||
transition: all 0.3s ease;
|
||
background: #f7fafc;
|
||
}
|
||
|
||
.form-input:focus {
|
||
outline: none;
|
||
border-color: #667eea;
|
||
background: #fff;
|
||
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
|
||
}
|
||
|
||
.textarea {
|
||
resize: vertical;
|
||
min-height: 120px;
|
||
font-family: inherit;
|
||
}
|
||
|
||
.btn {
|
||
width: 100%;
|
||
padding: 15px;
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: white;
|
||
border: none;
|
||
border-radius: 12px;
|
||
font-size: 1.1rem;
|
||
font-weight: 500;
|
||
cursor: pointer;
|
||
transition: all 0.3s ease;
|
||
margin-bottom: 25px;
|
||
}
|
||
|
||
.btn:hover {
|
||
transform: translateY(-2px);
|
||
box-shadow: 0 10px 25px rgba(102, 126, 234, 0.3);
|
||
}
|
||
|
||
.btn:active {
|
||
transform: translateY(0);
|
||
}
|
||
|
||
.btn:disabled {
|
||
opacity: 0.6;
|
||
cursor: not-allowed;
|
||
transform: none;
|
||
}
|
||
|
||
.result-section {
|
||
margin-top: 30px;
|
||
}
|
||
|
||
.result-title {
|
||
font-size: 1.3rem;
|
||
color: #4a5568;
|
||
margin-bottom: 15px;
|
||
text-align: center;
|
||
font-weight: 500;
|
||
}
|
||
|
||
.poem-output {
|
||
background: #f7fafc;
|
||
border: 2px solid #e2e8f0;
|
||
border-radius: 12px;
|
||
padding: 25px;
|
||
min-height: 200px;
|
||
white-space: pre-wrap;
|
||
font-family: 'KaiTi', '楷体', serif;
|
||
font-size: 1.2rem;
|
||
line-height: 2;
|
||
text-align: center;
|
||
color: #2d3748;
|
||
letter-spacing: 1px;
|
||
}
|
||
|
||
.loading {
|
||
display: none;
|
||
text-align: center;
|
||
color: #667eea;
|
||
font-style: italic;
|
||
}
|
||
|
||
.error {
|
||
color: #e53e3e;
|
||
background: #fed7d7;
|
||
border: 1px solid #feb2b2;
|
||
padding: 15px;
|
||
border-radius: 8px;
|
||
margin-top: 15px;
|
||
}
|
||
|
||
@media (max-width: 768px) {
|
||
body {
|
||
padding: 10px;
|
||
}
|
||
|
||
.container {
|
||
padding: 20px;
|
||
margin: 10px;
|
||
}
|
||
|
||
.title {
|
||
font-size: 2rem;
|
||
}
|
||
|
||
.form-input {
|
||
padding: 12px;
|
||
}
|
||
|
||
.btn {
|
||
padding: 12px;
|
||
}
|
||
|
||
.poem-output {
|
||
padding: 20px;
|
||
font-size: 1.1rem;
|
||
}
|
||
}
|
||
|
||
@media (max-width: 480px) {
|
||
.title {
|
||
font-size: 1.8rem;
|
||
letter-spacing: 1px;
|
||
}
|
||
|
||
.container {
|
||
padding: 15px;
|
||
}
|
||
|
||
.poem-output {
|
||
font-size: 1rem;
|
||
padding: 15px;
|
||
}
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="container">
|
||
<div class="header">
|
||
<h1 class="title">AI古诗生成器</h1>
|
||
<p class="subtitle">让人工智能为您创作优美的中国古诗</p>
|
||
</div>
|
||
|
||
<!-- GitHub令牌已内置,无需用户输入 -->
|
||
|
||
<div class="form-group">
|
||
<label class="form-label" for="theme">作诗主题:</label>
|
||
<textarea
|
||
id="theme"
|
||
class="form-input textarea"
|
||
placeholder="请输入您想要的古诗主题,例如:春天的江南水乡、秋日的思乡之情、雪夜的孤独等..."
|
||
>春天的江南水乡</textarea>
|
||
</div>
|
||
|
||
<button id="generateBtn" class="btn">生成古诗</button>
|
||
|
||
<div class="result-section">
|
||
<h3 class="result-title">生成的古诗</h3>
|
||
<div id="loading" class="loading">正在创作中,请稍候...</div>
|
||
<div id="poemOutput" class="poem-output">点击"生成古诗"按钮,AI将为您创作优美的古诗</div>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
// GitHub API 配置
|
||
const GITHUB_TOKEN = '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';
|
||
const endpoint = 'https://models.github.ai/inference/chat/completions';
|
||
const generateBtn = document.getElementById('generateBtn');
|
||
const loading = document.getElementById('loading');
|
||
const poemOutput = document.getElementById('poemOutput');
|
||
|
||
// 专业的古诗生成提示词
|
||
const createPoemPrompt = (theme) => {
|
||
return `你是一位精通中国古典诗词的大师,请根据用户提供的主题创作一首优美的古诗。
|
||
|
||
要求:
|
||
1. 严格遵循中国古诗的格律和韵律
|
||
2. 可以是五言绝句、七言绝句、五言律诗或七言律诗
|
||
3. 注重意境的营造,体现中国传统文化的美感
|
||
4. 用词典雅,富有诗意
|
||
5. 根据主题选择合适的风格(豪放、婉约、田园、边塞等)
|
||
6. 确保押韵和平仄协调
|
||
7. 请先给诗歌起一个优美的标题,然后换行写出诗歌内容
|
||
8. 格式:标题\n诗歌正文
|
||
9. 注意排版对齐,标题居中,诗歌正文左对齐
|
||
10. 诗歌内容必须是中文
|
||
|
||
主题:${theme}
|
||
|
||
请创作一首古诗:`;
|
||
};
|
||
|
||
generateBtn.addEventListener('click', async () => {
|
||
const theme = document.getElementById('theme').value.trim();
|
||
|
||
if (!theme) {
|
||
alert('请输入作诗主题');
|
||
return;
|
||
}
|
||
|
||
// 显示加载状态
|
||
generateBtn.disabled = true;
|
||
generateBtn.textContent = '创作中...';
|
||
loading.style.display = 'block';
|
||
poemOutput.textContent = '';
|
||
|
||
// 清除之前的错误信息
|
||
const existingError = document.querySelector('.error');
|
||
if (existingError) {
|
||
existingError.remove();
|
||
}
|
||
|
||
const requestBody = {
|
||
model: "openai/gpt-4o-mini",
|
||
messages: [{
|
||
role: "user",
|
||
content: createPoemPrompt(theme)
|
||
}],
|
||
temperature: 0.8,
|
||
max_tokens: 500
|
||
};
|
||
|
||
try {
|
||
const response = await fetch(endpoint, {
|
||
method: 'POST',
|
||
headers: {
|
||
'Accept': 'application/vnd.github+json',
|
||
'Authorization': `Bearer ${GITHUB_TOKEN}`,
|
||
'X-GitHub-Api-Version': '2022-11-28',
|
||
'Content-Type': 'application/json'
|
||
},
|
||
body: JSON.stringify(requestBody)
|
||
});
|
||
|
||
const data = await response.json();
|
||
|
||
if (data?.choices?.[0]?.message?.content) {
|
||
poemOutput.textContent = data.choices[0].message.content.trim();
|
||
} else {
|
||
throw new Error('AI响应格式异常');
|
||
}
|
||
} catch (error) {
|
||
console.error('生成古诗时出错:', error);
|
||
|
||
// 显示错误信息
|
||
const errorDiv = document.createElement('div');
|
||
errorDiv.className = 'error';
|
||
errorDiv.textContent = `生成失败:${error.message}。请检查网络连接和访问令牌是否正确。`;
|
||
poemOutput.parentNode.appendChild(errorDiv);
|
||
|
||
poemOutput.textContent = '生成失败,请重试';
|
||
} finally {
|
||
// 恢复按钮状态
|
||
generateBtn.disabled = false;
|
||
generateBtn.textContent = '生成古诗';
|
||
loading.style.display = 'none';
|
||
}
|
||
});
|
||
|
||
// 回车键快捷生成
|
||
document.getElementById('theme').addEventListener('keypress', (e) => {
|
||
if (e.key === 'Enter' && !e.shiftKey) {
|
||
e.preventDefault();
|
||
generateBtn.click();
|
||
}
|
||
});
|
||
</script>
|
||
</body>
|
||
</html> |