Hands on New Words Generation App with Machine Learning, using Python

Here’s how I generated an app that creates new non-existing words using OpenAI and Python, in a few lines of code

Piero Paialunga
6 min readMar 16, 2023
Photo by Brett Jordan on Unsplash

I was doing a rewatch of a Netflix Technology Blog TV show called “You” with my girlfriend a couple of weeks ago.

This is a TV show about (toxic) love and there is an episode where the two lovers are playing Scrabble. The particular thing is that they could only use made-up words. For example, one word was “everythingship”, which was a made up word. According to their rules, they also had to explain the meaning of this word. I remember that I told my girlfriend:

“It would be cool to write an Artificial Intelligence that makes up a new word given two input existing ones”

Then we fell asleep.

A couple of days ago, OpenAI announced the release of the ChatGPT and Whisper APIs. This is huge on so many levels. There are multiple consequences of releasing this API to other companies:

  • Snapchat is using MyAI in Snapchat+. This uses the ChatGPT API to build a virtual AI friend on your Snapchat list that can be used to get recommendations and can write custom text for another friend you have on Snapchat as well.
  • Instacart improves your grocery shopping again using ChatGPT based on your input text. More precisely, based on what you want to cook (e.g. “Dinner with friends”) it will be the friend that you usually call to know which ingredients you have to buy 🙃
  • Kind of the same thing is happening with Shop, which is kind of your shopping assistant for clothes and shopping in general

This gives us just a slight idea of the power that companies can use to build incredibly efficient code without months and months of modeling and computational cost.

But I believe that the power of this model is also in the hands of every single person. Imagine how many things you needed to know before the era of pre-trained NLP models. Imagine how much power you needed to have. How much knowledge. How much money and time. Now you not only have a pre-trained model, but you have a pre-trained chatbot. At your convenience. In your laptop. For Free.

I agree that it can be implemented in a company and you can build an end-to-end business using it, but it is also true that everyone can now implement a solution for their Machine Learning problem in a few lines of code. And that problem doesn’t have to be naive or a simple one, but it can actually also be something fairly complex…like making up words, for example 😏

Let me show you what I did, in 1 hour, after a nightmare that kept me awake this morning before my shift:

Image by author

Pretty cool right?

And as I told you, the power of OpenAI is that this becomes super easy to do. It is so easy, that it is super immediate to write an app using a module named streamlit, in less than 50 lines of code.

Let me explain you how!

1. The setup

Let’s say you already have Python. What do we need to run this app?

  1. The OpenAI module
  2. Streamlit
  3. That’s it.

Impressive right?
You can download streamlit running this code:

pip install streamlit

Veeeery easy.

You can install openai with the same syntax:

pip install openai

The only thing you should care about is that you have an OpenAI API. To do that, please follow the instructions here (OpenAI API).

2. The code

The code is very easy and uploaded in a public Github folder (https://github.com/PieroPaialungaAI/new_word_generator). You would need to change the OPENAI_KEY value with your API. The code consists in two blocks.

2.1 The actual function

The first block is the proper word_maker function.

Note!!! You would also need a constants.py file. Download it from the Github folder and change the OPENAI_KEY with yours

As we can see, now we are instructing the GPT model with 4 blocks.

The first block says:

You are a word maker. You are a word maker. You are creative, funny, assertive.

This is the first introduction where you tell them “what the chatbot is”. This is a system prompt.

The second one is the real instruction:

Combine the word “+word_a+” and the word “+word_b

This is the real instruction, here we want an output, that’s why we defined it as user. Then you give them another (the third) instruction:

Then you explain the meaning of this made-up word.

This is more of an explanation. We define this as an assistant.

And the last (the fourth) one is the last thing we want, so it is again a user:

What does it mean?

2.2 The app part

Then we are building an app to do exactly what I showed you in the introduction.

Note!!! In this section you also need to import PIL to show the image in the app. Again, please do it with the following code or comment that part if you don’t care about the image 😔

pip install PIL

This is pretty boring actually. You are just building the HTML to run the app.

This is the whole code (made by stacking the two pieces together):

import openai 
from constants import *
from PIL import Image
import streamlit as st

def word_maker(word_a,word_b):
return openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a word maker. You are creative, funny, assertive."},
{"role": "user", "content": "Combine the word "+word_a+" and the word "+word_b},
{"role": "assistant", "content": "Then you explain the meaning of this made up word."},
{"role": "user", "content": "What does it mean?"}
]
)['choices'][0]['message']['content']


if __name__=='__main__':
openai.api_key = OPENAI_KEY
image = Image.open('NameMakerImage.jpeg')
st.image(image, caption='Photo by Brett Jordan on Unsplash')
st.header("Write two different words")
st.subheader('First word here: :point_down:')
text = st.text_input("Type here")
st.subheader('Second word here: :point_down:')
text2 = st.text_input("Type here", key = "last_name")
while text!='' and text2!='':
made_up_word_explained = word_maker(text,text2)
#text_res = st.write('Result='+made_up_word_explained)
st.markdown("""
<style>
.big-font {
font-size:32px !important;
}
</style>
""", unsafe_allow_html=True)
st.markdown('<p class="big-font">'+made_up_word_explained+'</p>', unsafe_allow_html=True)
break

3. One last example

One more example for you 😏

Image by author

This made me smile. OpenAI just playkidded me 🙃

4. Some takeaways!

In this code, we made a chatbot that, given two words, makes up a new non existing one. We also made an app using streamlit about that and played around a little bit.

What did we learn?

A. That with this new feature of OpenAI chatbot API you can be very creative, as you can actually instruct your chatbot to be whatever you want it to be.
B. That some of the problems that might have required a lot of coding back in the days can now be made automatic with a few lines of code
C. That wordplays are very fun and I think this could be a very nice game to do with little kids 🙃

5. Conclusions

If you liked the article and you want to know more about machine learning, or you just want to ask me something, you can:

A. Follow me on Linkedin, where I publish all my stories
B. Subscribe to my newsletter. It will keep you updated about new stories and give you the chance to text me to receive all the corrections or doubts you may have.
C. Become a referred member, so you won’t have any “maximum number of stories for the month” and you can read whatever I (and thousands of other Machine Learning and Data Science top writers) write about the newest technology available.

--

--

Piero Paialunga

PhD student in Aerospace Engineering at the University of Cincinnati. Master’s degree Physicist, Data Scientist, professional google-searcher.