<jupyter_start><jupyter_text>Hit the fossilnet API You will have to be running the flask server for this to work.<jupyter_code>urls = ["https://www.bgs.ac.uk/discoveringGeology/time/Fossilfocus/images/foraminifera/Elphidium.jpg",
        "https://earthsky.org/upl/2016/03/fish-fossil-green-river.jpg",
        "https://assets3.fossilera.com/sp2/150226/diplomystus/620x400/diplomystus-dentatus.jpg",
       ]<jupyter_output><empty_output><jupyter_text>GET<jupyter_code>api = "http://127.0.0.1:5000/predict"<jupyter_output><empty_output><jupyter_text>POST with URL<jupyter_code>api = "http://127.0.0.1:5000/post"
headers = {'contentType': "application/json"}

json = {'url': urls[2]}

r = requests.post(api, json=json, headers=headers)<jupyter_output><empty_output><jupyter_text>POST with base64-encoded image First we fetch the image data (a bunch of bytes) from its URL, then we encode that<jupyter_code>api = "http://127.0.0.1:5000/api/v0.1"
import base64
import requests

r = requests.get(urls[2])
img_b64 = base64.b64encode(r.content).decode('utf-8')<jupyter_output><empty_output><jupyter_text>Now we can make a POST request to our server.<jupyter_code>headers = {'Accept': 'application/json'}

json = {'image': img_b64}

r = requests.post(api, json=json, headers=headers)
r<jupyter_output><empty_output><jupyter_text>Making a b64 string from a disk imageHow can we do that with a disk image?<jupyter_code>from PIL import Image

img = Image.open('images/42485-116.jpg')
img
import base64
from io import BytesIO

handle = BytesIO()
img.save(handle, format='png')
handle.seek(0)
img_b64 = base64.b64encode(handle.getvalue()).decode('utf-8')<jupyter_output><empty_output><jupyter_text>We can actually read the bytes straight from the file, without `pillow`.<jupyter_code>with open('images/42485-116.jpg', 'rb') as f:
    img_b64 = base64.b64encode(f.read()).decode('utf-8')<jupyter_output><empty_output><jupyter_text>Now we can submit to the server:<jupyter_code>import requests

json = {'image': img_b64}

r = requests.post(api, json=json, headers=headers)
r.json()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14456106870229007, 'fraction_numerical': 0.04961832061068702, 'mean_word_length': 6.256055363321799, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '351448', 'license': 'Apache-2.0', 'n_tokens_mistral': 845, 'n_tokens_neox': 787, 'n_words': 149, 'path': 'notebooks/Hitting_our_web_API.ipynb', 'repo_name': 'jalasami/Git-test', 'size': '2096'}