100 Days of Python, Project 082 – Personal Portfolio Website #100DaysofCode

This felt like an odd one, but probably still a good one to be included.  I also was kind of torn on if I even needed/wanted to actually “do” it.  This part is effectively already “Done”.  We created a GitHub.io site.  We created a Heroku based Blog (which I skipped in favor of doing it on my own server/host).  I also basically already have a portfolio website.  Right here, on this blog.  I post my projects, I write about them, etc etc.  I also have a pretty nice GitHub page, which is the best “programming portfolio” one can have.

So I was going to skip it initially, but instead, I opted to go with getting Flask to work on my web server, which was more of a hassle than I expected, but now, over time, I can start modifying that site to show case some of the things I’ve built.  Currently, it resides at https://joshmiller.net .  It’s nothing fancy as of this writing, just the bare bones of the Flask Blog built previously.  I did strip out the registration portion.  I really don’t need or want to deal with comments.  Sorry, @me on Mastodon instead.  I don’t really need a blog either, so don’t expect much going on there, but I want to keep it for now because I may use it to basically, blog about the blog.  I also want to build some new features over time to make it more full featured.  For starters, it needs a settings/admin page, to set things like the title and toggle comments on/off.  It also desperately needs an upload portal for images for use in blogs.

So what’s the point.  Well, for one, it has built in Authentication.  Which means I can build pages and tools that only I will ever have access to.  One I have been thinking of building would basically be a blog post form, that simultaneously posts to several social networks, using APIs.  Also, related to authenticating, the functional nature of these Flask Apps, means I can easily drop in say, the Top Ten Movies website, and have it show up at joshmiller.net/movies or something, just by adjusting some of the code.  And with the built in authentication, I can lock the world out from being able to edit my movies list.

I also, have been really looking for SOME use for that domain.  Though, I still may reconsider and move it all to a sub domain on this site.  I have not decided.  I already stopped using that domain for email and it essentially just… exists… now.

Anyway, I think it’s worth talking about how I got things going, because I had a hell of a time getting Apache to work nicely with Flask and WSGI.  And I looked into a lot of guides…. a LOT.

Honestly it’s been so many I will probably miss something here.  This is going to be a bit general and not nearly as hand holdey as it could be.  Chances are, if you can’t figure this out, you probably shouldn’t be doing it.  And chances are, you already have a web server with Apache going, so you kind of know what you’re doing.  The first step is to make sure Python is installed, and Python3, and the various needed imports, using pip3, not pip.

sudo apt-get install python3

sudo apt-get install python3-pip

If you need them.

Download your flask app to a folder /var/www/FlaskApp .  It should just be in the base here, but it can be elsewhere if you know what you’re doing.  Then run the main.py file.

python3 main.py

Fix any permissions issues, or, for now try sudo python3 main.py.  For everything it can’t find each run, do

sudp pip3 install <Package>

Eventually the main.py should run, and you can access your site at <ServerIP:5000>.  The truck now is getting Apache to work with it.  First you’ll want to install mod-wsgi then enable it.

sudo apt-get install libapache2-mod-wsgi python-dev

sudo a2enmod wsgi

Now, in the folder with main.py, the /var/www/FlaskApp folder, create a file called flaskapp.wsgi.

sudo nano flaskapp.wsgi

Inside this file, add the following.

#!/usr/bin/python

import sys

import logging

logging.basicConfig(stream=sys.stderr)

sys.path.insert(0,"/var/www/FlaskApp/")

from main import app as application

application.secret_key = 'Your Secret Key, it may Need to be the same as the one in main.py I am not sure'

application.root_path = '/var/www/FlaskApp'

Save that file.

Next create the site file for Apache to use.

sudo pico /etc/apache2/sites-available/YOUR_SiTE_FILE_NAME_CAN_BE_WHATEVER_MAKE_IT_MEANINGFUL.conf

Inside that file, add the following.  Please note, this file also contains settings that allow for SSL via Let’s Encrypt, which you should be running on your web server anyway.

<IfModule mod_ssl.c>

SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)

<VirtualHost *:443>

        ServerAdmin YOUR EMAIL HERE

        ServerName YOUR SITE DOMAIN OR SUBDOMAIN.DOMAIN HERE

        ServerAlias www.YOUR SITE DOMAIN HERE SO WWW REDIRECTS REMOVE FOR SUB DOMAINS

        WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi

        <Directory /var/www/FlaskApp/FlaskApp/>

          Order allow,deny

          Allow from all

        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        CustomLog ${APACHE_LOG_DIR}/access.log combined

# If you don't have Lets Encrypt you can probably dump this next section through <VirtualHost> Keep that

        Include /etc/letsencrypt/options-ssl-apache.conf

        Header always set Strict-Transport-Security "max-age=31536000"

        SSLUseStapling on

        Header always set Content-Security-Policy upgrade-insecure-requests

        # Lets Encrypt Folders below

        SSLCertificateFile /etc/letsencrypt/live/YOURSITEDOMAIN.com/fullchain.pem

        SSLCertificateKeyFile /etc/letsencrypt/live/YOURSITEDOMAIN.com/privkey.pem

</VirtualHost>

</IfModule>

Now, the most important thing to note here.  In every single guide I found, it included a section to alias the “Static” directory.  I got to a point where my site was working, but it was not loading CSS.  I fixed this by REMOVING this alias.  

I beleive that having “application.root_path = ‘/var/www/FlaskApp'” inside the wsgi file, confliced with the Alias.  But the application wasn’t running at all without “application.root_path = ‘/var/www/FlaskApp'”.  The end result, was a working blog site.  I could create a user, create new posts, leave comments, and everything looked as it should.  It’s working now though, at least, which was the goal.  I can polish it up over time.  

Like I mentioned previously, I am not sure what or how much I will post there.  I may actually completely remove the blog part of it.  I’m more interested in the admin/login functionality really.  I already have plenty of blogging outlets between this site and Mastodon, and using Tumblr more and probably a fresh new push at Lameazoid.com as well.  I don’t need this additional random blog at all.

Advent of Code 2022, Day 1

Another year, another Advent of Code. Though I kind of skipped last year. I think I may have been too busy or something. I do enjoy doing these puzzles, though they aren’t really “code projects”.  Each day is just a puzzle, that is solved with math.  Most of the problem, is figuring out what exactly is being asked.  The other part is just, figuring out how to format the data set you are given.  A lot of these are kind of repetitive, open your data file, read it in, convert it to a useful format, loop through the data, performing some action, and output some sort of total.

For this year’s theme, Santa’s Elves are taking a journey to a magical grove to collect food for the reindeer.

Day 1 – Elf Food Calories

The Problem Part 1:

The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves’ expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important consideration is food – in particular, the number of Calories each Elf is carrying (your puzzle input).

The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they’ve brought with them, one item per line. Each Elf separates their own inventory from the previous Elf’s inventory (if any) by a blank line.

In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they’d like to know how many Calories are being carried by the Elf carrying the most Calories.

The Problem Part 2

By the time you calculate the answer to the Elves’ question, they’ve already realized that the Elf carrying the most Calories of food might eventually run out of snacks.

To avoid this unacceptable situation, the Elves would instead like to know the total Calories carried by the top three Elves carrying the most Calories. That way, even if one of those Elves runs out of snacks, they still have two backups.

So, each day consists of a Part 1 and a Part 2.  Generally speaking, each of these two parts is semi related, and can be completed with most of the same code. This one is simple enough, split the list up by lines, then split it into each elf’s calories, sort the list so the highest valies are at the front and print those three values out.  Part 1 only needs the highest, Part 2 needs the next two.

with open("Day01Input.txt") as file:
    calories = file.read()

calories = calories.replace("\n"," ")
elves = [each.split(" ") for each in calories.split("  ")]
totals = []

for each in elves:
    total = 0
    for i in each:
        total += int(i)
    totals.append(total)
 
totals.sort(reverse=True)
print(totals[0]+totals[1]+totals[2])    

100 Days of Python, Project 081 – Morse Code Generator #100DaysofCode

I feel like these last 19 projects are more complex and should get their own post each.  Just skimming over what they are, they all seem to be quite a bit more complex than a lot of the previous projects, and they also are given no help or instruction, just “Make X”.

I wanted to comment a bit briefly on the course as a whole so far though.  I’ve really enjoyed it, and I have lots of idea of projects I WANT to do, and I have started on a few, but I’m doing my best to force myself to focus on finishing this course FIRST.  Eyes on the Prize, so to speak.  The flow overall is pretty good, though I noticed some of the course comments, many people were complaining a bit during some parts.  Mostly about the lack of videos during the final, third or so, of the course.  I admit, I kind of feel for them a bit, but I also do somewhat get the point.

The point is, at some point, you do need to do this stuff without everything being hand-holdey the whole way.

That said, she was doing a pretty good job of this already within each “Broader” topic.  For say, Turtle Graphics, she would start off being extremely “Do X, Do Y, Do Z” about pretty much every step of a project.  Next lesson, there would be a bit less, then by the third it would ask the students to do something, then give a solution.  Eventually it was just, “Do a while project” with “here is a solution”.  It was gradual over 5-6 lessons.  

It feels like she tried to do this quite a bit with the overall course as well, the problem is, when you end up in an area that is completely unfamiliar again, like the Flask sections, or the Data Analytics sections, it would be a bit nice to start each “New Section” with some videos and a bit more help.

Personally, I didn’t find it too much of a problem, I was already doing many of the projects without watching any of the videos, then watching videos afterwards to see how she did it.  The user comments also were really great for suggestions and ideas.

I would still recommend the course.  It seems to be one of Udemy’s best selling courses too.

Anyway, on with the project, and I admit, I am kind of adding some filler.

Day 81 – Morse Code Translator

Ok, so, compared to what the rest of the “Professional” level projects seem to be, this particular project felt stupid super easy.  Like, “am I doing something wrong” easy.  I may have even already done this one evening on CodeWars.com.  I even fleshed it out a bit just to make it more interesting.

The project was to translate a user input into Morse Code.

Even just thinking about that, at it’s core, it’s literally just find and replace on a string.

Step One – Create a Dictionary of Morse Code sequences and the equivalent Alphabet letters

Step Two – Get a User Input

Step Three – Loop through the Input and convert each character using the Dictionary to Morse Code

That is IT.  That’s nothing.  The assignment also specified “Text Based”, though I considered converting it to work in TKinter or maybe even Flask.

I did spruce things up a bit, I added a prompt so the user can translate additional strings.  I looked up how to make sound output, and added an option to play the Morse Code out over the PC Speaker, which was fun, and new useful information.

Anyway, it’s on Github, but this is the entire code.

import winsound
from time import sleep

# Morse Code
morse_code = {
    'a': '·−',
    'b': '−···',
    'c': '−·−·',
    'd': '−··',
    'e': '·',
    'f': '··−·',
    'g': '−−·',
    'h': '····',
    'i': '··',
    'j': '·−−−',
    'k': '−·−',
    'l': '·−··',
    'm': '−−',
    'n': '−·',
    'o': '−−−',
    'p': '·−−·',
    'q': '−−·−',
    'r': '·−·',
    's': '···',
    't': '−',
    'u': '··−',
    'v': '···−',
    'w': '·−−',
    'x': '−··−',
    'y': '−·−−',
    'z': '−−··',
    '0': '−−−−−',
    '1': '·−−−−',
    '2': '··−−−',
    '3': '···−−',
    '4': '····−',
    '5': '·····',
    '6': '−····',
    '7': '−−···',
    '8': '−−−··',
    '9': '−−−−·',
    ' ': '/'
}

# Loop Variable
keep_going = True
valid_answers = ["yes","y","no","n"]

# Sound Variables
frequency = 700  # Set Frequency To 2500 Hertz
duration_short = 100  # Set Duration To 100 ms == .1 second
duration_long = 300  # Set Duration To 300 ms == .3 second

while keep_going:
    # Get String of Text to Convert
    conversion_string = input("Please enter a string to convert to Morse Code:\n").lower()
    # Fresh Code String Each Time
    code_string = ""
    # Do the Conversion
    for letter in conversion_string:
        if letter in morse_code:
            code_string += morse_code[letter]+" "
        else:
            code_string += letter
    # Show the Result
    print("Your Morse Code is:\n")
    print(code_string)
    # Ask if the user wants to hear the sound
    go_on = ""
    while go_on not in valid_answers:
        go_on = input("Would you like to play this sound? (Yes/No) ").lower()
    # If Yes, Play the sound
    if go_on == "yes" or go_on == "y":
        for beep in code_string:
            #print(beep)
            if beep == "−":
                winsound.Beep(frequency, duration_long)
            elif beep == "·":
                winsound.Beep(frequency, duration_short)
            # Needs a brief pause
            sleep(.05)

    # See if the user wants to do another conversion.
    go_on = ""
    while go_on not in valid_answers:
        go_on = input("Translate another string? (Yes/No) ").lower()
    # Quit if no more conversions
    if go_on == "no" or go_on == "n":
        keep_going = False

A Progressive Journey Through Working With AI Art – Part 4 – Better Prompts

The next step in my journey to better AI Art, was better prompts. Which also has sort of landed me on just using one complex prompt I found and modifying it as needed, which works very well. I started off by adding more descriptive words to the basic prompts. Including Camera models which was suggested by quite a few people.

  • “In the Style of Manga”
  • “An oil Painting Of”
  • “A Pencil Sketch of”
  • “in the style of [artist]
  • “Realistic”
  • “Hyper-realistic”
  • Canon 5D

This worked better. But I started looking around on the Stable diffusion Sub-Reddits for good prompts to use. I came across the following Prompt:

, (humorous illustration, hyperrealistic, big depth of field, colors, night club scenery, 3d octane render, 4k, concept art, hyperdetailed, hyperrealistic, trending on artstation:1.1)

Negatives:
text, b&w, (cartoon, 3d, bad art, poorly drawn, close up, blurry, disfigured, deformed, extra limbs:1.5)

Which I have used and adapted quite a lot. Essentially, everything in front of the first Comma is your actual prompt. This is essentially, what I have been doing. Everything after refines things a lot. You can also change the background by editing the “night club scenery” bit.

Anyway, the rest of the post is sharing some more pics based on this prompt.

Prompt: “Tracer from Overwatch” +

As normal, really iffy on the hands, but still some neat concepts that could actually be skins in the game.

Prompt: Godzilla +

Prompt: Several different Batman Prompts (Batman Fighting, Batman Overlooking Gotham, Batman Battling Joker)

Prompt: The Joker +

These are some of my favorites so far. I am not a huge Joker Fan really, but they do a REALLY good job of portraying the more modern crazy that is The Joker. I actually left a few off because frankly, they are super creepy, but really are nice.

Prompt: Professor Layton

Again, it has no idea who Layton is, but still seems to do really well with the Aethetic of Layton. Which is kind of odd honestly.

Prompt: An Adorable Pixar Kitten

Feels like Pixar styled art is cheater mode a bit but these came out pretty good as well.

Three Prompts with similar results, A Norwegian Landscape, The Lord of the Rings, and Arya Stark,

It’s kind of crazy just how much better the results have gotten from previous attempts, especially just like, 6 months ago or something, when I started playing with this concept using online tools. That said, it also gets old pretty quick, and you end up with a lot of “Weird shit” output, extra limbs, weird proportions, extra elbows, odd faces. I can see how it might be useful to produce some generic banner backdrops and whatnot. I also can see it just getting even better, very rapidly. If hands can be figured out, that would be a real game changer.

A Progressive Journey Through Working With AI Art – Part 3 – Running Natively with Automatic1111

After experimenting with online sources, then running Stable Diffusion locally using Windows Subsystem for Linux, I wanted more, and better, because I knew my machine was capable of much more. So I looked into alternatives and found Automatic1111’s Stable Diffusion variant.

The core take away here, is how this is like night and day in performance and quality.

Previously, with WSL, I would run batches of prompts and seeds and maybe get a few okish results. Also, any dimensions larger than the base 512×512 would crash the thing and I’d get nothing. Basically, it definitely was not exploiting the full potential here. It also completely dogged my entire rig down while building an image, which took maybe 5-10 minutes for the actual processing to work.

It still dogs the machine down, but not nearly as much as it had been. And it takes like 10-20 SECONDS to produce an image. The image quality is also like 1000 times better, though not still without that “AI Art Wonkeyness” like these weird square cows.

—- Read More —-: A Progressive Journey Through Working With AI Art – Part 3 – Running Natively with Automatic1111 Read More