ChatGPT-101 Part 2

Rohit Ranjan
2 min readDec 14, 2023

--

Fun with OpenAI

In continuation to my previous Article: ChatGPT-101 (https://medium.com/p/5a554961febc / https://rrohitrockss.medium.com/chatgpt-101-5a554961febc) this article explains more features of ChatGPT including the most sought after Dall-E (Generative AI) model. Article also explores Text to Speech and Moderator.

Prerequisite:

Refer to my previous article on link below:

Dalle — Image Generation for Fun:

Below code generates AI Images as per instructions on prompt and shows the image on default web browser and you can tweak size variable for desired image quality. Just type your imagination on the prompt and see it live on your default web browser. Fully functional Sample Code follows:

import os
import openai
import webbrowser
openai.organization="org-iV*********************ws"
openai.api_key = os.getenv("OPENAI_API_KEY")
from openai import OpenAI

client = OpenAI()

def image_prompt(PROMPT):
response=client.images.generate(
model= "dall-e-3",
prompt=PROMPT,
quality="hd",
n=1,
#size="1024x1024",
size="1792x1024",
#size=#1024x1792",
)
return(response.data[0].url)

while True:
print ("Type 'exit' to close prompt!")
user_input=input("User: ")
if user_input == 'exit':
print('Thank You.')
break
response=image_prompt(user_input)
webbrowser.open(response)
#print("ChatGPT: ",response)

TTS — Text to speech

Below code can be used to generate audio of the text inserted on the prompt. Audio file is in mp3 format with filename as output.mp3

Format supported: File uploads are currently limited to 25 MB and the following input file types are supported: mp3, mp4, mpeg, mpga, m4a, wav, and webm.

Voice of the narrator/reader can also be customized, and the available formats are as follows:

Types of Voices: alloy, echo, fable, onyx, nova, and shimmer

import os
import openai
from openai import OpenAI
openai.organization="org-iV********************ws"
openai.api_key = os.getenv("OPENAI_API_KEY")
client = OpenAI()

#from openai import OpenAI
def data(recv):
response = client.audio.speech.create(
model="tts-1",
voice="nova",
input=recv,
#input="Hello world! This is a streaming test.",
)
return response.stream_to_file("output.mp3")


while True:
print ("Type 'exit' to close prompt!")
user_input=input("User: ")
if user_input == 'exit':
print('Thank You.')
break
response=data(user_input)
print("ChatGPT: ",response)

Moderator:

Moderations endpoint is a utility which you can use to check whether content complies with OpenAI’s usage policies. Developers can thus identify content that our usage policies prohibits and take action, for instance: by filtering it.

Below code shows moderator output for the supplied content on prompt

import os
import openai
openai.organization="org-iV*********************ws"
openai.api_key = os.getenv("OPENAI_API_KEY")
from openai import OpenAI
client = OpenAI()

def mod(PROMPT):
response = client.moderations.create(
input=PROMPT
)
# return response.results[0]
return (response.results[0].flagged)


while True:
print ("Type 'exit' to close prompt!")
user_input=input("User: ")
if user_input == 'exit':
print('Thank You.')
break
response=mod(user_input)
print("ChatGPT: ",response)

References:

https://github.com/SecurityIsIllusion/GPT

https://medium.com/@rrohitrockss/chatgpt-101-5a554961febc

--

--