Skip to content

Instantly share code, notes, and snippets.

View dhuynh95's full-sized avatar

DanyWin dhuynh95

View GitHub Profile
@dhuynh95
dhuynh95 / page_summarizer.py
Created May 19, 2024 16:22
Summarize the content of an HTML page using Llama Index
from llama_index.core import Document, VectorStoreIndex
from llama_index.core import Settings
import trafilatura
class PageSummarizer:
def __init__(self, llm, embed_model):
self.llm = llm
self.embed_model = embed_model
def summarize(self, html: str) -> str:
@dhuynh95
dhuynh95 / iou_lavague.py
Created May 1, 2024 14:27
LaVague evaluation functions with Intersection over Union
def compute_box(element):
location = element.location
size = element.size
# Coordinates
x_coordinate = location['x']
y_coordinate = location['y']
# Size
width = size['width']
from playwright.async_api import async_playwright
# Example on how to call these async functions using asyncio
import asyncio
async def load():
# Example where we connect to a local existing Chrome session in debug mode
playwright = await async_playwright().start()
browser = await playwright.chromium.connect_over_cdp("http://localhost:9222")
default_context = browser.contexts[0]
@dhuynh95
dhuynh95 / preprocess_gpt2.py
Last active September 1, 2022 10:44
Preprocessing for GPT2 text generation.
# Inspired from https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/python/tools/transformers/notebooks/Inference_GPT2_with_OnnxRuntime_on_CPU.ipynb
def get_example_inputs(example, tokenizer):
tokenizer.padding_side = "left"
tokenizer.pad_token = tokenizer.eos_token
max_length = 64
num_attention_heads, hidden_size, num_layer = 12, 768, 12
encodings_dict = tokenizer.batch_encode_plus(example, padding='max_length', max_length=max_length)
@dhuynh95
dhuynh95 / encoding_decoding_ckks.ipynb
Created June 22, 2020 08:57
Encoding and decoding in CKKS
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dhuynh95
dhuynh95 / encoding_decoding_ckks_vanilla.ipynb
Last active June 22, 2020 08:45
Vanilla encoding and decoding for Homomorphic Encryption
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dhuynh95
dhuynh95 / Markdium-python.py
Created June 19, 2020 09:29
Markdium-Introduction to encoding in CKKS
@patch_to(CKKSEncoder)
def pi(self, z: np.array) -> np.array:
"""Projects a vector of H into C^{N/2}."""
N = self.M // 4
return z[:N]
@patch_to(CKKSEncoder)
def pi_inverse(self, z: np.array) -> np.array:
"""Expands a vector of C^{N/2} by expanding it with its
@dhuynh95
dhuynh95 / Markdium-python.py
Created June 19, 2020 09:29
Markdium-Introduction to encoding in CKKS
@patch_to(CKKSEncoder)
def compute_basis_coordinates(self, z):
"""Computes the coordinates of a vector with respect to the orthogonal lattice basis."""
output = np.array([np.real(np.vdot(z, b) / np.vdot(b,b)) for b in self.sigma_R_basis])
return output
def round_coordinates(coordinates):
"""Gives the integral rest."""
coordinates = coordinates - np.floor(coordinates)
return coordinates
@dhuynh95
dhuynh95 / Markdium-python.py
Created June 19, 2020 09:29
Markdium-Introduction to encoding in CKKS
p1 = encoder.sigma_inverse(m1)
p2 = encoder.sigma_inverse(m2)
@dhuynh95
dhuynh95 / Markdium-python.py
Created June 19, 2020 09:29
Markdium-Introduction to encoding in CKKS
b_reconstructed = encoder.sigma(p)
b_reconstructed