Spaces:
Running
Running
ช่วยทำเว็บแอพ พี่สามารถถ่ายรูปตัวเอง โดยใช้การเปิดกล้องเท่านั้น และอัพโหลดรูปเข้าไปที่ ChatGPT ผ่าน API key เพื่อที่จะแปลงรูปตัวเอง ให้เป็นรูปคนที่หุ่นดี fit & firm ได้ ทั้งเพศหญิง และเพศชาย โดยที่หน้าไม่เปลี่ยน แต่เปลี่ยนหุ่น และชุดเป็นชุดฟิตเนส เน้นถึงกล้ามท้อง และสัดส่วนที่เหมาะสมกับคนออกกำลังกายเป็นอย่างดี
8bbd13a verified Download script.js from CSNET/fittransform-pro: direct link, hf CLI and curl.
- Browser
- Download file 10.1 kB
-
https://huggingface.co/spaces/CSNET/fittransform-pro/resolve/main/script.js
- Command line
-
hf download hf://spaces/CSNET/fittransform-pro/script.js
-
curl -L -o script.js https://huggingface.co/spaces/CSNET/fittransform-pro/resolve/main/script.js
10.1 kB
| let stream = null; | |
| let selectedGender = null; | |
| let capturedImageData = null; | |
| // DOM Elements | |
| const video = document.getElementById('video'); | |
| const canvas = document.getElementById('canvas'); | |
| const placeholder = document.getElementById('placeholder'); | |
| const startCameraBtn = document.getElementById('startCamera'); | |
| const capturePhotoBtn = document.getElementById('capturePhoto'); | |
| const transformBtn = document.getElementById('transformBtn'); | |
| const downloadBtn = document.getElementById('downloadBtn'); | |
| const originalImage = document.getElementById('originalImage'); | |
| const transformedImage = document.getElementById('transformedImage'); | |
| const resultPlaceholder = document.getElementById('resultPlaceholder'); | |
| const loadingIndicator = document.getElementById('loadingIndicator'); | |
| const successMessage = document.getElementById('successMessage'); | |
| const errorMessage = document.getElementById('errorMessage'); | |
| const errorText = document.getElementById('errorText'); | |
| const comparisonContainer = document.getElementById('comparisonContainer'); | |
| const apiKeyInput = document.getElementById('apiKey'); | |
| const fitnessLevel = document.getElementById('fitnessLevel'); | |
| // Gender selection | |
| document.querySelectorAll('.gender-btn').forEach(btn => { | |
| btn.addEventListener('click', function() { | |
| document.querySelectorAll('.gender-btn').forEach(b => b.classList.remove('active')); | |
| this.classList.add('active'); | |
| selectedGender = this.dataset.gender; | |
| checkTransformReady(); | |
| }); | |
| }); | |
| // Start Camera | |
| startCameraBtn.addEventListener('click', async () => { | |
| try { | |
| stream = await navigator.mediaDevices.getUserMedia({ | |
| video: { | |
| facingMode: 'user', | |
| width: { ideal: 1280 }, | |
| height: { ideal: 1600 } | |
| } | |
| }); | |
| video.srcObject = stream; | |
| video.classList.remove('hidden'); | |
| placeholder.classList.add('hidden'); | |
| capturePhotoBtn.disabled = false; | |
| startCameraBtn.innerHTML = '<i data-feather="camera-off" class="inline mr-2 w-5 h-5"></i>ปิดกล้อง'; | |
| startCameraBtn.onclick = stopCamera; | |
| video.parentElement.classList.add('camera-active'); | |
| feather.replace(); | |
| } catch (err) { | |
| showError('ไม่สามารถเข้าถึงกล้องได้: ' + err.message); | |
| } | |
| }); | |
| // Stop Camera | |
| function stopCamera() { | |
| if (stream) { | |
| stream.getTracks().forEach(track => track.stop()); | |
| stream = null; | |
| } | |
| video.classList.add('hidden'); | |
| placeholder.classList.remove('hidden'); | |
| capturePhotoBtn.disabled = true; | |
| startCameraBtn.innerHTML = '<i data-feather="camera" class="inline mr-2 w-5 h-5"></i>เปิดกล้อง'; | |
| startCameraBtn.onclick = () => startCameraBtn.click(); | |
| video.parentElement.classList.remove('camera-active'); | |
| feather.replace(); | |
| } | |
| // Capture Photo | |
| capturePhotoBtn.addEventListener('click', () => { | |
| canvas.width = video.videoWidth; | |
| canvas.height = video.videoHeight; | |
| const ctx = canvas.getContext('2d'); | |
| ctx.drawImage(video, 0, 0); | |
| capturedImageData = canvas.toDataURL('image/jpeg', 0.95); | |
| originalImage.src = capturedImageData; | |
| originalImage.classList.remove('hidden'); | |
| resultPlaceholder.classList.add('hidden'); | |
| stopCamera(); | |
| checkTransformReady(); | |
| // Show capture animation | |
| canvas.style.display = 'block'; | |
| setTimeout(() => { | |
| canvas.style.display = 'none'; | |
| }, 500); | |
| }); | |
| // Check if ready to transform | |
| function checkTransformReady() { | |
| if (capturedImageData && selectedGender && apiKeyInput.value.trim()) { | |
| transformBtn.disabled = false; | |
| } else { | |
| transformBtn.disabled = true; | |
| } | |
| } | |
| apiKeyInput.addEventListener('input', checkTransformReady); | |
| // Transform Image | |
| transformBtn.addEventListener('click', async () => { | |
| if (!capturedImageData || !selectedGender || !apiKeyInput.value.trim()) { | |
| showError('กรุณากรอกข้อมูลให้ครบถ้วน'); | |
| return; | |
| } | |
| loadingIndicator.classList.remove('hidden'); | |
| successMessage.classList.add('hidden'); | |
| errorMessage.classList.add('hidden'); | |
| transformBtn.disabled = true; | |
| try { | |
| // Convert data URL to Blob | |
| const response = await fetch(capturedImageData); | |
| const blob = await response.blob(); | |
| // Create FormData for API request | |
| const formData = new FormData(); | |
| formData.append('image', blob, 'photo.jpg'); | |
| // Create prompt based on selections | |
| const prompt = `Transform the body to a healthy, athletic "${fitnessLevel.value}" physique while preserving the original face identity. Keep natural skin tone consistency, realistic muscle definition (focus on abs and overall proportion), and replace outfit with tasteful modern fitness wear appropriate for "${selectedGender}". Avoid nudity; keep the style photorealistic and respectful.`; | |
| // Call OpenAI API (using DALL-E 3 for image editing) | |
| const apiResponse = await fetch('https://api.openai.com/v1/images/edits', { | |
| method: 'POST', | |
| headers: { | |
| 'Authorization': `Bearer ${apiKeyInput.value.trim()}`, | |
| }, | |
| body: formData | |
| }); | |
| if (!apiResponse.ok) { | |
| const errorData = await apiResponse.json(); | |
| throw new Error(errorData.error?.message || 'API request failed'); | |
| } | |
| const result = await apiResponse.json(); | |
| // Load transformed image | |
| const img = new Image(); | |
| img.onload = () => { | |
| transformedImage.src = result.data[0].url; | |
| transformedImage.classList.remove('hidden'); | |
| originalImage.classList.add('hidden'); | |
| downloadBtn.classList.remove('hidden'); | |
| successMessage.classList.remove('hidden'); | |
| downloadBtn.dataset.url = result.data[0].url; | |
| // Setup comparison view | |
| setupComparison(); | |
| }; | |
| img.src = result.data[0].url; | |
| } catch (error) { | |
| showError('การแปลงรูปล้มเหลว: ' + error.message); | |
| } finally { | |
| loadingIndicator.classList.add('hidden'); | |
| transformBtn.disabled = false; | |
| checkTransformReady(); | |
| } | |
| }); | |
| // Setup before/after comparison | |
| function setupComparison() { | |
| const compareOriginal = document.getElementById('compareOriginal'); | |
| const compareTransformed = document.getElementById('compareTransformed'); | |
| const compareTransformedWrapper = document.getElementById('compareTransformedWrapper'); | |
| const slider = document.getElementById('slider'); | |
| compareOriginal.src = originalImage.src; | |
| compareTransformed.src = transformedImage.src; | |
| let isSliding = false; | |
| slider.addEventListener('mousedown', () => isSliding = true); | |
| document.addEventListener('mouseup', () => isSliding = false); | |
| document.addEventListener('mousemove', (e) => { | |
| if (!isSliding) return; | |
| const rect = comparisonContainer.getBoundingClientRect(); | |
| const x = e.clientX - rect.left; | |
| const percentage = (x / rect.width) * 100; | |
| if (percentage >= 0 && percentage <= 100) { | |
| slider.style.left = percentage + '%'; | |
| compareTransformedWrapper.style.width = percentage + '%'; | |
| } | |
| }); | |
| // Touch support | |
| slider.addEventListener('touchstart', () => isSliding = true); | |
| document.addEventListener('touchend', () => isSliding = false); | |
| document.addEventListener('touchmove', (e) => { | |
| if (!isSliding) return; | |
| const rect = comparisonContainer.getBoundingClientRect(); | |
| const x = e.touches[0].clientX - rect.left; | |
| const percentage = (x / rect.width) * 100; | |
| if (percentage >= 0 && percentage <= 100) { | |
| slider.style.left = percentage + '%'; | |
| compareTransformedWrapper.style.width = percentage + '%'; | |
| } | |
| }); | |
| } | |
| // Download Image | |
| downloadBtn.addEventListener('click', () => { | |
| const link = document.createElement('a'); | |
| link.href = downloadBtn.dataset.url || transformedImage.src; | |
| link.download = `fittransform-${Date.now()}.jpg`; | |
| link.click(); | |
| }); | |
| // Toggle View | |
| function toggleComparison() { | |
| if (comparisonContainer.classList.contains('hidden')) { | |
| comparisonContainer.classList.remove('hidden'); | |
| transformedImage.parentElement.classList.add('hidden'); | |
| } else { | |
| comparisonContainer.classList.add('hidden'); | |
| transformedImage.parentElement.classList.remove('hidden'); | |
| } | |
| } | |
| // Show error message | |
| function showError(message) { | |
| errorText.textContent = message; | |
| errorMessage.classList.remove('hidden'); | |
| setTimeout(() => { | |
| errorMessage.classList.add('hidden'); | |
| }, 5000); | |
| } | |
| // Check for required APIs | |
| if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) { | |
| showError('เบราว์เซอร์ของคุณไม่รองรับการเข้าถึงกล้อง'); | |
| } | |
| // Auto-save API key to localStorage | |
| if (localStorage.getItem('openai_api_key')) { | |
| apiKeyInput.value = localStorage.getItem('openai_api_key'); | |
| checkTransformReady(); | |
| } | |
| apiKeyInput.addEventListener('change', () => { | |
| if (apiKeyInput.value.trim()) { | |
| localStorage.setItem('openai_api_key', apiKeyInput.value.trim()); | |
| } | |
| }); | |
| // Initialize tooltips and interactions | |
| document.addEventListener('DOMContentLoaded', () => { | |
| // Add ripple effect to buttons | |
| document.querySelectorAll('button').forEach(button => { | |
| button.classList.add('ripple'); | |
| }); | |
| // Smooth scroll for anchor links | |
| document.querySelectorAll('a[href^="#"]').forEach(anchor => { | |
| anchor.addEventListener('click', function (e) { | |
| e.preventDefault(); | |
| const target = document.querySelector(this.getAttribute('href')); | |
| if (target) { | |
| target.scrollIntoView({ behavior: 'smooth' }); | |
| } | |
| }); | |
| }); | |
| }); |