Skip to content

Instantly share code, notes, and snippets.

import boto3
from PIL import Image
import io
rekognition_client = boto3.client("rekognition")
def get_face_details(image_bytes):
response = rekognition_client.detect_faces(Image={"Bytes": image_bytes}, Attributes=["ALL"])
# Assuming the first face is the one we need
face_details = response["FaceDetails"][0] if response["FaceDetails"] else None
@arihantdaga
arihantdaga / avoid_branching.js
Last active January 2, 2023 10:37
Avoiding branching
// Implementation 1
async function makeRequest({
method = RequestMethod.GET,
path,
body = {},
tokenStore,
isLoginRequest = false,
}) {
// We can avoid this branching by using implementation 2
@arihantdaga
arihantdaga / set_intersection.js
Created September 26, 2022 12:50
Intersection of sets
function getIntersection(setA, setB) {
const intersection = new Set(
[...setA].filter(element => setB.has(element))
);
return intersection;
}
// const set1 = new Set(['a', 'b', 'c']);
// const set2 = new Set(['a', 'b', 'd', 'e']);
@arihantdaga
arihantdaga / javascript
Created April 26, 2022 08:10
Getting API response with minimum latency
Given an API - /all_data which returns all the schools, all the students and all the books from DB. And return the reponse with minimum possible latency.
And if any of the query fails, returns balnk array for only that entity.
Example -
router.get("/all_data", async (req,res,next)=>{
let schools = await School.find(); // takes 300ms to complete
let students = await Student.find(); // takes 300ms to complete
let books = await Book.find(); // takes 300ms to complete
const char *wifiSSID = "MY_WIFI";
const char *wifiPassword = "MY_PASS";
#define MQTT_HOST "mybroker"
#deinfe MQTT_PORT 1883
#include <ESP8266WiFi.h>
WiFiClient wifiClient;
#include <AsyncMqttClient.h>
AsyncMqttClient mqttClient;
# base Image
FROM python:3.6-stretch
# install build utilities
RUN apt-get update && \
apt-get install -y gcc make apt-transport-https ca-certificates build-essential git
#Install Pytorch and dependencies
RUN pip3 install torch==1.7.1+cpu torchvision==0.8.2+cpu torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
from flask import Flask
from flask import request
app = Flask(__name__)
from mood_tagging import get_emotion
@app.route('/get_emotion', methods=['POST'])
def get_emotion_of_text():
if request.method == 'POST':
text = request.json.get('text')
# Bunch of setting up Steps
pip install transformers[sentencepiece]
pip install sentencepiece
import os
# Load the Pretrained Model
from transformers import AutoTokenizer, AutoModelWithLMHead, PreTrainedModel
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-emotion", use_fast=False)
model = AutoModelWithLMHead.from_pretrained("mrm8488/t5-base-finetuned-emotion")
@arihantdaga
arihantdaga / return-promise.js
Created October 22, 2020 17:29
Return or Not Return after resolving/rejecting a promise
let p = new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve("Hello");
console.log("Will I be printed");
}, 2000);
});
p.then(data=>{
console.log(data);
}).catch(err=>{
@arihantdaga
arihantdaga / NodejsRaceAndLockDemo.js
Created August 23, 2020 16:41
A simple demonstration of how race conditions can occur in nodeJs and also how to tackle that
let _arr = [];
let count = 0;
class Lock {
constructor() {
this._locked = false;
this._waiting = [];
}