-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bad38d
commit a1d278d
Showing
3 changed files
with
24 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
""" | ||
Function must have `huggingfaceToken.json` to get access to ml model | ||
- this is basically an async function that returns the predicted label of the | ||
food image that is inputted. | ||
- images must be located physically | ||
""" | ||
from flask import Flask, request | ||
|
||
app = Flask(__name__) | ||
|
||
import aiohttp | ||
import json | ||
import asyncio | ||
|
||
token = json.load(open("ml/huggingfaceToken.json"))['token'] | ||
token = json.load(open("ml/huggingfaceToken.json"))["token"] | ||
|
||
API_URL = "https://api-inference.huggingface.co/models/nateraw/food" | ||
headers = {"Authorization": f"Bearer {token}"} | ||
|
||
|
||
@app.route("/predict/<filename>", methods=["GET"]) | ||
async def query(filename): | ||
with open(filename, "rb") as f: | ||
data = f.read() | ||
try: | ||
with open(filename, "rb") as f: | ||
data = f.read() | ||
|
||
async with aiohttp.ClientSession() as session: | ||
async with session.post(API_URL, headers=headers, data=data) as response: | ||
return await response.json() | ||
|
||
async with aiohttp.ClientSession() as session: | ||
async with session.post(API_URL, headers=headers, data=data) as response: | ||
return await response.json() | ||
except FileNotFoundError: | ||
return "File not found", 404 | ||
|
||
|
||
async def main(): | ||
# This part is used for testing the function separately. | ||
output = await query("ml/pizza.webp") | ||
print(output) | ||
|
||
|
||
if __name__ == "__main__": | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main()) | ||
|
||
if __name__ == "__main__": | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,4 @@ const main = async (): Promise<void> => { | |
} | ||
}; | ||
|
||
main(); | ||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from flask import Flask | ||
app = Flask(__name__) | ||
@app.route('/') | ||
def hello_world(): | ||
return 'Hello world!' |