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.