Skip to content

Instantly share code, notes, and snippets.

View peterc's full-sized avatar
🏠
Working from home

Peter Cooper peterc

🏠
Working from home
View GitHub Profile
@peterc
peterc / groq_client.rb
Last active April 19, 2024 17:45
Basic Groq API client for Ruby
require 'http'
require 'json'
class GroqClient
def initialize(api_key: nil, api_url: "https://api.groq.com/openai/v1/chat/completions", model: "mixtral-8x7b-32768")
@api_key = api_key || ENV['GROQ_API_KEY']
@api_url = api_url
@model = model
end
@peterc
peterc / index.html
Created October 13, 2023 13:57
Basic example of using import maps and TailwindCSS in a buildless fashion
<!DOCTYPE html>
<!-- A totally pointless and overkill use of import maps
and Moment.js, in order to simply use the technology and
see it in action.. -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Buildless example</title>
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
@peterc
peterc / 404.md
Last active October 4, 2023 21:10
A pretend 404 page

A 404 page.. kinda

The content which was previous linked to by the link you clicked has been removed for quality control reasons. Sorry! On the plus side, you didn't waste your time reading something that wasn't actually very good :-)

@peterc
peterc / embedding_store.rb
Last active December 28, 2023 06:27
Using SQLite to store OpenAI vector embeddings from Ruby
# Example of using SQLite VSS with OpenAI's text embedding API
# from Ruby.
# Note: Install/bundle the sqlite3, sqlite_vss, and ruby-openai gems first
# OPENAI_API_KEY must also be set in the environment
# Other embeddings can be used, but this is the easiest for a quick demo
# More on the topic at
# https://observablehq.com/@asg017/introducing-sqlite-vss
# https://observablehq.com/@asg017/making-sqlite-extension-gem-installable
@peterc
peterc / perceptron.rb
Created June 7, 2023 20:30
Basic implementation of a perceptron in Ruby
class Perceptron
def initialize(inputs, bias = 0.0)
@weights = Array.new(inputs.keys.first.size) { rand }
@inputs = inputs
@bias = bias
end
def run(inputs)
z = inputs.zip(@weights).map { |i, w| i * w }.reduce(:+) + @bias
1.0 / (1.0 + Math.exp(-z))
@peterc
peterc / webui_client.rb
Created April 12, 2023 22:41
Basic Ruby client to the Automatic1111 WebUI API
require 'json'
require 'rest-client'
require 'base64'
url = "http://localhost:7860"
payload = {
"prompt": "liminal space, unusual lighting, sinister presence",
"negative_prompt": "",
"steps": 30,
"sampler_name": "Euler a",
@peterc
peterc / email_list_mx_checker.rb
Created March 13, 2023 12:53
Email list MX record checker
# Read stdin, extract email addresses
# Look up MX records for those addresses
# Return each email address and their first MX record
# (or IP address, as a fallback)
hosts = {}
STDIN.each_line do |l|
# Extract the email and the email domain from each line
email = l[/^([^\s]+\@[^\s+\,]+)\b/, 1]
@peterc
peterc / wikimediaips.py
Created March 11, 2023 00:06
Get the IPs that MediaWiki / Wikipedia blocks programmatically
import requests
import time
# Initialize variables
api_url = "https://meta.wikimedia.org/w/api.php"
params = {
"action": "query",
"format": "json",
"list": "globalblocks",
"bglimit": "500"
@peterc
peterc / songs_to_spotify.py
Created March 8, 2023 23:20
Python app to take a list of songs and add them to a Spotify playlist
# finds songs by names listed in a text file
# and adds them to a spotify playlist
import pprint
import re
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from iteration_utilities import grouper
playlist = ...
@peterc
peterc / gist:6fce570061cc179ff262791b0bc45adf
Last active February 16, 2023 00:24
ufw rules to block abusive IP ranges in 2023
ufw insert 1 deny from 2.59.50.0/24 to any
ufw insert 1 deny from 5.183.130.0/24 to any
ufw insert 1 deny from 31.40.203.0/24 to any
ufw insert 1 deny from 45.11.20.0/23 to any
ufw insert 1 deny from 45.11.20.0/24 to any
ufw insert 1 deny from 45.11.21.0/24 to any
ufw insert 1 deny from 45.15.72.0/23 to any
ufw insert 1 deny from 45.15.72.0/24 to any
ufw insert 1 deny from 45.15.73.0/24 to any
ufw insert 1 deny from 45.15.236.0/23 to any