Skip to content

Instantly share code, notes, and snippets.

View fredmonroe's full-sized avatar

fred monroe fredmonroe

View GitHub Profile
@zz2115
zz2115 / ws_streaming.py
Created August 3, 2024 20:14
Chatbot built with fasthtml and ollama (websocket streaming)
from fasthtml.common import *
import ollama
import asyncio
# Set up the app, including daisyui and tailwind for the chat component
tlink = (Script(src="https://unpkg.com/[email protected]/tailwindcss.js"),)
dlink = Link(
rel="stylesheet",
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css",
)
@trygvebw
trygvebw / find_noise.py
Last active March 31, 2025 01:40
A "reverse" version of the k_euler sampler for Stable Diffusion, which finds the noise that will reconstruct the supplied image
import torch
import numpy as np
import k_diffusion as K
from PIL import Image
from torch import autocast
from einops import rearrange, repeat
def pil_img_to_torch(pil_img, half=False):
image = np.array(pil_img).astype(np.float32) / 255.0
@DannyQuah
DannyQuah / 2022.01-D.Quah-Obsidian-iPad-syncing-via-iSH-git.md
Last active January 4, 2025 09:44
Obsidian on iPad syncing via iSH, git, and GitHub

Obsidian iPad syncing via iSH git

by Danny Quah, Jan 2022

This gist describes using Obsidian on iPad while syncing to other Obsidian platforms. The procedure uses git in iSH on iOS, and thus differs from using either Obsidian Sync or Working Copy as described in Obsidian/iOS+app.

(To be clear, Obsidian is one of my favourite Apps, and I'm all for supporting the team financially. Moreover, everything I've heard suggests the paid Obsidian Sync is excellent. However, I don't want my syncing processes to proliferate --- each service using a different client sync flow --- so I keep my systems minimal: just syncthing and git. After writing this I found an Obsidian Forum writeup which uses the same tools I do to achieve the same goal, but you'll want to read that with its accumulated contributions dispersed across the comments. So at least I was thinking

@Flunzmas
Flunzmas / calc_2_wasserstein_dist.py
Last active April 1, 2025 11:11
Differentiable 2-Wasserstein Distance in PyTorch
import math
import torch
import torch.linalg as linalg
def calculate_2_wasserstein_dist(X, Y):
'''
Calulates the two components of the 2-Wasserstein metric:
The general formula is given by: d(P_X, P_Y) = min_{X, Y} E[|X-Y|^2]
For multivariate gaussian distributed inputs z_X ~ MN(mu_X, cov_X) and z_Y ~ MN(mu_Y, cov_Y),
@jeremiecoullon
jeremiecoullon / Jax_progress_bar_tqdm.py
Last active October 12, 2022 14:51
Add a tqdm progress bar to a JAX scan or fori_loop. Code from this blog post: This code is from this blog post: https://www.jeremiecoullon.com/2021/01/29/jax_progress_bar/
from tqdm.auto import tqdm
from jax import jit, vmap, grad, random, lax, partial
import jax.numpy as jnp
from jax.experimental import host_callback
def progress_bar_scan(num_samples, message=None):
"Progress bar for a JAX scan"
if message is None:
message = f"Running for {num_samples:,} iterations"
@kissgyorgy
kissgyorgy / listen.py
Created September 4, 2020 16:37
How to use PostgreSQL's LISTEN/NOTIFY as a simple message queue with psycopg2 and asyncio
import asyncio
import psycopg2
# dbname should be the same for the notifying process
conn = psycopg2.connect(host="localhost", dbname="example", user="example", password="example")
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
cursor.execute(f"LISTEN match_updates;")
@tranhoangnguyen03
tranhoangnguyen03 / interactive_google_oauth2.py
Created July 31, 2020 04:25 — forked from laranea/interactive_google_oauth2.py
Interactive Google OAuth2 flow with Streamlit
import asyncio
import streamlit as st
from httpx_oauth.clients.google import GoogleOAuth2
st.title("Google OAuth2 flow")
"## Configuration"
client_id = st.text_input("Client ID")
@okld
okld / multipage_settings_app.py
Last active July 11, 2024 23:55
Streamlit - Settings page with session state
import streamlit as st
from persist import persist, load_widget_state
def main():
if "page" not in st.session_state:
# Initialize session state.
st.session_state.update({
# Default page.
"page": "home",
@p3jitnath
p3jitnath / install-docker.sh
Last active December 21, 2024 09:19
Docker and Nvidia Docker installation in Ubuntu 20.04 LTS
# WARNING : This gist in the current form is a collection of command examples. Please exercise caution where mentioned.
# Docker
sudo apt-get update
sudo apt-get remove docker docker-engine docker.io
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
docker --version
@dmontagu
dmontagu / app.py
Created February 18, 2020 00:28
FastAPI + dash
# Based on the example from https://www.activestate.com/blog/dash-vs-bokeh/
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as obj
import uvicorn as uvicorn
from dash.dependencies import Input, Output
from fastapi import FastAPI
from starlette.middleware.wsgi import WSGIMiddleware