AI Hello World - Part 1To install, open a terminal and type this:
sudo apt install python3 python3-pip
pip3 install torch torchvision torchaudio transformers
Try AI generated text with GPT-Neo-2.7B, a pretrained model. The 2.7B stands for 2.7 Billion parameters. At the first run, the code will auto-download the GPT-Neo-2.7B model, which is a 10GB download (by default downloaded in ~/.cache/huggingface/transformers/). Once downloaded, it will all run entirely locally, no further downloads, no subscription and no Internet required.
To run it, open a terminal and type (only the text, without the ">>> "):
python3
>>> from transformers import pipeline
>>> generator = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B')
>>> text_seed = "A horse, a frog and a giraffe walk into a bar."
>>> ai_answer = generator(text_seed, max_length=128, do_sample=True, temperature=0.7)
>>> print(); print( ai_answer[0]['generated_text'] ); print()
>>> # press arrow up twice, then Enter, to re-run the ai_answer = ... and the print() ... lines again
>>> # if answers take too long, make max_length=50, for more creativity in answers try with temperature=0.9
>>> exit()
That's all. From now on it can run offline any time.
\[ \star \ \star \ \star \]
Some examples of AI generated answers (on a i7/4GHz it takes about 1 minute to answer):
text_seed = "A horse, a frog and a giraffe walk into a bar."
A horse, a frog and a giraffe walk into a bar. The frog says, “It’s been a while since I’ve had a drink, but I’ll have one.” The giraffe says, “I’m going to have one, too.” And the bartender says, “How about a drink for the whole group?” The horse says, “Are you going to pay for this?” And the bartender says, “No, it’s on me.”
Wow, that was a disturbingly OK answer, WTF?!
and pardon my French. Wonder what would happen if we try to troll the AI. Let's write in French.
text_seed = "La mécanisation de l'agriculture a changé la vie de nos villages."
L'économie est devenue stratosphère, la croissance économique était née de la mécanisation de l'agriculture et de l'élevage, les salaires augmentent et les coûts de production sont plus bas.
Lol, nice usage of the "croissant" word
, wonder who's trolling who here, or as we in Ro use to say, "Unde dai şi unde crapă." (meaning in Ro - "where you hit vs. where it splits"). Let's try the Romanian text, too, with the AI.
text_seed = "Unde dai şi unde crapă."
Unde dai şi unde crapă. În ciuda faptului că, în ciuda unui astfel de scop, ceva întâmplător se va întâmpla, cineva va întâlni, într-un post de televiziune, un politicien care-l va acuza pe un lider politic de corupţie, şi cineva va spune, în numele lui, ...
Meh OK, but the fact alone that it answered in plain Romanian felt uncanny already, and unsettling. Though, is all these of any use, other than for entertainment that gets old quick? Could be of use. For example, it can "speak" Python, too, so GPT-Neo can be used as a programming companion:
text_seed = "def palindrome(string): # returns the string backwards"
def palindrome(string): # returns the string backwards in order
for i, char in enumerate(string):
if char!= string[-i - 1]:
return string[:i] + string[-i - 1]
return string
Usage:
print(palindrome("I ate a piece of pie yesterday")[::-1])
Output:
I ate a piece of pie yesterday
Note the usage example contains the pythonic way of reversing a string, "reversed = string[::-1]"
text_seed = "def bubble_sort(list): #sort a Python list"
ai_answer = generator(text_seed, max_length=200, do_sample=True, temperature=0.9)
def bubble_sort(list): # sort a Python list in ascending order
for i in range(len(list)): # for each item in the list
if list > list[i+1]:
list, list[i+1] = list[i+1], list
Here is a version that uses sorting.sort() and then loops over the sorted list repeatedly:
def bubble_sort(list):
sorted_list = sorted(list)
for i in range(len(sorted_list)):
Could be helpful.
\[ \star \ \star \ \star \]
Though, keep in mind that some answers might not be that good. Sometimes it returns sloppy answers, or it starts repeating the same group of words over and over (increase the "temperature" if you get repetitions).
Anyway, the point is: Installing and running an AI is easier than one might guess.
\[ \star \ \star \ \star \]
AI Hello World - Part 2https://www.eevblog.com/forum/programming/ai-hello-world/msg4365268/#msg4365268\[ \star \ \star \ \star \]