| import './style.css' |
|
|
| |
| const portNum = process.env.PORT_NUM || 3001; |
|
|
| |
| const imageUploader = document.getElementById("imageFileInput"); |
| const imageUrlInput = document.getElementById("imageUrlInput"); |
| const promptInput = document.getElementById("promptInput"); |
| const submitButton = document.querySelector("#submitButton"); |
| const qwenOutput = document.querySelector("#qwenOutput"); |
| |
| imageUploader.addEventListener("change", imageFileChanged); |
| imageUrlInput.addEventListener("input", imageUrlChanged); |
| submitButton.addEventListener("click", submitReplicate); |
|
|
| var imageDataURL = ""; |
|
|
| |
| |
| const model_code = process.env.MODEL_CODE || "50881b153b4d5f72b3db697e2bbad23bb1277ab741c5b52d80cd6ee17ea660e9"; |
|
|
| |
| const proxyUrl = `http://localhost:${portNum}/api/predictions`; |
| const proxyUrlImage = `http://localhost:${portNum}/imagehosting/upload`; |
| |
| var requestOptions = { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json' |
| } |
| }; |
|
|
|
|
| function imageFileChanged(event) { |
| const file = event.target.files[0]; |
| if (!file) return; |
| imageUrlInput.value = ""; |
| const reader = new FileReader(); |
| reader.onload = (e) => { |
| console.log(e.target); |
| imageDataURL = e.target.result; |
| }; |
| reader.readAsDataURL(file); |
| } |
|
|
| function imageUrlChanged(event) { |
| const imageUrl = imageUrlInput.value.trim(); |
| if (imageUrl === "") return; |
| imageDataURL = ""; |
| imageUploader.value = ""; |
| } |
|
|
| function submitReplicate() { |
| const promptText = promptInput.value; |
|
|
| let imageUrlPromise; |
|
|
| |
| if (imageDataURL !== "") { |
| |
| const reqImageHostOpts = { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json' |
| }, |
| body: JSON.stringify({ source: imageDataURL.split(',')[1] }) |
| }; |
|
|
| |
| imageUrlPromise = new Promise((resolve, reject) => { |
| fetch(proxyUrlImage, reqImageHostOpts) |
| .then(response => response.json()) |
| .then(data => { |
| console.log(`Image data:`); |
| console.log(data); |
| resolve(data.data.url); |
| }) |
| .catch(error => reject(error)); |
| }); |
| } else { |
| |
| imageUrlPromise = Promise.resolve(imageUrlInput.value); |
| } |
|
|
| |
| imageUrlPromise.then(imageUrl => { |
| const requestData = { |
| version: model_code, |
| input: { |
| image: imageUrl, |
| prompt: promptText |
| } |
| }; |
|
|
| |
| requestOptions.body = JSON.stringify(requestData); |
|
|
| runReplicate(); |
| }).catch(error => { |
| console.error('Error fetching image data:', error); |
| }); |
| } |
|
|
| function runReplicate() { |
| console.log('runReplicate() invoked'); |
| console.log(`Request:`); |
| console.log(requestOptions); |
| fetch(proxyUrl, requestOptions) |
| .then(response => response.json()) |
| .then(data => { |
| console.log(`Initial data:`); |
| console.log(data); |
| const getUrl = data.urls.get; |
| qwenOutput.innerHTML = 'Waiting for output...'; |
| pollPrediction(getUrl); |
| }) |
| .catch(error => console.error('Error:', error)); |
| } |
|
|
| function pollPrediction(predictionUrl) { |
| fetch('/api/predictions/' + encodeURIComponent(predictionUrl)) |
| .then(response => response.json()) |
| .then(data => { |
| qwenOutput.innerHTML = `Status: ${data.status}`; |
| if (data.status === 'succeeded') { |
| console.log(`Received data:`); |
| console.log(data); |
| qwenOutput.innerHTML = `Qwen: ${data.output}`; |
| } else { |
| |
| setTimeout(() => pollPrediction(predictionUrl), 3000); |
| } |
| }) |
| .catch(error => console.error('Error:', error)); |
| } |
|
|