Skip to content

Instantly share code, notes, and snippets.

View tarekziade's full-sized avatar

Tarek Ziade tarekziade

View GitHub Profile
@tarekziade
tarekziade / t5-distill.py
Created February 22, 2024 09:35
t5 distillation with bert-squeeze
from bert_squeeze.assistants import DistilAssistant
from lightning.pytorch import Trainer
config_assistant = {
"teacher_kwargs": {
"pretrained_model": "cnicu/t5-small-booksum",
},
"student_kwargs": {
"pretrained_model": "cnicu/t5-small-booksum",
import sys
import nltk
from nltk.corpus import wordnet as wn
nltk.download("wordnet")
def find_hypernyms(synset_name):
synset = wn.synset(synset_name)
hypernyms = synset.hypernyms()
@tarekziade
tarekziade / scrape.py
Created May 18, 2023 15:57
extract fully rendered web pages
"""
Demo of extracting text and links from a rendered web page.
$ brew install geckodriver
$ python3 -m venv .
$ bin/pip install bs4 selenium
$ bin/python scrap.py
The script looks for an element of a specific id on the page.
This can be used to make sure we wait for all JS to execute, and
@tarekziade
tarekziade / cancellable.py
Created December 2, 2022 20:36
Python asyncio cancellable sleeps
# taken from https://stackoverflow.com/a/37211337
def _make_sleep():
async def sleep(delay, result=None, *, loop=None):
coro = asyncio.sleep(delay, result=result)
task = asyncio.ensure_future(coro, loop=loop)
sleep.tasks.add(task)
try:
return await task
except asyncio.CancelledError:
return result
@tarekziade
tarekziade / concurrent.py
Last active November 30, 2022 01:49
Run corountines in parallel with a maximum concurrency
class ConcurrentRunner:
def __init__(self, max_concurrency=5):
self.max_concurrency = 5
self.tasks = []
async def put(self, coro):
"""Starts a coroutine if there are 4 or less already running ones.
"""
# blocks until there's room
while len(self.tasks) >= self.max_concurrency:
@tarekziade
tarekziade / mutation.js
Created March 21, 2018 10:05
Demonstrates how to detect when a button gets enabled
/* Like for the IntersectionObserver, we're using here the
* Mutation observer to detect when an element attribute is changed
* by some Javascript on the page.
*
* use case: a button is enabled
*/
function watchMutation(selector, condition) {
var target = document.querySelector(selector);
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
import functools
import logging
import graypy
import json
import time
import random
from collections import defaultdict, deque
from flask import Flask, jsonify, g
app = Flask(__name__)
@tarekziade
tarekziade / marionette.py
Last active April 8, 2017 13:01
async marionette calls
# see https://dxr.mozilla.org/mozilla-central/source/testing/marionette/driver.js?from=testing%2Fmarionette%2Fdriver.js#2957
import functools
import json
from asyncio import open_connection
import asyncio
from molotov import *
class Marionette(object):
def __init__(self, host='localhost', port=2828, loop=None):
@tarekziade
tarekziade / grum.py
Last active April 5, 2017 14:24
poutine test
import asyncio
import configparser
import pytest
from smwogger import API
@pytest.fixture
def conf():
config = configparser.ConfigParser()
config.read('manifest.ini')
@tarekziade
tarekziade / call_hb.py
Created April 4, 2017 14:00
Smowgger Example
import asyncio
from smwogger import API
async def run_heartbeat():
async with API('__api__.json') as api:
res = await api.__heartbeat__()
body = await res.json()
print(body)