Welcome to the AI Image Generator project! This repository showcases how to generate high-quality images using the powerful capabilities of Stable Diffusion, a state-of-the-art AI model for text-to-image synthesis. With just a few lines of code, you can bring your creative ideas to life, generating images from textual prompts.
Stable Diffusion is a deep learning model designed to generate images from text descriptions. Unlike many AI models, Stable Diffusion can run on local machines, making it highly accessible. It works by gradually transforming random noise into coherent images that match the given text prompt, leveraging a process called diffusion. This model is particularly known for its flexibility, high-quality output, and the ability to be fine-tuned for various applications.
To run this project, you'll need the following dependencies:
- Python 3.7+
- Hugging Face Hub
- diffusers
- transformers
- torch
- gradio
To generate an API token from huggingface go to the link, login to your account or sign up if you dont have one. Then, go to profile settings and click on 'Access Tokens'. Click on create new token, an API token will be generated. Save it and use that in your code.
You can install the required libraries using pip:
!pip install diffusers transformers torch huggingface_hub gradio
-
Clone the repository:
git clone https://github.com/yourusername/AI-image-generator.git cd AI-image-generator
-
Install the required dependencies:
pip install -r requirements.txt
-
Login to Hugging Face: Run the following code in a Jupyter notebook or script:
from huggingface_hub import notebook_login notebook_login() # Enter your Hugging Face API key when prompted
-
Set up the environment:
import os os.environ['HUGGINGFACE_TOKEN'] = 'your_huggingface_token'
-
Generate an image:
from diffusers import StableDiffusionPipeline pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=True) pipe = pipe.to("cuda") # Use GPU if available prompt = "red full moon shown from earth" image = pipe(prompt).images[0] image.show()
-
Use Gradio to create a web interface:
import gradio as gr def generate_image(prompt): image = pipe(prompt).images[0] return image gr.Interface(fn=generate_image, inputs="text", outputs="image").launch()
- StableDiffusionPipeline: The core component that loads the pre-trained Stable Diffusion model from Hugging Face.
- pipe(prompt).images[0]: Generates an image based on the given prompt.
- Gradio Interface: A simple web interface to interact with the image generator using text inputs.
The project allows you to generate stunning images based on text prompts. For instance, the prompt "red full moon shown from earth"
produces this image.
Gradio interface will look something like this
This AI Image Generator demonstrates the power and versatility of Stable Diffusion in creating art from simple text prompts. Whether you're experimenting with creative ideas or building sophisticated applications, this project serves as a starting point for exploring the possibilities of AI-generated imagery.
Feel free to fork this repository, contribute, and share your unique creations!
For more information on AI image generation and the technology behind it, see my article: https://medium.com/@farzeeenimran/ai-image-generation-magic-myths-and-masterpieces-where-do-we-draw-the-line-d0955fd2c24a