Spaces:
Running
Running
Download index.html from aied-lab/Course_Demo: direct link, hf CLI and curl.
- Browser
- Download file 11.1 kB
-
https://huggingface.co/spaces/aied-lab/Course_Demo/resolve/refs%2Fpr%2F1/index.html
- Command line
-
hf download hf://spaces/aied-lab/Course_Demo@refs/pr/1/index.html
-
curl -L -o index.html https://huggingface.co/spaces/aied-lab/Course_Demo/resolve/refs%2Fpr%2F1/index.html
11.1 kB
| <html lang="zh-Hant"> | |
| // | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Gemma 多模態影像分析助手</title> | |
| <style> | |
| :root { | |
| --bg-color: #0b0f19; | |
| --card-bg: rgba(17, 24, 39, 0.75); | |
| --border-glow: rgba(0, 240, 255, 0.3); | |
| --neon-cyan: #00f0ff; | |
| --neon-purple: #b026ff; | |
| --text-main: #f3f4f6; | |
| --text-muted: #9ca3af; | |
| } | |
| body { | |
| font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; | |
| max-width: 650px; | |
| margin: 40px auto; | |
| padding: 20px; | |
| background-color: var(--bg-color); | |
| background-image: | |
| radial-gradient(circle at 10% 20%, rgba(176, 38, 255, 0.1) 0%, transparent 40%), | |
| radial-gradient(circle at 90% 80%, rgba(0, 240, 255, 0.1) 0%, transparent 40%); | |
| color: var(--text-main); | |
| min-height: 100vh; | |
| } | |
| .card { | |
| background: var(--card-bg); | |
| backdrop-filter: blur(16px); | |
| -webkit-backdrop-filter: blur(16px); | |
| padding: 30px; | |
| border-radius: 16px; | |
| border: 1px solid var(--border-glow); | |
| box-shadow: 0 0 30px rgba(0, 240, 255, 0.1), inset 0 0 15px rgba(176, 38, 255, 0.05); | |
| position: relative; | |
| overflow: hidden; | |
| } | |
| .card::before { | |
| content: ''; | |
| position: absolute; | |
| top: 0; | |
| left: -100%; | |
| width: 100%; | |
| height: 2px; | |
| background: linear-gradient(90deg, transparent, var(--neon-cyan), transparent); | |
| animation: scanline 4s linear infinite; | |
| } | |
| @keyframes scanline { | |
| 0% { left: -100%; } | |
| 100% { left: 100%; } | |
| } | |
| h2 { | |
| margin-top: 0; | |
| font-size: 1.8rem; | |
| background: linear-gradient(135deg, var(--neon-cyan), var(--neon-purple)); | |
| -webkit-background-clip: text; | |
| -webkit-text-fill-color: transparent; | |
| letter-spacing: 0.5px; | |
| } | |
| p { | |
| color: var(--text-muted); | |
| font-size: 0.95rem; | |
| margin-bottom: 24px; | |
| } | |
| .form-group { | |
| margin-bottom: 20px; | |
| } | |
| label { | |
| display: block; | |
| font-weight: 600; | |
| margin-bottom: 8px; | |
| font-size: 0.9rem; | |
| color: var(--neon-cyan); | |
| letter-spacing: 0.5px; | |
| } | |
| input[type="password"], | |
| input[type="text"], | |
| input[type="file"], | |
| textarea { | |
| width: 100%; | |
| padding: 12px 14px; | |
| background: rgba(3, 7, 18, 0.6); | |
| border: 1px solid rgba(255, 255, 255, 0.1); | |
| border-radius: 8px; | |
| box-sizing: border-box; | |
| color: var(--text-main); | |
| font-size: 0.95rem; | |
| transition: all 0.3s ease; | |
| } | |
| input:focus { | |
| outline: none; | |
| border-color: var(--neon-cyan); | |
| box-shadow: 0 0 10px rgba(0, 240, 255, 0.3); | |
| } | |
| input[type="file"]::file-selector-button { | |
| background: rgba(0, 240, 255, 0.1); | |
| color: var(--neon-cyan); | |
| border: 1px solid var(--neon-cyan); | |
| padding: 6px 12px; | |
| border-radius: 4px; | |
| cursor: pointer; | |
| transition: all 0.2s; | |
| } | |
| input[type="file"]::file-selector-button:hover { | |
| background: var(--neon-cyan); | |
| color: #000; | |
| } | |
| button { | |
| background: linear-gradient(135deg, var(--neon-cyan), var(--neon-purple)); | |
| color: #000; | |
| border: none; | |
| padding: 12px 20px; | |
| font-weight: 700; | |
| border-radius: 8px; | |
| cursor: pointer; | |
| width: 100%; | |
| font-size: 1rem; | |
| letter-spacing: 1px; | |
| transition: all 0.3s ease; | |
| box-shadow: 0 4px 15px rgba(0, 240, 255, 0.2); | |
| } | |
| button:hover { | |
| opacity: 0.9; | |
| transform: translateY(-1px); | |
| box-shadow: 0 6px 20px rgba(176, 38, 255, 0.4); | |
| } | |
| button:disabled { | |
| background: #374151; | |
| color: #9ca3af; | |
| cursor: not-allowed; | |
| box-shadow: none; | |
| transform: none; | |
| } | |
| #preview-container { | |
| margin-top: 12px; | |
| text-align: center; | |
| } | |
| #imagePreview { | |
| max-width: 100%; | |
| max-height: 250px; | |
| border-radius: 8px; | |
| display: none; | |
| border: 1px solid var(--border-glow); | |
| box-shadow: 0 4px 12px rgba(0,0,0,0.5); | |
| } | |
| #result { | |
| margin-top: 12px; | |
| padding: 16px; | |
| background: rgba(3, 7, 18, 0.8); | |
| border: 1px solid rgba(255, 255, 255, 0.08); | |
| border-radius: 8px; | |
| white-space: pre-wrap; | |
| min-height: 60px; | |
| font-size: 0.95rem; | |
| color: #e5e7eb; | |
| box-shadow: inset 0 2px 4px rgba(0,0,0,0.4); | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="card"> | |
| <h2>Gemma 多模態影像分析助手</h2> | |
| <p>透過 Hugging Face Router 呼叫 gemma-4-31B-it 模型進行高精度影像辨識與分析。</p> | |
| <div class="form-group"> | |
| <label for="apiKey">Hugging Face Token (HF_TOKEN)</label> | |
| <input type="password" id="apiKey" placeholder="請輸入您的 HF_TOKEN"> | |
| </div> | |
| <div class="form-group"> | |
| <label for="imageInput">上傳圖片</label> | |
| <input type="file" id="imageInput" accept="image/*" onchange="previewSelectedImage(event)"> | |
| <div id="preview-container"> | |
| <img id="imagePreview" alt="圖片預覽"> | |
| </div> | |
| </div> | |
| <div class="form-group"> | |
| <label for="promptInput">輸入提示詞 (Prompt)</label> | |
| <input type="text" id="promptInput" value="Describe this image in one sentence in chinese"> | |
| </div> | |
| <button id="submitBtn" onclick="analyzeImage()">啟動分析</button> | |
| <div class="form-group" style="margin-top: 24px;"> | |
| <label>模型回應</label> | |
| <div id="result">系統待命,尚未開始分析...</div> | |
| </div> | |
| </div> | |
| <script> | |
| // 選擇圖片時即時顯示預覽 | |
| function previewSelectedImage(event) { | |
| const file = event.target.files[0]; | |
| const preview = document.getElementById('imagePreview'); | |
| if (file) { | |
| const reader = new FileReader(); | |
| reader.onload = function(e) { | |
| preview.src = e.target.result; | |
| preview.style.display = 'inline-block'; | |
| } | |
| reader.readAsDataURL(file); | |
| } else { | |
| preview.src = ''; | |
| preview.style.display = 'none'; | |
| } | |
| } | |
| // 壓縮並轉成 Base64 的前端實作 | |
| function compressImage(file, maxSize = 1024, quality = 0.85) { | |
| return new Promise((resolve, reject) => { | |
| const reader = new FileReader(); | |
| reader.onload = function(e) { | |
| const img = new Image(); | |
| img.onload = function() { | |
| let width = img.width; | |
| let height = img.height; | |
| if (width > height) { | |
| if (width > maxSize) { | |
| height *= maxSize / width; | |
| width = maxSize; | |
| } | |
| } else { | |
| if (height > maxSize) { | |
| width *= maxSize / height; | |
| height = maxSize; | |
| } | |
| } | |
| const canvas = document.createElement('canvas'); | |
| canvas.width = width; | |
| canvas.height = height; | |
| const ctx = canvas.getContext('2d'); | |
| ctx.drawImage(img, 0, 0, width, height); | |
| const dataUrl = canvas.toDataURL('image/jpeg', quality); | |
| resolve(dataUrl); | |
| }; | |
| img.src = e.target.result; | |
| }; | |
| reader.onerror = reject; | |
| reader.readAsDataURL(file); | |
| }); | |
| } | |
| async function analyzeImage() { | |
| const apiKey = document.getElementById('apiKey').value.trim(); | |
| const imageFile = document.getElementById('imageInput').files[0]; | |
| let prompt = document.getElementById('promptInput').value.trim(); | |
| const resultDiv = document.getElementById('result'); | |
| const submitBtn = document.getElementById('submitBtn'); | |
| if (!apiKey) { | |
| alert('請先輸入 Hugging Face Token!'); | |
| return; | |
| } | |
| if (!imageFile) { | |
| alert('請先上傳一張圖片!'); | |
| return; | |
| } | |
| if (!prompt) { | |
| prompt = "Describe this image in one sentence."; | |
| } | |
| submitBtn.disabled = true; | |
| resultDiv.innerText = '處理中,神經網路運算中...'; | |
| try { | |
| const base64Image = await compressImage(imageFile); | |
| const response = await fetch("https://router.huggingface.co/v1/chat/completions", { | |
| method: "POST", | |
| headers: { | |
| "Authorization": `Bearer ${apiKey}`, | |
| "Content-Type": "application/json" | |
| }, | |
| body: JSON.stringify({ | |
| model: "google/gemma-4-31B-it:novita", | |
| messages: [{ | |
| role: "user", | |
| content: [ | |
| { type: "text", text: prompt }, | |
| { type: "image_url", image_url: { url: base64Image } } | |
| ] | |
| }], | |
| max_tokens: 300 | |
| }) | |
| }); | |
| const data = await response.json(); | |
| if (response.ok && data.choices && data.choices.length > 0) { | |
| resultDiv.innerText = data.choices[0].message.content; | |
| } else { | |
| resultDiv.innerText = `發生錯誤:${data.error?.message || JSON.stringify(data)}`; | |
| } | |
| } catch (error) { | |
| resultDiv.innerText = `發生錯誤:${error.message}`; | |
| } finally { | |
| submitBtn.disabled = false; | |
| } | |
| } | |
| </script> | |
| </body> | |
| </html> |