From 41cf69a39c08f3eb58dffdf4af2bc733eab25a08 Mon Sep 17 00:00:00 2001 From: Aaron Lau Date: Tue, 23 Apr 2024 00:00:35 -0500 Subject: [PATCH] some backend ideas for flask Getting ideas for flask together, to see how we can make our api backend. --- .vscode/extensions.json | 5 ++++ app.py | 66 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 .vscode/extensions.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 000000000..8f5956f4a --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "ms-vscode.azure-account" + ] +} \ No newline at end of file diff --git a/app.py b/app.py index 3d1808cf6..e289743e3 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,36 @@ import os - +from transformers import pipeline from flask import (Flask, redirect, render_template, request, send_from_directory, url_for) +pipe_ingredients = pipeline("image-segmentation", model="prem-timsina/segformer-b0-finetuned-food") +pipe_name = pipeline("image-classification", model="nateraw/food") +from werkzeug.utils import secure_filename +import firebase_admin +from firebase_admin import credentials, storage + +# Initialize Firebase Admin SDK +cred = credentials.Certificate('path/to/serviceAccountKey.json') +firebase_admin.initialize_app(cred, { + 'storageBucket': 'your_project_id.appspot.com' +}) # TODO: update this function with appropriate credentials + +def upload_file_to_firebase(file_path, destination_blob_name): + try: + # Get a reference to the Firebase Storage bucket + bucket = storage.bucket() + + # Create a blob object representing the file to upload + blob = bucket.blob(destination_blob_name) + + # Upload the file to Firebase Storage + blob.upload_from_filename(file_path) + + print(f"File uploaded to Firebase Storage: {destination_blob_name}") + return True + except Exception as e: + print(f"Error uploading file to Firebase Storage: {e}") + return False + app = Flask(__name__) @@ -27,6 +56,41 @@ def hello(): print('Request for hello page received with no name or blank name -- redirecting') return redirect(url_for('index')) +ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'} +def allowed_file(filename): + return '.' in filename and \ + filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + + +@app.route('/upload', methods = ['PUT']) +def upload(): + if request.method == 'PUT': + if 'file' not in request.files: + return "No file selected" + for file in request.files(file.filename): + if file and allowed_file(file.filename): + filename = secure_filename(file.filename) + # save file + #return jsonify(result) # return in json format + + +@app.route('/echo', methods = ['GET', 'POST', 'PATCH', 'PUT', 'DELETE']) +def api_echo(): + if request.method == 'GET': + return "ECHO: GET\n" + + elif request.method == 'POST': + return "ECHO: POST\n" + + elif request.method == 'PATCH': + return "ECHO: PACTH\n" + + elif request.method == 'PUT': + return "ECHO: PUT\n" + + elif request.method == 'DELETE': + return "ECHO: DELETE" + if __name__ == '__main__': app.run()