```javascript
class ImageProcessor extends HTMLElement {
constructor() {
super();
this.selectedModel = 'prompthero/openjourney';
this.uploadedImage = null;
}
connectedCallback() {
this.render();
this.attachEventListeners();
}
render() {
this.innerHTML = `
AI Image Processor
Artistic Style Transfer
Apply artistic styles to your images
Super Resolution
Enhance image resolution and details
Edge Enhancement
Transform images using edge detection
Processed Result
Processed image will appear here
`;
}
attachEventListeners() {
// Model selection
const modelOptions = this.querySelectorAll('.model-option');
modelOptions.forEach(option => {
option.addEventListener('click', (e) => {
modelOptions.forEach(opt => opt.classList.remove('active'));
e.currentTarget.classList.add('active');
this.selectedModel = e.currentTarget.dataset.model;
});
});
// File upload
const dropZone = this.querySelector('#drop-zone');
const fileInput = this.querySelector('#file-input');
const browseBtn = this.querySelector('#browse-btn');
const preview = this.querySelector('#preview');
browseBtn.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', (e) => {
if (e.target.files.length) {
this.handleFile(e.target.files[0]);
}
});
// Drag and drop events
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('drag-over');
});
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('drag-over');
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.classList.remove('drag-over');
if (e.dataTransfer.files.length) {
this.handleFile(e.dataTransfer.files[0]);
}
});
}
handleFile(file) {
if (!file.type.match('image.*')) {
alert('Please select an image file');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
this.uploadedImage = e.target.result;
this.updatePreview();
};
reader.readAsDataURL(file);
}
updatePreview() {
const inputPreview = this.querySelector('#input-preview');
inputPreview.innerHTML = `
`;
}
async processImage(prompt) {
if (!this.uploadedImage) {
alert('Please upload an image first');
return;
}
const outputPreview = this.querySelector('#output-preview');
outputPreview.innerHTML = `
`;
try {
// Convert base64 to blob
const imageData = this.uploadedImage.split(',')[1];
const byteString = atob(imageData);
const mimeString = this.uploadedImage.split(',')[0].split(':')[1].split(';')[0];
const ab = new ArrayBuffer(byteString.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
const blob = new Blob([ab], {type: mimeString});
// Prepare form data
const formData = new FormData();
formData.append('inputs', prompt);
formData.append('image', blob, 'input.png');
// Call Hugging Face API
const response = await fetch(
`https://api-inference.huggingface.co/models/${this.selectedModel}`,
{
headers: {
'Authorization': 'Bearer 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'
},
method: 'POST',
body: formData
}
);
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
const blobResult = await response.blob();
const imageUrl = URL.createObjectURL(blobResult);
// Display result
outputPreview.innerHTML = `
`;
} catch (error) {
console.error('Error processing image:', error);
outputPreview.innerHTML = `
Error processing image. Please try again.
`;
}
}
downloadImage(url) {
const link = document.createElement('a');
link.href = url;
link.download = 'processed-image.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
customElements.define('image-processor', ImageProcessor);
>>>>>>> REPLACE