Building a Text and Image Generation API with OpenAI and Flask in Python

Miluska Romero
3 min readMar 17, 2024

--

Image Generation

Introduction

In this tutorial we’ll walk through the steps of creating a Python API using Flask and OpenAI’s powerful GPT and DALL-E models.

Prerequisites

  • Basic understanding of Python programming and object-oriented concepts.
  • Familiarity with using Postman for testing APIs.
  • An OpenAI API key (obtainable by signing up on OpenAI’s platform).

Step 1: Setting Up Your Environment

Install Python and Virtualenv

  • Install Python: Download and install the latest version of Python from the official Python website. During installation, ensure to check the box that says “Add Python to PATH.”
  • Install Virtualenv: Open Windows Command Prompt and install virtualenv using pip:
pip install virtualenv

Create and Activate a Virtual Environment

Create a Virtual Environment: Navigate to your project directory and create a virtual environment named myenv (you can choose any name you prefer):

cd path\to\your\project
virtualenv myenv
.\myenv\Scripts\activate

Step 2: Install Dependencies

With your virtual environment activated, install the necessary Python packages:

pip install openai python-dotenv flask

Step 3: Project Structure

Your project directory should include the following files and folders:

  • config.py: Stores configuration variables and reads from .env.
  • .env: Contains environment-specific variables.
  • model_service.py: Handles the business logic.
  • __init__.py: Initializes the Flask app and routes.

Create and Configure Files

  1. config.py: Create a file named config.py in your project directory. This file will load your OpenAI API key from the .env file and define other important settings.
from dotenv import load_dotenv
import os

load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
GPT_MODEL = "gpt-3.5-turbo-1106"
GPT_IMAGE_MODEL = "dall-e-3"
HOST = "localhost"
PORT = 8080
DEBUG = True

2. .env: Create a .env file in the same directory as your config.py. Inside, specify your OpenAI API key:

OPENAI_API_KEY=XXX

3. model_service.py: In model_service.py, define a class that uses the OpenAI package to communicate with the OpenAI API:

from config import OPENAI_API_KEY, GPT_MODEL, GPT_IMAGE_MODEL
from openai import OpenAI

class ModelService:
def __init__(self):
self.client = OpenAI(api_key=OPENAI_API_KEY)

def predict(self, prompt):
response = self.client.chat.completions.create(
model=GPT_MODEL,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
)
return response.choices[0].message.content

def image(self, prompt):
size = "1024x1024"
response = self.client.images.generate(
model=GPT_IMAGE_MODEL,
prompt=prompt,
size=size,
n=1,
)
return {
"size": size,
"url": response.data[0].url
}

4. __init__.py: This file will set up your Flask app and define routes for generating text and images:

from flask import Flask, request
from config import HOST, PORT, DEBUG
from model_service import ModelService

app = Flask(__name__)
model_service = ModelService()

@app.route("/api/v1/predict", methods=["POST"])
def predict():
json = request.json
prompt = json.get("prompt")
result = model_service.predict(prompt)
return {
"success": True,
"result": result
}

@app.route("/api/v1/image", methods=["POST"])
def image():
json = request.json
prompt = json.get("prompt")
result = model_service.image(prompt)
return {
"success": True,
"result": result
}

if __name__ == "__main__":
app.run(port=PORT, host=HOST, debug=DEBUG)

Step 4: Run Your API

With your Flask application set up and your model service ready to interact with OpenAI, the final step is to run your API locally.

execute:

python __init__.py

This command will start your Flask application, and it will be accessible at localhost:8080 by default. You can then use Postman to send requests to the endpoints you’ve defined:

Text generation endpoint: localhost:8080/api/v1/predict
Image generation endpoint: localhost:8080/api/v1/image

When constructing your requests in Postman, ensure to set the body to raw with a JSON format and include a prompt field in your request body, like so:

{
"prompt": "Write a brief introduction to APIs."
}

Conclusion

This concludes our step-by-step guide to creating a text and image generation API using Flask and OpenAI on Windows. You now have a functional API that taps into the power of AI to generate content based on prompts you provide. Explore different prompts and see how your API performs. Happy coding!

--

--

Miluska Romero
Miluska Romero

Written by Miluska Romero

I'm a Full-Stack Developer from Peru interested in AI

No responses yet