Skip to content

Instantly share code, notes, and snippets.

@moohax
Created August 7, 2021 03:19
Show Gist options
  • Save moohax/76212382654b53ec51dacff4b9b2991b to your computer and use it in GitHub Desktop.
Save moohax/76212382654b53ec51dacff4b9b2991b to your computer and use it in GitHub Desktop.
# Generated by counterfit #
import requests
import base64
from counterfit.core.targets import ArtTarget
class Twitter(ArtTarget):
model_name = "twitter"
model_data_type = "image"
model_endpoint = "http://0.0.0.0:9000/_dash-update-component"
model_input_shape = (1, )
model_output_classes = [0, 1]
X = []
def __init__(self):
image = self.load_image("download.jpg")
self.X = [image]
def load_image(self, img_path):
with open(img_path, "rb") as fp:
img_content = base64.b64encode(fp.read())
img_content = "data:image/jpg;base64,{}".format(
img_content.decode("utf-8").replace("\n", ""))
return img_content
def upload_image(self, image):
headers = {
"Accept": "application/json",
"X-CSRFToken": "undefined",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62",
"Content-Type": "application/json",
"Origin": "http://0.0.0.0:9000",
"Referer": "http://0.0.0.0:9000/",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.9,pt;q=0.8",
"Connection": "close"
}
image = {
"changedPropIds": ["upload-image.contents"],
"inputs": [{
"id": "upload-image", "property": "contents",
"value": [f"{image}"]}],
"output": "output-image-upload.children",
"outputs": {"id": "output-image-upload", "property": "children"},
"state": [{"id": "upload-image", "property": "filename", "value": ["download.jpg"]}, {"id": "upload-image", "property": "last_modified", "value": [1.628300834743E9]}]}
resp = requests.post(self.model_endpoint,
headers=headers, json=image)
return resp.json()
def __call__(self, x):
for sample in x:
# do something with data
resp = self.upload_image(sample)
# do something with the response
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment