These Python and Raspberry Pi projects. They are fun aren’t they? And often they look deceptively simple. But you don’t see all the projects that failed and usually not where they struggled. This project got stuck (and almost failed) at:
Not being able to scrape dynamic website content.
When I found out how to do that, I couldn’t run my working Python code on the Raspberry Pi.
That turned out to be because the scraping packages use a chromium browser, but not for the ARM processor that the Raspberry Pi has.
And to top it all off, the Python package for the Inky Impression e-ink display had some kind of problem running numpy.
In my spare time I do stage acting. And there is almost no better feeling having performed a play really well. But to do so, you need to learn your lines. Preferably you learn your text well in advance, so you also have time to play with it.
Memorizing things is not something I’m particularly good at. For remembering things I have apps: calendars, Evernote, mail. But that doesn’t work when you’re on stage of course.
Previously, to train myself, I would record the scenes I was in, without the parts I was supposed to say. And then I would replay that recording and try to fill in the gaps. But it took quite a number of turns to get that right.
Last production I tried something else: spaced repetition. The principle of this is that you space out review sessions where hard questions return earlier and easy questions reappear in your “deck” in later review sessions.
For this I’ve used open source software called Anki. In Anki you create flashcards and Anki will run study sessions where it will determine based on spaced repetition which cards should be reviewed.
In Anki you create a deck. In my case the deck has the name of the play/production I’m studying for.
Here is a flashcard for a hypothetical play called Project Hail Mary. (Unfortunately the Venn diagram between stage actors and sci-fi nerds doesn’t seem to intersect all that much, based on my past experiences. It is still my dream to bring sci-fi to the stage some day.).
I use the line of the actor before me as the question, and my line as the answer. If I have a lot of text, or even have a monologue, I use my previous lines as question and the next line as the answer. I try to keep the amount of text in the answer limited if possible.
I use tags to add a card to a scene. It’s okay that Anki goes through these cards randomly.
When have added your cards and are finished adding more, you can start a study session:
Anki will start showing you the question. You don’t have to answer it. You have to memorise what the answer is.
Then you click “Show answer” and you can select how hard or easy you thought that answering was. If you choose easy, it might take multiple days before the question returns. If you choose hard, it can come back in minutes.
When you have gone through the deck you get this message. And you’re done for the day.
It’s up to you how soon you want to check in with Anki again. Anki doesn’t have some kind of scheduling system. It’s completely up to you to fire it up when you feel the need to study again.
But if you fire up Anki a couple of times per week, you will find that you will get better at it. And it takes significantly less time than my previous method.
And of course this method can be used for many things you need to memorise. Like that Azure DP-203 exam I have on my todo list somewhere.
I have a hobby project I’m working on. It’s an astronomy news feed reader. Long story short: I currently gather links to interesting articles about astronomy by hand. And I want to automate this, so that I have more time to actually read the news.
What I want is that an article, based on its contents, will be tagged with a couple of keywords. And also that it is placed in one main category. For example: an article on the discovery of volcanos on Venus can have tags like “Venus”, “vulcanism”, “Magellan” and the main category is “Venus”.
So how to do that in Python? I’ve looked it up and according to Stack Overflow I need to start reading books on data mining and Natural Language Text Processing. Hmm, no. Not for a hobby project. So I was wondering. Can’t ChatGPT do it?
Can’t ChatGPT do it?
Short answer: it can. Here is how I did it. First I learned how to use ChatGPT with Python from Harisson Kinsley’s video on this topic:
As Harrison explains in his video: you are going to need an OpenAI account. And using ChatGPT from Python costs money. Check the pricing here: https://openai.com/pricing. So far I’ve spent a handful of dollarcents for about 50 ChatGPT queries. But if you’re going to experiment with this, don’t forget to set some usage limits, so you won’t get unpleasant surprises.
Creating tags for a text with ChatGPT
The idea is to send ChatGPT a question with the text that needs to be categorised in it. So I create a string “Categorise this text with one to five tags: <text of the article here>”.
newstext = "As Canada celebrates its first astronaut to go to the moon, it is starting a new project that could eventually enable a Canadian to walk on the lunar surface. <more text here>"
user_input = f"Categorise this text with one to five tags:\n\n {newstext}"
message_history.append({"role": "user", "content": f"{user_input}"})
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=message_history
)
reply_content = completion.choices[0].message.content
print(reply_content)
- U.S. Space Force
- Greenland
- Department of Defense
- Pituffik Space Base
- Cultural heritage
Wait, what? Why the dashes all of a sudden? And this time no period at the end.
So ChatGPT 3.5’s results can be as inconsistent as when humans do this thing. Results get better when you tell ChatGPT to deliver the tags as comma delimited:
user_input = f"Categorize this text with one to five tags:\n\n {item['summary_detail']}." \
f" Print the tags separated by commas."
But that’s the lesson here: you have do be very clear and specific about what you want.
One main category from a list
Now I want ChatGPT to pick one main category from a list I have picked. This is the list:
astro_categories = "Mercury, Venus, Moon, Earth, Mars, " \
"Jupiter, Saturn, Uranus, Neptune, " \
"Pluto and the Kuiper Belt, Comets, " \
"Exoplanets, Formation of the Solar System, " \
"Telescopes, Meteorites, " \
"Artificial Intelligence, Miscellaneous"
Because you can have a message history in ChatGPT, I can ask follow-up questions, based on the text I gave it earlier.
user_followup = f"Also categorize this text in one of the following categories:\n\n {astro_categories}"
message_history.append({"role": "user", "content": f"{user_followup}"})
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=message_history
)
reply_content = completion.choices[0].message.content
print(reply_content)
Let’s see what ChatGPT makes of it.
For the article about the Ariane 5 rocket for the JUICE mission, it picks:
Jupiter.
That’s good. I didn’t ask for the period at the end, but I can work with that.
Now let’s look at the Thule Air Base article. ChatGPT chose this as the main category:
space technology, rocketry, China, Tianbing Technology, TH-11V engine.
But the response on picking the main category was:
The text should be categorized under "space technology" and "rocketry" as these are the most relevant categories. It doesn't fit neatly into any of the specific celestial bodies or topics listed in the second prompt, nor does it relate to artificial intelligence or meteorites.
Sounds reasonable. But you definitely need to be aware of such possible responses before you start relying on ChatGPT’s results.
So then I told ChatGPT to only pick one category. And if you don’t know what to do, pick Miscellaneous.
user_followup = f"Also categorize this text in only one of the following categories:\n\n {astro_categories}." \
f"If it doesn't fit in any of these categories, categorize it under Miscellaneous."
pop culture, television, 3D printing, Star Trek, Picard
What main category does ChatGPT pick now?
Telescopes
Telescopes? Really? The word “telescope” does not appear once in the article. (Also, this time no period at the end!)
Conclusion
First of all: I’m really happy that ChatGPT can come up with relevant tags. That works quite well and I intend to use it.
Second: if you want to use ChatGPT (version 3.5) in your data pipelines, you better be prepared for some very rigorous testing. Because it can sometimes throw some weird curveballs that can mess up the data quality equally well as humans can.
This is part 3 of a series of blogposts on how I created a Strava dashboard on a Inky Impression e-ink display with a Raspberry Pi.
OAuth2
This was the part that I expected to be the hard part: getting my data from Strava. Or, to be more precise: getting the connection right so the Strava API would allow me to get that data. Because it requires authentication via the OAuth2 protocol and I’ve tried a similar thing a few years back with a Google API and I just didn’t get it. But now I do.
In last blogpost we set up the Raspberry Pi, attached the Inky Impression display and got the Raspberry Pi ready for remote access.
Time to get the Inky Impression software installed and make the Inky Impression screen display something.
Your SSH connection of choice
For this we’re going to have to run some commands via remote SSH. There are multiple ways to log in remotely. You can use a tool like Putty or the terminal on MacOS (I like iTerm2). That’s actually simpler.
But I chose to use Visual Studio Code because you can edit Python code remotely via SSH straight on the Raspberry Pi.
To do this you must install Visual Studio Code. Visual Studio Code has all kinds of extensions. Here we will install the Remote – SSH extension. And while you’re at it, maybe install the Python extension as well, because we will be writing some Python later.
(The Inky Impression comes with a 40-pin female header included to boost height for full-size Pis and standoffs included to securely attach to your Pi)
Initially: keyboard, mouse and monitor (but if you configure the WiFi on the Raspberry Pi and configure it to allow remote SSH, you can connect to it via WiFi from the convenience of your regular computer)
For those who don’t know a Raspberry Pi: this is a very small and quite cheap computer. The Raspberry Pi 3B+ I’ve used for example is about 40 euros. But you can spend even less, because my Strava dashboard doesn’t exactly require a lot of computing power.
So you could instead use a Raspberry Pi Zero 2 W (15-25 euros), which takes up less space also. But I believe this will require soldering to attach the GPIO. And it seems to be out of stock on a lot of sites.
Let’s face it: my purchase of the Pimoroni Inky Impression 5.7 inch display was a solution looking for a problem. I saw a video about it and I was sold on the idea of having an e-ink display on one of my Raspberry Pi’s.
While having a 7-colour e-ink display is cool and all, I had to come up with a good plan to utilize one. So it wouldn’t end up in a drawer after a short experiment.
You can use it to display images, but it is 7-colour. So you have to “dither” full colour images to have it display well. Actually comic book style images are displayed much better than the average dithered photo. The resolution is quite low (600×448) and the refresh rate is quite slow (10-20 seconds). But for some applications this is just fine.
Have you heard about text-to-image models like DALL-E 2, Stable Diffusion and MidJourney? These are AI algorithms that take in text (the “prompt”) that describes what kind of picture you want as input and as output the algorithm creates that picture, based on billions of images.
An example could be “an astronaut on a bicycle on the moon by Van Gogh”. And this would be one of the results:
I got access to DALL-E 2 in July this year. DALL-E 2 is a closed source algorithm made by OpenAI. You can sign up to request access to DALL-E 2. Once you get access you can use it for free for a limited of runs. After that you have to pay to use it more.
Well, that was scary. Just before I went on holiday I switched providers for my marcel-jan.eu domain. And while I had some time build in before going on vacation, there were problems with the transfer code not working. Because apparently the .eu domain is different from the regular .nl domain.
In the end I managed to get my marcel-jan.eu mail working just the evening before leaving. But I saw no way to migrate the blog while packing my bags. So the blog was down for more than 2 weeks. Did anybody miss it?
After getting back home I had to piece back the WordPress blog with a .zip backup and a backup of the filesystem. Never done such a thing before. And the original WordPress blog on my old provider’s site was already gone. So there were no more alternatives to do a better export.
Importing did not go as planned
I started by installing WordPress at my new provider’s site. And I went to PHPMyAdmin, which is the tool to work with the database behind WordPress. I imported the .zip (with a .sql file in it). And.. no blogposts. A further look with PHPMyAdmin in the database showed that there were several xxx_posts tables. The one the WordPress site was looking in, was wplx_posts. My imported tables where called wp_posts and 4a2vK12BOL_posts. wp_posts contained old stuff. The 4a2vK12BOL_posts table turned out to have all my posts.
Time to play dirty with SQL
So how do I point WordPress to the right data? It’s good to have some SQL skills. What if.. hear me out.. I read the .sql file I got from the export, pick out the SQL to import the 4a2vK12BOL_posts table. Search and replace in the SQL text the term “4a2vK12BOL_posts” for “wplx_posts” in a text editor? And then import that? It’s dirty, I grant you that.
But it turns out, it works. As long as you don’t create any new posts beforehand that use the same ID as the ones you try to import. A quick removal of the Hello World post made sure of that.
And it worked. I got my posts back. Okay, that’s something. I don’t have to type all my writings from 2017 to now again.
I did something similar for the comments. Make sure you do that before the first comment spam arrives. Because it will overlap the ID in the comment table with the ones you try to import.
Now I need some images
I was not really surprised that restoring table contents did nothing for my images. Pretty sure that had to come from the filesystem. Luckily I had made a backup of all that. But where to get the image files and where to put them?
Well, looking over the sql for the posts table, I found references to image files like this one: https://marcel-jan.eu/datablog/wp-content/uploads/2017/11/Heart-Reanimation-65992.gif. So somewhere there should be a path with something like wp-content/uploads in the name and a lot of gifs and jpgs in it. I found that, uploaded the directories to the new site and now I had my images back.
That one time I used TablePress
My article about Lion’s Mane is one of the most popular blogposts for some reason. Lots of people who want to gain cognitive enhancement. (I wished my post about becoming a skeptic was just as popular. Oh well.) In that post was my one use of a TablePress table. How to get that back?
It turns out the data can be found in the options table. But I had some doubts whether importing it would mess other things up and whether TablePress would find it. So I dug in the Internet Archive to find the contents of the table, and used Excel to create a csv file of that table. Imported that in TablePress and hey presto: we got ourselves our table back.
Tags and categories
One thing I noticed that my categories and tags were gone. The categories were a big mess after 5 years of blogging. Actually it wasn’t a big loss. More like a good moment to rethink them. As for tags: it would be nice to retrieve them somehow.
From this I learned what tables I needed to import to get my tags back. It turns out it’s wplx_term_taxonomy and wplx_term_relationships. In wplx_term_taxonomy there were already 3 IDs taken. ID 2 and 3 were now a wp_theme, where in my old table they were categories.
I decided to remove ID 1, 2 and 3 from my insert statement and import that. If I’m missing 2 categories, that won’t hurt me a lot.
Anything else?
From the wp-staging article I learned I probably won’t be needing much more from the import. Maybe I will me missing some stuff from the options table, because there’s all kind of stuff that plugins put there. But I’m not going to open that can of worms.
I certainly learned a lot on WordPress and its database.. forcefully. Glad the blog is back on the road at my new provider.
Last June I made a short video while walking in the park next to the DIKW Intelligence office. And I posted it on LinkedIn. To my surprise it did very well. So I thought: why not make more of these short videos on data topics? And why not make them in somewhere in nature?
I’m on my bike almost every day this time of year. Surely I could make a short stop and do a little talk? I started to make them in Dutch and then also in English. Continue reading →