Skip to content

Instantly share code, notes, and snippets.

View sohang3112's full-sized avatar
:octocat:

Sohang Chopra sohang3112

:octocat:
View GitHub Profile
@sohang3112
sohang3112 / intset.py
Last active August 22, 2024 07:02
Efficient set of integers, maintained using bit shift operations on an integer.
from __future__ import annotations
from typing import Iterable
class IntSet:
"""
Efficiently store integers in a set - internally uses bit shift operations.
NOTE: len() isn't supported - instead cast to list first: len(list(intset))
>>> IntSet(0b10011) # Set of all the bits which are set to 1 (using 1-based bit indexing)
@sohang3112
sohang3112 / html_attributes.hs
Created August 22, 2024 05:59
Simple Haskell function to handle HTML attributes that may or may not have a value specified.
-- Simple function to handle HTML attributes that may or may not have a value specified.
import qualified Data.Map as Map
import Data.Map (Map)
-- HTML Property Key | Value
type Prop k v = Either k v
key = Left
val = Right
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sohang3112
sohang3112 / pandas_time_series_notes.ipynb
Created August 16, 2024 15:19
Example of working with Time Series in Pandas
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sohang3112
sohang3112 / wordcloud.py
Last active July 26, 2024 09:13
Draw wordcloud in Python
from collections import Counter
from io import BytesIO
import os
# Optional: If you already downloaded NLTK resources at a non-standard location
# Espcially useful in case of AWS Lambda - you can put already downloaded nltk resources in layer and use in AWS Lambda
# Here path = /opt because that's where AWS Lambda puts all Layer contents
# ALREADY_DOWNLOADED_NLTK_PATH = '/opt'
ALREADY_DOWNLOADED_NLTK_PATH = None # nltk resources not already downloaded, to download

Zeromq example in Haskell

Zeromq client & server example in Haskell using zeromq4-haskell package (a Haskell wrapper over libzmq).

NOTE: In 2023 my PR was merged that added this Haskell example to zeromq.org website. But it doesn't show yet on zeromq.org website because the site owner hasn't yet updated the site according to the latest changes in its Github repo. You can also see the below example client & server in the Zeromq Github repository.

Server

@sohang3112
sohang3112 / onedrive_linux_sync.md
Last active June 29, 2024 05:50
OneDrive sync in Linux

Sync OneDrive in Linux

Using the official OneDrive Linux client:

$ sudo dnf install -y onedrive               # or "apt install", etc. - depends on distro
$ onedrive                                   # gives a link, open it in browser to login to OneDrive
$ onedrive --synchronize                     # sync files to ~/OneDrive ; downloading files first time takes some time
$ systemctl enable onedrive@<username>.service --now        # sync automatically in background

Check more on its usage in the docs.

@sohang3112
sohang3112 / ollama_notes.md
Last active July 15, 2024 05:57
Notes on self-hosting Generative AI LLM models with Ollama

Ollama Notes

Ollama is an open-source tool that allows self-hosting Large Language Models (LLMs). Normally these LLMs are not feasible to deploy on consumer hardware, however Ollama optimizes the models by removing unused layers, rounding decimal points in weights to reduce model size, etc.

Note: Some details about the Ollama service are Linux-specific, but most things are same on all platforms.

In Linux, Ollama can be installed using the command curl -fsSL https://ollama.com/install.sh | sh.

@sohang3112
sohang3112 / openai_notes.py
Created May 13, 2024 12:05
Openai notes - GPT calling
import os
import openai
# Call GPT
os.environ['OPENAI_API_KEY'] = 'PUT-OPENAI-API-KEY-HERE'
client = openai.OpenAI()
prompt = "Say Hi!" # put instructions to GPT here
gpt_response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
@sohang3112
sohang3112 / video_audio_notes.py
Created May 8, 2024 06:46
Notes on processing & generation of video / audio in Python
# Create a silent MP3 audio
# Requires ffmpeg library, install with: sudo apt install -y ffmpeg
from pydub import AudioSegment
silence = AudioSegment.silent(duration=5000) # duration in milliseconds
silence.export("silence.mp3", format="mp3")
# Extract audio from video
import moviepy.editor
video = moviepy.editor.VideoFileClip("/path/to/video.mp4")
if video.audio is None: