Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mimetype to file data sent to model #39

Merged
merged 3 commits into from
Aug 16, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import collections
import json
import logging
import mimetypes
import os
import requests
import signal
Expand Down Expand Up @@ -120,7 +121,8 @@ def valid_file_ext(filename):

# Runs ML on given image
def run_ml(img_path):
img_file = {'image': open(img_path, 'rb')}
mime_type = mimetypes.guess_type(img_path)[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested what happens when no mime-type can be guessed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested with the three valid extensions, any other extension would be rejected before getting to this line. Given this library guesses mime type by the file extension (IIUC), that should be enough. I originally used magic to do this (more robust, not a guess), but it requires installs outside pip, which I didn't want to add.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading the docs, it's fine to assume [0] here. cool.

https://docs.python.org/2/library/mimetypes.html#mimetypes.guess_type

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I looked into that to make sure and the return is always a "pair"

img_file = {'image': (img_path, open(img_path, 'rb'), mime_type)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken the file handle returned by open is never closed ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, odd that no one ever caught that before, not that it matter much given this file is opened in a thread. I'll close it.

r = requests.post(url=ml_endpoint, files=img_file)
cap_json = r.json()
caption = cap_json['predictions']
Expand Down