0% found this document useful (0 votes)
28 views5 pages

Wa0028.

Uploaded by

pankajsilot999
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)
28 views5 pages

Wa0028.

Uploaded by

pankajsilot999
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
You are on page 1/ 5

12/4/24, 11:18 AM Sha_LS_13a_and_13b.

ipynb - Colab

Generative AI CSE3348
Name: Pankaj Silot
20211cse0730
7CSE15

https://colab.research.google.com/drive/1ZvDUeFYe5dyDR219frV3OwvKw2YcswRC#scrollTo=fsqsjMjHNReB&printMode=true 1/5
12/4/24, 11:18 AM Sha_LS_13a_and_13b.ipynb - Colab

import os

# Set the OpenAI API key


os.environ['OPENAI_API_KEY'] = 'sk-VTZGcU2zYRjmcUOEUNqG4J9HUvwMlMJg_WGdU

api_key = os.getenv('OPENAI_API_KEY')

print(api_key) # Check if the key is successfully retrieved

sk-VTZGcU2zYRjmcUOEUNqG4J9HUvwMlMJg_WGdUa01WST3BlbkFJcC_CHLnJDt26s

!pip install openai

Requirement already satisfied: openai in /usr/local/lib/python3.10


Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/p
Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/
Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/
Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/p
Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/li
Requirement already satisfied: sniffio in /usr/local/lib/python3.1
Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10
Requirement already satisfied: typing-extensions<5,>=4.11 in /usr/
Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3
Requirement already satisfied: exceptiongroup in /usr/local/lib/py
Requirement already satisfied: certifi in /usr/local/lib/python3.1
Requirement already satisfied: httpcore==1.* in /usr/local/lib/pyt
Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/p
Requirement already satisfied: annotated-types>=0.6.0 in /usr/loca
Requirement already satisfied: pydantic-core==2.23.4 in /usr/local

import openai # Import an API client library

client = openai # Here, client is defined and assigned


client.api_key = "sk-VTZGcU2zYRjmcUOEUNqG4J9HUvwMlMJg_WGdUa01WST3Bl
bkFJc

import openai
import requests
from PIL import Image
import io

# Set up your OpenAI API key


openai.api_key = 'sk-VTZGcU2zYRjmcUOEUNqG4J9HUvwMlMJg_WGdUa01WST3Bl
bkF

def generate_image(prompt, n=1, size="1024x1024"):


"""
Generate an image using DALL-E 3

:param prompt: Text description of the image


:param n: Number of images to generate
:param size: Size of the image
:return: List of image URLs
"""
try:
response = client.images.generate
(
model="dall-e-3",
prompt=prompt,
n=n,
size=size
)

https://colab.research.google.com/drive/1ZvDUeFYe5dyDR219frV3OwvKw2YcswRC#scrollTo=fsqsjMjHNReB&printMode=true 2/5
12/4/24, 11:18 AM Sha_LS_13a_and_13b.ipynb - Colab

https://colab.research.google.com/drive/1ZvDUeFYe5dyDR219frV3OwvKw2YcswRC#scrollTo=fsqsjMjHNReB&printMode=true 3/5
12/4/24, 11:18 AM Sha_LS_13a_and_13b.ipynb - Colab
# Importing PyTorch library, for building and trai
ning neural networks
import torch

# Importing StableDiffusionPipeline to use pre-tra


ined Stable Diffusio
from diffusers import StableDiffusionPipeline

# Image is a class for the PIL module to visualizeimages in a Python


from PIL import Image

The cache for model files in Transformers v4.22.0 has been updated
0/0 [00:00<?, ?it/s]

# Creating pipeline
pipeline = StableDiffusionPipeline.from_pretrained
("CompVis/stable-dif
torch_dtype=torch.fl

/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_aut
The secret `HF_TOKEN` does not exist in your Colab secrets.
To authenticate with the Hugging Face Hub, create a token in your
You will be able to reuse this secret in all of your notebooks.
Please note that authentication is recommended but still optional
warnings.warn(
model_index.json: 100% 541/541 [00:00<00:00, 40.2kB/s]

Fetching 16 files: 100% 16/16 [00:37<00:00, 2.35s/it]

model.safetensors: 100% 492M/492M [00:11<00:00, 73.1MB/s]

text_encoder/config.json: 100% 592/592 [00:00<00:00, 6.83kB/s]

(…)ature_extractor/preprocessor_config.json: 100% 342/342 [00:00<00:00, 4.31kB/s

scheduler/scheduler_config.json: 100% 313/313 [00:00<00:00, 3.58kB/s]

safety_checker/config.json: 100% 4.56k/4.56k [00:00<00:00, 56.5kB/s]

tokenizer/merges.txt: 100% 525k/525k [00:00<00:00, 4.14MB/s]

model.safetensors: 100% 1.22G/1.22G [00:24<00:00, 16.2MB/s]

(…)kpoints/scheduler_config - 209/209 [00:00<00:00, 2.35kB/s]

checkpoint.json: 100%

diffusion_pytorch_model.safetensors: 100% 3.44G/3.44G [00:36<00:00, 223MB/s]

tokenizer/vocab.json: 100% 1.06M/1.06M [00:00<00:00, 5.21MB/s]

tokenizer/tokenizer_config.json: 100% 806/806 [00:00<00:00, 8.85kB/s]

tokenizer/special_tokens_map.json: 100% 472/472 [00:00<00:00, 4.75kB/s]

unet/config.json: 100% 743/743 [00:00<00:00, 7.79kB/s]

vae/config.json: 100% 551/551 [00:00<00:00, 13.8kB/s]

diffusion_pytorch_model.safetensors: 100% 335M/335M [00:06<00:00, 56.8MB/s]

# Defining function for the creation of a grid ofimages


def image_grid(imgs, rows, cols):
assert len(imgs) == rows*cols

w, h = imgs[0].size
grid = Image.new('RGB', size = (cols*w,
rows * ))
w
grid_w, grid_h = grid.size

for i, img in enumerate(imgs):


grid.paste(img, box = (i%cols*w, i // cols*h))
return grid

https://colab.research.google.com/drive/1ZvDUeFYe5dyDR219frV3OwvKw2YcswRC#scrollTo=fsqsjMjHNReB&printMode=true 4/5
12/4/24, 11:18 AM Sha_LS_13a_and_13b.ipynb - Colab

# Moving pipeline to GPU


# Check if CUDA is available before moving the pip
eline
if torch.cuda.is_available():
pipeline = pipeline.to('cuda')
else:
print("CUDA is not available. Using CPU instead."
)

n_images = 6 # Let's generate 6 images based on the prompt belo


w
prompt = ['Sunset on a beach'] * n_images

images = pipeline(prompt).images

grid = image_grid(images, rows=2, cols = 3)


grid

100% 50/50 [00:37<00:00, 1.37it/s]

Start coding or generate with AI.

https://colab.research.google.com/drive/1ZvDUeFYe5dyDR219frV3OwvKw2YcswRC#scrollTo=fsqsjMjHNReB&printMode=true 5/5

You might also like