Files
InfoGenie/frontend/aimodelapp/AI写诗小助手/index.html
2025-09-07 22:00:47 +08:00

317 lines
7.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!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 src="env.js"></script>
<script>
// 从配置文件导入设置
// 配置在 env.js 文件中定义
const generateBtn = document.getElementById('generateBtn');
const loading = document.getElementById('loading');
const poemOutput = document.getElementById('poemOutput');
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: CONFIG.MODEL_NAME,
messages: [{
role: "user",
content: CONFIG.createPoemPrompt(theme)
}],
temperature: 0.8,
max_tokens: 500
};
try {
const response = await fetch(CONFIG.endpoint, {
method: 'POST',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `Bearer ${CONFIG.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>