AI Image Generator with Hugging Face API :- This project is an AI-powered image generation web application that allows users to input a text prompt and generate high-quality images using various Hugging Face diffusion models. The application includes a clean UI with light/dark themes, model selection, aspect ratio control, and advanced features such as custom error alerts, loading spinners, and result downloading.
β¨ Built with: Vanilla JavaScript, HTML, CSS, Hugging Face Inference API
π Features π Text-to-Image Generation using Hugging Face inference models
π¨ Multiple Model Support β choose from a list of diffusion models
π’ Image Count Selector β generate multiple images at once
π Aspect Ratio Options β customize the width-to-height ratio
π² Prompt Suggestions β click-to-generate random prompts
π Dark/Light Theme Toggle β theme preference saved across sessions
β³ Loading Spinners while images are being fetched
π§Ύ Styled Error Alerts for unavailable models (custom alerts)
π₯ One-click Image Download
π± Fully Responsive Design for desktop and mobile users
π§ How It Works User enters a prompt (e.g., "A fantasy dragon flying over snowy mountains").
Chooses:
AI Model (e.g., stabilityai/stable-diffusion)
Number of images to generate
Aspect ratio (1:1, 4:3, 16:9, etc.)
On form submission:
A loading card is shown for each image
A POST request is sent to Hugging Face's inference API with the input prompt and model
The image blob is returned and displayed in the UI
The image is downloadable directly from the preview
If a model is unavailable:
A custom alert box is shown suggesting the user to try a different model
π Folder Structure pgsql Copy Edit /project-root βββ index.html βββ style.css βββ script.js βββ alert.js (Custom styled alert box) βββ README.md
π API Key Protection (Important) For development, your Hugging Face API key is required to be stored in script.js.
--> PLEASE REPLACE YOUR HUGGING FACE API KEY WITH THE PROMPT your_huggingface_API AT API_KEY. (As I can not share my API key publicly)
Free accounts: 30 requests/month
To continue using the app after hitting limits:
Upgrade to a paid Hugging Face plan
Generate a new key from another Hugging Face account
Or host your own model using transformers locally
πΈ Preview Hereβs how the app looks: https://saurabh19304.github.io/AI_image_generator/
Replace your Hugging Face API key in script.js:
js Copy Edit const API_KEY = 'your_huggingface_key_here';
π― Future Improvements Add drag-and-drop image preview area
Enable saving prompt history
Connect to a backend for safer API handling
Add login system to track usage
πββοΈ About Me I created this project as a part of my learning journey, writing most of the code manually. It was a valuable experience combining real-time coding, UI design, and integrating external APIs. Some parts of the code involved trial and error and learning on the go β I plan to revisit and document those areas for better understanding.
π¬ Feedback If you have suggestions, improvements, or want to collaborate, feel free to open an issue.