Spaces:
Sleeping
Sleeping
Download frontend/script.js from kodekdevs/kodek-roomrestyler: direct link, hf CLI and curl.
- Browser
- Download file 7.14 kB
-
https://huggingface.co/spaces/kodekdevs/kodek-roomrestyler/resolve/main/frontend/script.js
- Command line
-
hf download hf://spaces/kodekdevs/kodek-roomrestyler/frontend/script.js
-
curl -L -o script.js https://huggingface.co/spaces/kodekdevs/kodek-roomrestyler/resolve/main/frontend/script.js
7.14 kB
| import { initializeApp } from "https://www.gstatic.com/firebasejs/9.22.1/firebase-app.js"; | |
| import { | |
| getAuth, | |
| GoogleAuthProvider, | |
| signInWithPopup, | |
| createUserWithEmailAndPassword, | |
| signInWithEmailAndPassword, | |
| onAuthStateChanged, | |
| signOut | |
| } from "https://www.gstatic.com/firebasejs/9.22.1/firebase-auth.js"; | |
| import { | |
| getFirestore, | |
| doc, | |
| getDoc, | |
| setDoc, | |
| updateDoc, | |
| serverTimestamp | |
| } from "https://www.gstatic.com/firebasejs/9.22.1/firebase-firestore.js"; | |
| // === Firebase config === | |
| const firebaseConfig = { | |
| apiKey: "AIzaSyD89ncdY0AqNeYn-kpgdjlyfDKgsfgA1hk", | |
| authDomain: "roomrestyler.firebaseapp.com", | |
| projectId: "roomrestyler", | |
| storageBucket: "roomrestyler.firebasestorage.app", | |
| messagingSenderId: "997195017503", | |
| appId: "1:997195017503:web:6e34afb2c2a5ce0a3e142a", | |
| measurementId: "G-2DL70136H5" | |
| }; | |
| const app = initializeApp(firebaseConfig); | |
| const auth = getAuth(app); | |
| const db = getFirestore(app); | |
| // === DOM refs === | |
| const authForm = document.getElementById("authForm"); | |
| const userInfo = document.getElementById("userInfo"); | |
| const userEmailEl = document.getElementById("userEmail"); | |
| const tokenCount = document.getElementById("tokenCount"); | |
| const toolbox = document.getElementById("toolbox"); | |
| const uploadSection = document.getElementById("uploadSection"); | |
| const imageInput = document.getElementById("imageInput"); | |
| const preview = document.getElementById("preview"); | |
| const result = document.getElementById("result"); | |
| const loader = document.getElementById("loader"); | |
| // === Google Sign-In === | |
| async function googleLogin() { | |
| const provider = new GoogleAuthProvider(); | |
| try { | |
| const result = await signInWithPopup(auth, provider); | |
| const user = result.user; | |
| const userRef = doc(db, "users", user.uid); | |
| const snap = await getDoc(userRef); | |
| // If first-time Google sign-in, give them tokens | |
| if (!snap.exists()) { | |
| await setDoc(userRef, { | |
| email: user.email, | |
| tokens: 10, | |
| createdAt: serverTimestamp() | |
| }); | |
| } | |
| // Update token count immediately | |
| const updatedSnap = await getDoc(userRef); | |
| const tokens = updatedSnap.exists() ? updatedSnap.data().tokens : 0; | |
| tokenCount.innerText = tokens; | |
| alert("Logged in with Google!"); | |
| } catch (err) { | |
| console.error(err); | |
| alert("Google sign-in failed: " + err.message); | |
| } | |
| } | |
| // === Email/Password Sign-Up === | |
| async function signUp() { | |
| const email = document.getElementById("email").value; | |
| const password = document.getElementById("password").value; | |
| try { | |
| const cred = await createUserWithEmailAndPassword(auth, email, password); | |
| const userRef = doc(db, "users", cred.user.uid); | |
| await setDoc(userRef, { | |
| email, | |
| tokens: 10, | |
| createdAt: serverTimestamp() | |
| }); | |
| // Update token count immediately | |
| tokenCount.innerText = 10; | |
| alert("Signed up successfully! You’ve received 10 free tokens."); | |
| } catch (err) { | |
| console.error(err); | |
| alert("Sign-up failed: " + err.message); | |
| } | |
| } | |
| // === Email/Password Login === | |
| async function logIn() { | |
| const email = document.getElementById("email").value; | |
| const password = document.getElementById("password").value; | |
| try { | |
| const cred = await signInWithEmailAndPassword(auth, email, password); | |
| const userRef = doc(db, "users", cred.user.uid); | |
| const snap = await getDoc(userRef); | |
| // Update token count immediately | |
| const tokens = snap.exists() ? snap.data().tokens : 0; | |
| tokenCount.innerText = tokens; | |
| alert("Logged in successfully!"); | |
| } catch (err) { | |
| console.error(err); | |
| alert("Login failed: " + err.message); | |
| } | |
| } | |
| async function logOut() { | |
| try { | |
| await signOut(auth); | |
| alert("Logged out successfully."); | |
| } catch (err) { | |
| console.error(err); | |
| alert("Logout failed: " + err.message); | |
| } | |
| } | |
| // === Auth State Listener === | |
| onAuthStateChanged(auth, async (user) => { | |
| if (user) { | |
| authForm.style.display = "none"; | |
| userInfo.style.display = "flex"; | |
| toolbox.style.display = "block"; | |
| // Update user email and token count | |
| userEmailEl.innerText = user.email; | |
| const snap = await getDoc(doc(db, "users", user.uid)); | |
| const tokens = snap.exists() ? snap.data().tokens : 0; | |
| tokenCount.innerText = tokens; | |
| } else { | |
| authForm.style.display = "flex"; | |
| userInfo.style.display = "none"; | |
| toolbox.style.display = "none"; | |
| } | |
| }); | |
| // === Sliders live update === | |
| document.getElementById("strength").oninput = function () { | |
| document.getElementById("strengthValue").innerText = this.value; | |
| }; | |
| document.getElementById("guidanceScale").oninput = function () { | |
| document.getElementById("guidanceValue").innerText = this.value; | |
| }; | |
| // === Drag & Drop === | |
| uploadSection.addEventListener("dragover", (e) => { | |
| e.preventDefault(); | |
| uploadSection.style.background = "#e6f4ff"; | |
| }); | |
| uploadSection.addEventListener("dragleave", () => { | |
| uploadSection.style.background = ""; | |
| }); | |
| uploadSection.addEventListener("drop", (e) => { | |
| e.preventDefault(); | |
| uploadSection.style.background = ""; | |
| handleImage(e.dataTransfer.files[0]); | |
| }); | |
| imageInput.addEventListener("change", () => handleImage(imageInput.files[0])); | |
| function handleImage(file) { | |
| if (!file) return; | |
| const reader = new FileReader(); | |
| reader.onload = (e) => | |
| (preview.innerHTML = `<img src="${e.target.result}" alt="Preview">`); | |
| reader.readAsDataURL(file); | |
| } | |
| // === Restyle & Deduct Token === | |
| async function submitForm() { | |
| const user = auth.currentUser; | |
| if (!user) return alert("Please log in first."); | |
| const file = imageInput.files[0]; | |
| const prompt = document.getElementById("prompt").value; | |
| if (!file || !prompt) return alert("Upload an image and enter a prompt."); | |
| // Check tokens | |
| const userRef = doc(db, "users", user.uid); | |
| const snap = await getDoc(userRef); | |
| const tokens = snap.exists() ? snap.data().tokens : 0; | |
| if (tokens <= 0) return alert("❌ No credits left!"); | |
| // Call backend | |
| const formData = new FormData(); | |
| formData.append("image", file); | |
| formData.append("prompt", prompt); | |
| formData.append("negative_prompt", document.getElementById("negativePrompt").value); | |
| formData.append("strength", document.getElementById("strength").value); | |
| formData.append("guidance_scale", document.getElementById("guidanceScale").value); | |
| loader.classList.remove("hidden"); | |
| result.innerHTML = ""; | |
| try { | |
| const res = await fetch(`${window.location.origin}/restyle`, { | |
| method: "POST", | |
| body: formData, | |
| }); | |
| if (!res.ok) throw new Error("Server error"); | |
| // Deduct one token | |
| await updateDoc(userRef, { tokens: tokens - 1 }); | |
| // Update on-screen count | |
| tokenCount.innerText = tokens - 1; | |
| const blob = await res.blob(); | |
| loader.classList.add("hidden"); | |
| result.innerHTML = `<h3>Restyled Result:</h3><img src="${URL.createObjectURL( | |
| blob | |
| )}" alt="Result">`; | |
| } catch (err) { | |
| loader.classList.add("hidden"); | |
| result.innerHTML = `<p style="color:red">❌ ${err.message}</p>`; | |
| } | |
| } | |
| // Expose functions for inline handlers | |
| window.googleLogin = googleLogin; | |
| window.signUp = signUp; | |
| window.logIn = logIn; | |
| window.logOut = logOut; | |
| window.submitForm = submitForm; | |