Class 9-IMAGE GENERATION USING OpenAI APIs & Other Tools

Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)

Class 9-IMAGE GENERATION USING OpenAI APIs & Other Tools Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)

IMAGE GENERATION USING OpenAI APIs & Other Tools

Notes from the AI Basic Course (Class 9) by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)

Start Listening to Andrew Ng. It will take sometime to understand his level of knowledge, you have work realy hard.

First Develop your Base, then go to Andrew Ng.

Dall-E:

DALL-E is an artificial intelligence model developed by OpenAI.

It is designed to generate highly creative and unique images from textual descriptions.

DALL-E has the ability to understand and follow detailed instructions for generating images.

Hugging Face:

Hugging Face is an open-source library and platform for Natural Language Processing (NLP).

It provides a wide range of pre-trained models and tools that make it easier to work with NLP tasks.

Hugging Face is beginner-friendly and widely used by researchers and developers.

Hugging Face Pipeline:

Hugging Face Pipelines are a high-level and user-friendly API provided by the Hugging Face library.

They enable developers and researchers to perform various Natural Language Processing (NLP) tasks with just a few lines of code.

Pipelines contains complex NLP models and processes, making it easy to use and integrate them into applications.

Supported Tasks:

Hugging Face Pipelines support a wide range of NLP tasks, including:

Text Classification

Sentiment Analysis

Named Entity Recognition (NER)

Text Summarization

Image Classification

Text Summarization:

Text Summarization is a technique used in Natural Language Processing (NLP) to generate a concise summary of a longer text while preserving its main points.

Name Entity Recognition (NER):

Named Entity Recognition (NER) is a subtask of Natural Language Processing (NLP) that focuses on identifying and classifying named entities in text.

Named entities are real-world objects such as persons, organizations, locations, dates, and more.

Google Colab Link:

https://colab.research.google.com/drive/1i7GC3UH46U8rLOyKBIYfGo0JAdie9EYn#scrollTo=yfLEmcp7QS-7

Image Generation Using Openai:

!pip install openai

openai.api_key = 'your open ai key'

import io

import openai

import requests

import PIL

from PIL import Image

openai.api_key = 'your openai key'

def generate_image(text):

# Generate the image using OpenAI's DALL-E model

response = openai.Image.create(

prompt=text,

n=1,

size = "512x512"

)

# Get the image URL from the response

image_url = response.data[0]['url']

# Download the image and convert it to a PIL image

image_content = requests.get(image_url).content

image = Image.open(io.BytesIO(image_content))

image.show()

prompt = input("Enter your prompt to generate the image: ")

generate_image(prompt)

# print(type(output))

Hugging Face:

# Installing the required modules

!pip install transformers

# Installing the required modules

!pip install datasets

Sentiment Analysis:

import transformers

from transformers import pipeline

pipe = pipeline("text-classification")

pipe("This movie is very boring")

import transformers

from transformers import pipeline

pipe = pipeline(model="roberta-large-mnli")

pipe("I do not like this movie")

Text Summarization:

# text to be summarized

input_text = "Start by providing your text input. It could be a sentence or a paragraph.\n Tokenization: The input is tokenized, which means breaking it down into smaller units like words or subwords.\n Tokens are the building blocks for NLP models\n.Model: The tokenized input is passed through a pre-trained NLP model. \n Hugging Face offers a wide range of models for different NLP tasks, such as sentiment analysis,\n question answering, and text generation.Prediction/Output: The model processes the tokenized input and generates \n a prediction or output specific to the task. For example, if it's sentiment analysis, \n it could predict whether the input is positive or negative"

print(input_text)

# use bart in pytorch

summarizer = pipeline("summarization")

summarizer("Text Input: Start by providing your text input. It could be a sentence or a paragraph.Tokenization: The input is tokenized, which means breaking it down into smaller units like words or subwords. Tokens are the building blocks for NLP models.Model: The tokenized input is passed through a pre-trained NLP model. Hugging Face offers a wide range of models for different NLP tasks, such as sentiment analysis, question answering, and text generation.Prediction/Output: The model processes the tokenized input and generates a prediction or output specific to the task. For example, if it's sentiment analysis, it could predict whether the input is positive or negative.", min_length=5, max_length=30)

Name Entity Relation:

nlp = pipeline("ner")

example = "My name is Ahmad and i am going to Pakistan"

ner_results = nlp(example)

print(ner_results)

Image Classification:

# Displaying the image

from PIL import Image

# Specify the path to your PNG image

image_path = '/content/demo.png'

# Open the image using PIL

image = Image.open(image_path)

# Display the image

image.show()

from transformers import pipeline

classifier = pipeline(model="microsoft/beit-base-patch16-224-pt22k-ft22k")

classifier("/content/demo.png")

!pip install diffusers

!pip install transformers

import transformers

from transformers import pipeline

from diffusers import StableDiffusionPipeline

import torch

model_id = "runwayml/stable-diffusion-v1-5"

pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)

pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"

image = pipe(prompt).images[0]

image.save("astronaut_rides_horse.png")

Text Summarization:

Get summary from the large data.

#AI #artificialintelligence #datascience #irfanmalik #drsheraz #xevensolutions #hamzanadeem

要查看或添加评论,请登录

Hamza Nadeem的更多文章

社区洞察

其他会员也浏览了