'how to send request on my flask app google colab
How do I send a request from localhost to my google colab app? I want to test the functions of deepface api, because my computer is so bad for this, but i have a CORS trouble... I still tested this code on my computer, the error with CORS was also present, but the code below helped fix this problem
import argparse
import time
import uuid
import tensorflow
from flask import Flask, jsonify, request
from flask_cors import CORS, cross_origin
from flask_ngrok import run_with_ngrok
from deepface import DeepFace
tf_version = int(tensorflow.__version__.split(".")[0])
if tf_version == 2:
import logging
tensorflow.get_logger().setLevel(logging.ERROR)
if tf_version == 1:
graph = tensorflow.get_default_graph()
app = Flask(__name__)
run_with_ngrok(app)
# Service API Interface
@app.route('/')
def index():
return '<h1>Hello, world!</h1>'
@app.route('/analyze', methods=['POST'])
def analyze():
global graph
tic = time.time()
req = request.get_json()
trx_id = uuid.uuid4()
print(req)
if tf_version == 1:
with graph.as_default():
resp_obj = analyzeWrapper(req, trx_id)
elif tf_version == 2:
resp_obj = analyzeWrapper(req, trx_id)
toc = time.time()
resp_obj["trx_id"] = trx_id
resp_obj["seconds"] = toc-tic
return resp_obj, 200
def analyzeWrapper(req, trx_id=0):
resp_obj = jsonify({'success': False})
print(req)
instances = []
if "img" in list(req.keys()):
raw_content = req["img"] # list
for item in raw_content: # item is in type of dict
instances.append(item)
if len(instances) == 0:
return jsonify({'success': False, 'error': 'you must pass at least one img object in your request'}), 205
print("Analyzing ", len(instances), " instances")
detector_backend = 'opencv'
actions = ['emotion', 'age', 'gender', 'race']
if "actions" in list(req.keys()):
actions = req["actions"]
if "detector_backend" in list(req.keys()):
detector_backend = req["detector_backend"]
try:
resp_obj = DeepFace.analyze(instances, actions=actions)
except Exception as err:
print("Exception: ", str(err))
return jsonify({'success': False, 'error': str(err)}), 205
return resp_obj
if __name__ == '__main__':
app.run()
I'm trying to do this:
app = Flask(__name__)
CORS(app, support_credentials=True)
run_with_ngrok(app)
and this:
def _corsify_actual_response(response):
response.headers.add("Access-Control-Allow-Origin", "http://localhost:8080")
return response
and a few more options, but none helped:( Pls help
P.S. my JS post:
axios
.post('http://b3a6-35-237-77-168.ngrok.io/analyze', {})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
