Skip to content

Instantly share code, notes, and snippets.

@dcbark01
dcbark01 / listdirs.py
Created December 10, 2021 16:14
listdirs for all files of specific type (walking through all subdirectories)
import os
def listdirs(path='./', filetype='.*'):
""" List all files in directory. """
output = []
for root, dirs, files in os.walk(path):
for file in files:
if filetype in ['.*', '*']:
@dcbark01
dcbark01 / pdf2img.py
Created December 10, 2021 16:16
Create an image from a PDF file
from pdf2image import convert_from_path
from PyPDF2 import PdfFileReader
def pdf_to_image(file: str, page_num=0, output_file=None) -> Image:
""" Convert a PDF to a PIL Image. """
pdf_page = PdfFileReader(open(file, 'rb')).getPage(page_num)
pdf_shape = pdf_page.mediaBox
pdf_height = pdf_shape[3] - pdf_shape[1]
pdf_width = pdf_shape[2] - pdf_shape[0]

A simple Docker and Docker Compose install script for Ubuntu

Usage

  1. sh install-docker.sh
  2. log out
  3. log back in

Links

@dcbark01
dcbark01 / pisano_fibonacci_numbers.py
Created August 9, 2022 15:43
Pisano Period for Fibonacci Numbers
# Uses python3
import sys
from typing import List
def calc_fib(n):
if n == 1 or n == 2:
return 1
elif n == 0:
return 0
@dcbark01
dcbark01 / fractional_knapsack.ipynb
Created August 15, 2022 16:44
Fractional Knapsack
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dcbark01
dcbark01 / listdirs_new.py
Created October 29, 2022 16:17
New listdirs
import os
from typing import List, Union
def listdirs(path, extensions: Union[List[str], str] = None):
""" List all files in directory (including walking all subdirectories).
Can filter by file extension by providing either, for example:
extensions='png'
extensions=['png', 'jpeg']
@dcbark01
dcbark01 / auto_huggingface_textgen.py
Last active October 28, 2023 01:41
AutoHuggingFaceTextGenInference for Langchain PR
# TODO: Add this to PR for Langchain so that it will be easy to use across all our different LLM projects
import re
import time
import warnings
from pathlib import Path
from typing import List, Union, Optional
import requests
from tqdm import tqdm
from pydantic import BaseModel, Field, field_validator, computed_field
@dcbark01
dcbark01 / app.py
Last active September 21, 2024 10:23
Embed Mistral FastAPI
"""
# See https://huggingface.co/intfloat/e5-mistral-7b-instruct for model inference code
## Quickstart
Install requirements
```bash
pip install fastapi uvicorn torch transformers
```
@dcbark01
dcbark01 / langchain_save_jsonl.py
Created March 20, 2024 13:19
Save Langchain Documents to JSONL
import typing as t
import jsonlines
from langchain.schema import Document
def save_docs_to_jsonl(documents: t.Iterable[Document], file_path: str) -> None:
with jsonlines.open(file_path, mode="w") as writer:
for doc in documents:
writer.write(doc.dict())
@dcbark01
dcbark01 / file_size.sh
Last active December 16, 2024 22:41
File/dir size
# For directory
du -h ./ | sort -hr | head -n 10
# For the largest files within a given directory and its subdirectories
find ./ -type f -exec du -h {} + | sort -hr | head -n 10
# Find venv/.venv folders (limit recursive lookup to 4)
find ~/ -maxdepth 4 -type d \( -name ".venv" -o -name "venv" \) 2>/dev/null