| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>图生文</title>
- <style>
- body {
- display: flex;
- justify-content: center;
- align-items: center;
- min-height: 100vh;
- padding: 20px;
- }
- .container {
- display: grid;
- container-type: inline-size;
- width: 100%;
- max-width: 900px;
- gap: 20px;
- }
- textarea {
- width: 100%;
- padding: 10px;
- min-height: 120px;
- }
- input[type="file"] {
- width: 100%;
- padding: 10px;
- }
- input[type="text"] {
- width: 100%;
- padding: 10px;
- }
- button {
- width: 100%;
- padding: 10px;
- }
- button:disabled {
- opacity: 0.6;
- }
- .loading {
- display: none;
- }
- .loading.active {
- display: block;
- }
- .spinner {
- width: 20px;
- height: 20px;
- border: 3px solid #f3f3f3;
- border-top: 3px solid #000;
- border-radius: 50%;
- animation: spin 1s linear infinite;
- display: inline-block;
- }
- @keyframes spin {
- 0% { transform: rotate(0deg); }
- 100% { transform: rotate(360deg); }
- }
- .result-box {
- padding: 20px;
- min-height: 200px;
- max-height: 500px;
- overflow-y: auto;
- white-space: pre-wrap;
- border: 1px solid #ccc;
- }
- .image-preview {
- max-width: 100%;
- max-height: 300px;
- margin: 10px 0;
- border: 1px solid #ccc;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <textarea id="prompt" placeholder="请输入提示词..."></textarea>
-
- <input type="file" id="imageFile" accept="image/*">
-
- <input type="text" id="imageUrl" placeholder="或输入图片URL">
-
- <img id="imagePreview" class="image-preview" style="display: none;">
-
- <button id="sendBtn" onclick="sendRequest()">发送</button>
- <div class="loading" id="loading">
- <div class="spinner"></div>
- <span>正在处理...</span>
- </div>
- <div class="result-box" id="resultBox"></div>
- </div>
- <script>
- const apiUrl = 'https://' + window.location.hostname;
- const promptTextarea = document.getElementById('prompt');
- const imageFileInput = document.getElementById('imageFile');
- const imageUrlInput = document.getElementById('imageUrl');
- const imagePreview = document.getElementById('imagePreview');
- const sendBtn = document.getElementById('sendBtn');
- const loadingDiv = document.getElementById('loading');
- const resultBox = document.getElementById('resultBox');
- let currentImageUrl = '';
- imageFileInput.addEventListener('change', (e) => {
- const file = e.target.files[0];
- if (file) {
- const reader = new FileReader();
- reader.onload = (event) => {
- currentImageUrl = event.target.result;
- imagePreview.src = currentImageUrl;
- imagePreview.style.display = 'block';
- imageUrlInput.value = '';
- };
- reader.readAsDataURL(file);
- }
- });
- imageUrlInput.addEventListener('input', (e) => {
- const url = e.target.value.trim();
- if (url) {
- currentImageUrl = url;
- imagePreview.src = url;
- imagePreview.style.display = 'block';
- imageFileInput.value = '';
- } else {
- currentImageUrl = '';
- imagePreview.style.display = 'none';
- }
- });
- async function sendRequest() {
- const prompt = promptTextarea.value.trim();
- if (!prompt) {
- alert('请输入提示词!');
- return;
- }
- if (!currentImageUrl) {
- alert('请选择图片或输入图片URL!');
- return;
- }
- sendBtn.disabled = true;
- loadingDiv.classList.add('active');
- resultBox.textContent = '';
- try {
- const response = await fetch(`${apiUrl}/api/img2text`, {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- prompt: prompt,
- imageUrl: currentImageUrl
- })
- });
- const data = await response.json();
- resultBox.textContent = data.success
- ? JSON.stringify(data.data, null, 2)
- : `错误: ${data.error}`;
- } catch (error) {
- resultBox.textContent = `请求失败: ${error.message}`;
- } finally {
- sendBtn.disabled = false;
- loadingDiv.classList.remove('active');
- }
- }
- promptTextarea.addEventListener('keydown', (e) => {
- if (e.key === 'Enter' && e.ctrlKey) sendRequest();
- });
- </script>
- </body>
- </html>
|