Skip to content

Commit

Permalink
added flask for python api
Browse files Browse the repository at this point in the history
  • Loading branch information
LordFarquaadtheCreator committed Oct 22, 2023
1 parent 6bad38d commit a1d278d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
30 changes: 18 additions & 12 deletions ml/foodClassifier.py
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()
2 changes: 1 addition & 1 deletion ml/foodClassifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ const main = async (): Promise<void> => {
}
};

main();
main();
5 changes: 5 additions & 0 deletions ml/hello.py
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!'

0 comments on commit a1d278d

Please sign in to comment.