File size: 4,314 Bytes
2154b7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# ArtifyAI: Text-to-Image Generation

ArtifyAI is an innovative project that combines the power of Natural Language Processing (NLP) with image generation. This repository implements a pipeline using the T5 Transformer model for text summarization or generation and the Stable Diffusion model for creating images based on the generated text.

## Overview

ArtifyAI takes a text input, processes it through a T5 model, and then uses the processed output to generate an image using Stable Diffusion. This allows for seamless conversion of text descriptions into AI-generated images.

## Features

- **Text Processing**: Uses T5 (Text-to-Text Transfer Transformer) for summarizing or generating text from user inputs.
- **Image Generation**: Uses Stable Diffusion to create high-quality images from the text processed by the T5 model.
- **Combined Pipeline**: A simple Python function combines these models to produce stunning images from text.

## Installation

### Prerequisites

To run this project locally, ensure you have the following:

1. Python 3.7+
2. CUDA-compatible GPU (for faster performance with Stable Diffusion)
3. [Hugging Face Transformers](https://huggingface.co/transformers/) library
4. [Diffusers](https://huggingface.co/docs/diffusers/index) for Stable Diffusion
5. [PyTorch](https://pytorch.org/) with CUDA support (optional for faster image generation)

### Step-by-Step Setup

1. **Clone the Repository**:
    ```bash
    git clone https://github.com/your-username/ArtifyAI.git
    cd ArtifyAI
    ```

2. **Install Dependencies**:
    It's best to use a virtual environment to manage dependencies.
    ```bash
    pip install torch transformers diffusers
    ```

3. **Download the Pretrained Models**:
    You'll need to load the models locally from Hugging Face. You can either download them using the code inside the notebook or by modifying it as follows:
    ```python
    from transformers import T5Tokenizer, T5ForConditionalGeneration
    from diffusers import StableDiffusionPipeline
    import torch

    # Load models
    t5_tokenizer = T5Tokenizer.from_pretrained("t5-small")
    t5_model = T5ForConditionalGeneration.from_pretrained("t5-small")
    ArtifyAI_model = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)

    # Set model to GPU (if available)
    ArtifyAI_model = ArtifyAI_model.to("cuda" if torch.cuda.is_available() else "cpu")
    ```

4. **Run the Pipeline**:
    A sample pipeline is included in `pipeline.py`. You can run it using:
    ```bash
    python pipeline.py
    ```

5. **Text to Image Generation**:
    You can generate images from text input using the following function:
    ```python
    def t5_to_image_pipeline(input_text):
        # T5 model processing
        t5_inputs = t5_tokenizer.encode(input_text, return_tensors='pt', truncation=True)
        summary_ids = t5_model.generate(t5_inputs, max_length=50, num_beams=5, early_stopping=True)
        generated_text = t5_tokenizer.decode(summary_ids[0], skip_special_tokens=True)

        # Generate image from text using Stable Diffusion
        image = ArtifyAI_model(generated_text).images[0]
        return image
    ```

## Usage

1. **Run the Jupyter Notebook**: You can open `ArtifyAI_v1_1.ipynb` in Jupyter to run the code interactively.
2. **Save and Load Models**: You can modify the notebook to save your models to Google Drive or a local directory.
3. **Custom Inputs**: Modify the text input in the pipeline to generate customized images based on different descriptions.

## Example

Here's an example of generating an image from text:
```python
image = t5_to_image_pipeline("A futuristic city skyline at sunset")
image.show()

## For Non-Technical Users

Even if you are new to AI, you can use ArtifyAI by following these simple steps:

1. **Install Python**: Download and install Python 3.7+ from the [official Python website](https://www.python.org/downloads/).
   
2. **Install Dependencies**: Follow the steps in the Installation section to install necessary packages using the `pip` command.
   
3. **Run the Code**: You can run the project directly by using the provided code snippets. If you face any issues, you can refer to [Hugging Face](https://huggingface.co/) or [PyTorch](https://pytorch.org/) for troubleshooting.