| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
|
| <title>AI Image Caption Generator</title> |
|
|
| <style> |
| body { |
| margin: 0; |
| min-height: 100vh; |
| font-family: Arial, sans-serif; |
| background: linear-gradient(135deg, #667eea, #764ba2); |
| display: flex; |
| justify-content: center; |
| align-items: center; |
| padding: 20px; |
| box-sizing: border-box; |
| } |
| |
| .container { |
| width: 100%; |
| max-width: 650px; |
| background: white; |
| padding: 30px; |
| border-radius: 22px; |
| box-shadow: 0 10px 35px rgba(0,0,0,0.2); |
| text-align: center; |
| } |
| |
| h1 { |
| margin-top: 0; |
| color: #333; |
| } |
| |
| .subtitle { |
| color: #666; |
| margin-bottom: 25px; |
| } |
| |
| input[type="file"] { |
| width: 100%; |
| padding: 15px; |
| border: 2px dashed #aaa; |
| border-radius: 12px; |
| box-sizing: border-box; |
| cursor: pointer; |
| } |
| |
| #preview { |
| display: none; |
| max-width: 100%; |
| max-height: 350px; |
| margin: 20px auto; |
| border-radius: 15px; |
| } |
| |
| button { |
| width: 100%; |
| padding: 14px; |
| margin-top: 15px; |
| border: none; |
| border-radius: 12px; |
| background: #4f46e5; |
| color: white; |
| font-size: 16px; |
| cursor: pointer; |
| } |
| |
| button:disabled { |
| background: #999; |
| cursor: not-allowed; |
| } |
| |
| #status { |
| margin-top: 18px; |
| font-weight: bold; |
| color: #555; |
| } |
| |
| #caption { |
| margin-top: 20px; |
| padding: 18px; |
| background: #f3f4f6; |
| border-radius: 14px; |
| font-size: 18px; |
| line-height: 1.5; |
| min-height: 30px; |
| } |
| </style> |
| </head> |
|
|
| <body> |
|
|
| <div class="container"> |
|
|
| <h1>🖼️ AI Image Caption Generator</h1> |
|
|
| <p class="subtitle"> |
| Upload an image and let AI describe it ✨ |
| </p> |
|
|
| <input |
| type="file" |
| id="imageInput" |
| accept="image/*" |
| > |
|
|
| <img id="preview" alt="Image preview"> |
|
|
| <button id="captionBtn" disabled> |
| ✨ Generate Caption |
| </button> |
|
|
| <div id="status"></div> |
|
|
| <div id="caption"> |
| Your caption will appear here... |
| </div> |
|
|
| </div> |
|
|
| <script type="module"> |
| |
| import { pipeline } |
| from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.2"; |
| |
| const imageInput = document.getElementById("imageInput"); |
| const preview = document.getElementById("preview"); |
| const captionBtn = document.getElementById("captionBtn"); |
| const status = document.getElementById("status"); |
| const caption = document.getElementById("caption"); |
| |
| let selectedImage = null; |
| let captioner = null; |
| |
| imageInput.addEventListener("change", (event) => { |
| |
| const file = event.target.files[0]; |
| |
| if (!file) { |
| return; |
| } |
| |
| selectedImage = file; |
| |
| preview.src = URL.createObjectURL(file); |
| preview.style.display = "block"; |
| |
| captionBtn.disabled = false; |
| |
| caption.innerText = |
| "Image selected! Click Generate Caption ✨"; |
| |
| }); |
| |
| captionBtn.addEventListener("click", async () => { |
| |
| if (!selectedImage) { |
| return; |
| } |
| |
| try { |
| |
| captionBtn.disabled = true; |
| |
| status.innerText = |
| "⏳ AI model load ho raha hai... pehli baar thoda time lag sakta hai."; |
| |
| caption.innerText = ""; |
| |
| if (!captioner) { |
| |
| captioner = await pipeline( |
| "image-to-text", |
| "Xenova/vit-gpt2-image-captioning" |
| ); |
| |
| } |
| |
| status.innerText = |
| "🤖 Image analyze ho rahi hai..."; |
| |
| const imageURL = |
| URL.createObjectURL(selectedImage); |
| |
| const result = |
| await captioner(imageURL); |
| |
| caption.innerText = |
| "📝 " + result[0].generated_text; |
| |
| status.innerText = |
| "✅ Caption generated!"; |
| |
| URL.revokeObjectURL(imageURL); |
| |
| } catch (error) { |
| |
| console.error(error); |
| |
| status.innerText = |
| "❌ Kuch problem aa gayi."; |
| |
| caption.innerText = |
| "Model load nahi ho paya. Page refresh karke dobara try karo."; |
| |
| } |
| |
| captionBtn.disabled = false; |
| |
| }); |
| |
| </script> |
|
|
| </body> |
| </html> |