Skip to content

Instantly share code, notes, and snippets.

View mahmoudajawad's full-sized avatar
👨‍💻
Building awesome stuff!

Mahmoud Abduljawad mahmoudajawad

👨‍💻
Building awesome stuff!
View GitHub Profile
@mahmoudajawad
mahmoudajawad / parse_multipart.py
Last active May 23, 2018 12:36
Alternative parse_multipart function (to replace python cgi default implementation) that can read additional attributes.
def parse_multipart(rfile, boundary):
boundary = b'--' + boundary
rfile = re.compile(boundary+b'(?:\r\n|\n)').split(rfile.replace(b'\r\n'+boundary+b'--', b'').replace(b'\n'+boundary+b'--', b''))
pattern = b'Content-Disposition: form-data; name="?([\$a-zA-Z0-9\.\-_\\\:]+)"?(?:; filename="?([a-zA-Z0-9\.\-_]+)"?(?:\r\n|\n)Content-Type: ([a-zA-Z0-9\.\-_]+\/[a-zA-Z0-9\.\-_]+)(?:\r\n|\n)|(?:\r\n|\n))(?:\r\n|\n)(.+)'
multipart = {}
for part in rfile:
try:
multipart_key = re.match(pattern, part, re.DOTALL).group(1)
multipart[multipart_key] = [group for group in re.match(pattern, part, re.DOTALL).groups()]
if multipart[multipart_key][3][-2] == 13 and multipart[multipart_key][3][-1] == 10:
@mahmoudajawad
mahmoudajawad / class_singleton.py
Last active April 1, 2018 05:47
Python (3.x) singleton metaclass. It allows manipulation of child classes, as well as converting all class funcs to classmethod's.
class ClassSingleton(type):
def __new__(cls, cls_name, bases, attrs):
for name, attr in attrs.items():
if callable(attr):
attrs[name] = classmethod(attr)
return type.__new__(cls, cls_name, bases, attrs)
def __init__(cls, cls_name, bases, attrs):
cls.singleton()
@mahmoudajawad
mahmoudajawad / update_package.tns-ng10.js
Last active September 19, 2020 18:36
Node script to update NativeScript+Angular projects to Angular 10
/*
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
@mahmoudajawad
mahmoudajawad / update_package.ns-ng11.js
Created December 23, 2020 08:22
Node script to update NativeScript+Angular projects to Angular 11
/*
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
@mahmoudajawad
mahmoudajawad / falcon_40b_instruct_chat.py
Created June 18, 2023 15:55
Run falcon-40b-instruct in conversational mode
import torch
import transformers
from transformers import AutoTokenizer, AutoModelForCausalLM
model = "tiiuae/falcon-40b-instruct"
tokenizer = AutoTokenizer.from_pretrained(model, padding_side="left")
pipeline = transformers.pipeline(