How to Create Your Own AI Image Generator

How to Create Your Own AI Image Generator
Photo by Erick Butler / Unsplash

Introduction:

In this tutorial, we will be using Vue.js to create an AI image generator. We will be using an API that will return random images based on the keywords that we provide.

The first step is to create a new Vue.js project. We will be using the Vue CLI to generate our project.

vue create ai-image-generator

Once the project is created, we will need to install the following dependencies:

vue-router
vuex
axios

To install these dependencies, we can use the following command:

npm install vue-router vuex axios --save

Once these dependencies are installed, we can start setting up our application.

The first thing we need to do is create a file called routes.js in the src directory. This file will contain our application routes.

We will be creating two routes:

/ – This route will render the home page of our application.
/generate – This route will render the image generation page of our application.

Our routes.js file will look like this:

import Vue from 'vue' 
import Router from 'vue-router' 
import Home from './views/Home.vue' 
import Generate from './views/Generate.vue' 
Vue.use(Router) 

export default 
new Router({ 
mode: 'history', 
base: process.env.BASE_URL, 

routes: [ { 
path: '/', 
name: 'home', 
component: Home }, { 

path: '/generate', 
name: 'generate', 
component: Generate } ] 
})

As you can see, we have imported the Home and Generate components. These components will be rendered when the respective routes are accessed.

Next, we need to create the Home and Generate components.

We will start by creating the “Home” component. Create a new file called Home.vue in the src/views directory.

The Home component will contain a simple message. The code for the Home.vue component is as follows:

<template> 
<div> 
<h1>Home</h1> 
<p>Welcome to the AI image generator!</p> 
</div> 
</template> 

<script> 
export default { 
name: 'home', 

data() { 
return { } } } 
</script> 

<style>
</style>

As you can see, the “Home” component is very simple. It just contains a heading and a paragraph.

Next, we will create the Generate component. Create a new file called Generate.vue in the src/views directory.

This component will contain the UI for our image generation page. The code for the Generate.vue  component is as follows:

<template>  
<div> 
<h1>Generate</h1> 
<div> <label>Keywords:</label> 
<input type="text" v-model="keywords" /> 
<button @click="generate">Generate</button> 
</div> 

<div v-if="image"> 
<img :src="image" /></div> 

</div> 

</template> 

<script> 
export default { 

name: 'generate', 

data() { 
return { 
keywords: '', 
image: '' } }, 

methods: { 
generate() { } } } 
</script> 

<style> 
input { padding: 10px; } 
button { padding: 10px; } 
img { width: 300px; } 
</style>

As you can see, the Generate component contains an input field and a button. The input field is used to enter the keywords that we want to use for image generation. The button is used to trigger the image generation process.

The Generate component also contains a div that will be used to display the generated image. This div is only displayed if an image has been generated.

Next, we need to write the code for the generate method. This method will be used to make an API call to the image generation API. The code for the “generate” method is as follows:

generate() { 
axios.get('[<https://api.unsplash.com/photos/random>](<https://api.unsplash.com/photos/random>)', { 

params: { query: this.keywords, client_id: 'YOUR_CLIENT_ID' } }).then((response) => { this.image = response.data.urls.regular }) 
}

As you can see, we are using the “axios” library to make an HTTP GET request to the image generation API. We are passing the keywords that we want to use for image generation as a query parameter.

We are also passing our client_id as a query parameter. This client_id is used to authenticate our API request. You can get your own client_id by signing up for a free account at unsplash.com.

Once we make the API call, we are setting the image data property to the URL of the generated image. This will cause the generated image to be displayed on the page.

Next, we need to write the code for the Home and Generate components.

The code for the Home.vue component is as follows:

<template> 
<div> 
<h1>Home</h1> 
<p>Welcome to the AI image generator!</p> 
</div> 
</template> 

<script> 
export default { 
name: 'home', 

data() { 
return { } } } 
</script> 

<style>
</style>

As you can see, the Home component is very simple. It just contains a heading and a paragraph.

The code for the Generate.vue component is as follows:

<template> 
<div> 
<h1>Generate</h1> 

<div> 
<label>Keywords:</label> 
<input type="text" v-model="keywords" /> 
<button @click="generate">Generate</button> 
</div> 

<div v-if="image"> 
<img :src="image" /> 
</div> 

</div> 
</template> 

<script> 
export default { 
name: 'generate', 

data() { 
return { 
keywords: '', 
image: '' } }, 

methods: { 
generate() { 
axios.get('[<https://api.unsplash.com/photos/random>](<https://api.unsplash.com/photos/random>)', { 

params: { 
query: this.keywords, 
client_id: 'YOUR_CLIENT_ID' } }).then((response) => { 
this.image = response.data.urls.regular }) } } } 
</script> 

<style> 
input { padding: 10px; } 
button { padding: 10px; } 
img { width: 300px; } 
</style>

As you can see, the Generate component contains an input field and a button. The input field is used to enter the keywords that we want to use for image generation. The button is used to trigger the image generation process.

The Generate component also contains a div that will be used to display the generated image. This div is only displayed if an image has been generated.

Next, we need to write the code for the App.vue component. This component is the root component of our application. The code for the App.vue component is as follows:

<template> 
<div id="app"> 
<router-view/> 
</div> 
</template> 

<script> 
export default { 
name: 'app', 

data() { 
return { } } } 
</script> 

<style> 
#app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } 
</style>

As you can see, the App component just contains a router-view. This router-view will be used to render the component that corresponds to the current route.

Next, we need to write the code for the main.js file. This file is used to initialize our application. The code for the main.js file is as follows:

import Vue from 'vue' 
import App from './App.vue' 
import router from './router' 
import store from './store' 
Vue.config.productionTip = false 

new Vue({ 
router, 
store, 
render: h => h(App) }).$mount('#app')

As you can see, we are importing the App component and the router. We are also importing the “store”. The “store” is used to manage the state of our application.

Next, we need to write the code for the store.js file. This file contains the code for our application store. The code for the store.js file is as follows:

import Vue from 'vue' 
import Vuex from 'vuex' 
Vue.use(Vuex) 

export default 
new Vuex.Store({ 
state: { }, 
mutations: { }, 
actions: { }, 
modules: { } })

As you can see, the “store” is very simple. It just contains the initial state of our application.

Next, we need to write the code for the “index.html” file. This file is the entry point for our application. The code for the “index.html” file is as follows:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<meta name="viewport" content="width=device-width,initial-scale=1.0"> 
<link rel="icon" href="<%= BASE_URL %>favicon.ico"> 
<title>ai-image-generator</title> 
</head> 

<body> 
<noscript>
 <strong>We're sorry but ai-image-generator doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> 
</noscript> 
<div id="app">
</div> <!-- built files will be auto injected --> 
</body>

</html>

As you can see, the index.html file is very simple. It just contains the app div. This div is used to render our application.

Finally, we need to write the code for the webpack.config.js file. This file is used to configure webpack. The code for the webpack.config.js file is as follows:

const path = require('path') 
function resolve (dir) { 
return path.join(__dirname, dir) 
} 

module.exports = { chainWebpack: config => { 
config.resolve.alias .set('@', resolve('src')) .
set('assets', resolve('src/assets')) }, 
css: { loaderOptions: { sass: { data: @import "./src/assets/styles/variables.scss"; } } } }

As you can see, we are setting up some aliases. These aliases are used to make it easier to import files from our application.

Now that we have written all the code for our application, we can run our application.

To run our application, we can use the following command:

npm run serve

This will start a development server on http://localhost:8080.

You can access the application at http://localhost:8080.

Conclusion:

In this article, we have created a simple AI image generator using Vue.js.