0% found this document useful (0 votes)
16 views2 pages

Python AI Code Generation Examples

This document provides basic Python code examples for generating text and images using the OpenAI API. It includes a text generation example with GPT and an image generation example with DALL-E, along with instructions for installation and usage. Additionally, it suggests using Flask or FastAPI to integrate the AI functionality into a web application with HTML and JavaScript.

Uploaded by

akshanshp436
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Python AI Code Generation Examples

This document provides basic Python code examples for generating text and images using the OpenAI API. It includes a text generation example with GPT and an image generation example with DALL-E, along with instructions for installation and usage. Additionally, it suggests using Flask or FastAPI to integrate the AI functionality into a web application with HTML and JavaScript.

Uploaded by

akshanshp436
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic AI Generation Code Examples

This PDF gives you simple Python examples to generate text and images using AI (OpenAI API).

1) TEXT GENERATION

Example using GPT (ChatGPT) to generate text.

import openai

openai.api_key = "YOUR_OPENAI_API_KEY"

response = [Link](
model="text-davinci-003",
prompt="Write a poem about the sun",
max_tokens=50
)

print([Link][0].[Link]())

2) IMAGE GENERATION

Example using DALL-E to generate an image.

import openai

openai.api_key = "YOUR_OPENAI_API_KEY"

response = [Link](
prompt="A futuristic car design",
n=1,
size="512x512"
)

image_url = response['data'][0]['url']
print("Image URL:", image_url)

HOW TO USE:
- Install OpenAI library: pip install openai

- Replace YOUR_OPENAI_API_KEY with your real API key.

- Run the script with Python.

CONNECT TO HTML:

Use Flask or FastAPI to make your Python AI run on a website, then connect with HTML +

JavaScript.

You might also like