Request in flask API deployed in Cloud Run doesn't work - flask

I have an application in Flask that should receive an image per POST request. The request works with the server running locally, but it does not work on my API implemented by Cloud Run :(
I would appreciate any pointers!
Regards, Helena.
#app.route("/predict", methods=["GET", "POST"])
def predict():
# initialize the data dictionary that will be returned from the view
data = {"success": False}
# ensure an image was properly uploaded to our endpoint
if flask.request.method == 'POST':
_file = flask.request.files['image']
if _file:
# read the image in PIL format
thermal_image = flask.request.files['image'].read()
# get thermal region on embedded image
try:
image = get_cropped_embedded(thermal_image)
#print(image)
except Exception as e:
print(e)
data = {"Success": 'Be positive!'}
return flask.jsonify(data)
################ More code
data.update({
"path": path,
"success": True
})
else:
logging.info("No hot point found out. Exitting without classification.")
return flask.jsonify(data)
# return the data dictionary as a JSON response
return flask.jsonify(data)
if __name__ == "__main__":
app.run('0.0.0.0', port=5000)
desired result
result

Related

Vertex AI batch prediction results in ('ValueError', 1)

I have a predictor that works successfully when run on a local container, but when that container is pushed to the Artifact Registry, and imported to a Vertex AI model, and run in Batch Prediction mode, it returns an empty prediction file and an error file that simply says ('ValueError', 1)
The request is made using Flask.
code (main.py) is
if 'AIP_HEALTH_ROUTE' in os.environ:
AIP_HEALTH_ROUTE = os.environ['AIP_HEALTH_ROUTE']
else:
AIP_HEALTH_ROUTE = '/health'
if 'AIP_PREDICT_ROUTE' in os.environ:
AIP_PREDICT_ROUTE = os.environ['AIP_PREDICT_ROUTE']
else:
AIP_PREDICT_ROUTE = '/predict'
#app.route(AIP_HEALTH_ROUTE, methods=['GET'])
def health():
response = {"message": "OK", "code": "SUCCESS"}
return make_response(jsonify(response), 200)
#app.route(AIP_PREDICT_ROUTE, methods=['POST'])
def predict():
try:
instances = pd.DataFrame(request.json.get("instances"))
# creates a model using model and encoders stored at given GCS location
model = model_active.CustomPredictor('datascience-modelartifacts/my-model/actives')
processed_data = model.process_score_data(instances)
predictions = model.predict(processed_data)
response = {"predictions": predictions}
status = 200
except Exception as e:
response = {"error": str(e)}
status = 500
return make_response(jsonify(response), status)
For local testing, the data looks like:
dat = {"instances": [
{"F1": 1000854828492, "F2": 3000076437878, "F3": 19.99, ...},
{"F1": 1000222326963, "F2": 3000127917915, "F3": 19.99, ...},
... ]}
I call this with
import requests
url = 'http://localhost:5000/predict'
headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
r = requests.post(url, json=dat, headers=headers)
and in this case everything works as expected (I get a list of predicted classes returned).
I can successfully deploy this model as an endpoint and provide this data as test and get predictions returned.
However, for Vertex batch prediction (which is what I ultimately want to enable since we only need this model once a month), I have tried providing this data as both JSONL and CSV and I keep receiving output with no successful predictions and an error file that simply says ('ValueError', 1)
Does anyone have any suggestions what this error means and what is causing it?

Dajngo CSV FIle not download ? When we have a large CSV file download its takes some time?Django 502 bad gateway nginx error Django

How can I download a large CSV file that shows me a 502 bad gateway error?
I get this solution I added in below.
Actually, in this, we use streaming references. In this concept for example we download a movie it's will download in the browser and show status when complete this will give the option to show in a folder same as that CSV file download completely this will show us.
There is one solution for resolving this error to increase nginx time but this is will affect cost so better way to use Django streaming. streaming is like an example when we add a movie for download it's downloading on the browser. This concept is used in Django streaming.
Write View for this in Django.
views.py
from django.http import StreamingHttpResponse
503_ERROR = 'something went wrong.'
DASHBOARD_URL = 'path'
def get_headers():
return ['field1', 'field2', 'field3']
def get_data(item):
return {
'field1': item.field1,
'field2': item.field2,
'field3': item.field3,
}
class CSVBuffer(object):
def write(self, value):
return value
class Streaming_CSV(generic.View):
model = Model_name
def get(self, request, *args, **kwargs):
try:
queryset = self.model.objects.filter(is_draft=False)
response = StreamingHttpResponse(streaming_content=(iter_items(queryset, CSVBuffer())), content_type='text/csv', )
file_name = 'Experience_data_%s' % (str(datetime.datetime.now()))
response['Content-Disposition'] = 'attachment;filename=%s.csv' % (file_name)
except Exception as e:
print(e)
messages.error(request, ERROR_503)
return redirect(DASHBOARD_URL)
return response
urls.py
path('streaming-csv/',views.Streaming_CSV.as_view(),name = 'streaming-csv')
For reference use the below links.
https://docs.djangoproject.com/en/4.0/howto/outputting-csv/#streaming-large-csv-files
GIT.
https://gist.github.com/niuware/ba19bbc0169039e89326e1599dba3a87
GIT
Adding rows manually to StreamingHttpResponse (Django)

Process received data from python requests library to show it in template

i am try to get live data from Raspberry pi and showing it to my django template. i could send data by 'requests' library and receiv it by Django REST. but the problem is in my django for example i post data to localhost/a how could i can render this data to localhost/b for example.
i tried django session. i thought that when i in localhost/b maybe i can use session but that dose not work.
rpi-req.py
# this is code from Raspberry pi, to send(POST) data to django
while True:
# print(cam.get_frame())
da = {'rpiLive': "30 or any data"}
res = requests.post(url, files=da, timeout=1000, auth=('anas', '****'))
print(res.status_code)
time.sleep(1)
views.py inside django.
# this code is from django-views, to receive data
#api_view(['POST'])
def video_feed(request):
if request.method == 'POST':
reqData = request.FILES['rpiLive']
try:
return HttpResponse(gen22(request, reqData),
content_type='multipart/x-mixed-replace; boundary=reqData')
except Exception as q:
print("live Data aborted!!!", q)
return HttpResponseRedirect('/live2/')
return HttpResponseRedirect('/')
And gen22 function is:
def gen22(request, data):
a = data
time.sleep(0.5)
con = {
"anas": a,
"con_tag": "live",
}
time.sleep(1)
return render(request, "dahsboard.html", con)
urls.py
path('<int:user_id>/<tag>/', views.dahsboard, name='dahsboard'),
path('live/', views.video_feed, name='live-frame2'),

Django rest framework API calling to another API [duplicate]

Is there any way to make a RESTful api call from django view?
I am trying to pass header and parameters along a url from the django views. I am googling from half an hour but could not find anything interesting.
Any help would be appreciated
Yes of course there is. You could use urllib2.urlopen but I prefer requests.
import requests
def my_django_view(request):
if request.method == 'POST':
r = requests.post('https://www.somedomain.com/some/url/save', params=request.POST)
else:
r = requests.get('https://www.somedomain.com/some/url/save', params=request.GET)
if r.status_code == 200:
return HttpResponse('Yay, it worked')
return HttpResponse('Could not save data')
The requests library is a very simple API over the top of urllib3, everything you need to know about making a request using it can be found here.
Yes i am posting my source code it may help you
import requests
def my_django_view(request):
url = "https://test"
header = {
"Content-Type":"application/json",
"X-Client-Id":"6786787678f7dd8we77e787",
"X-Client-Secret":"96777676767585",
}
payload = {
"subscriptionId" :"3456745",
"planId" : "check",
"returnUrl": "https://www.linkedin.com/in/itsharshyadav/"
}
result = requests.post(url, data=json.dumps(payload), headers=header)
if result.status_code == 200:
return HttpResponse('Successful')
return HttpResponse('Something went wrong')
In case of Get API
import requests
def my_django_view(request):
url = "https://test"
header = {
"Content-Type":"application/json",
"X-Client-Id":"6786787678f7dd8we77e787",
"X-Client-Secret":"96777676767585",
}
result = requests.get(url,headers=header)
if result.status_code == 200:
return HttpResponse('Successful')
return HttpResponse('Something went wrong')
## POST Data To Django Server using python script ##
def sendDataToServer(server_url, people_count,store_id, brand_id, time_slot, footfall_time):
try:
result = requests.post(url="url", data={"people_count": people_count, "store_id": store_id, "brand_id": brand_id,"time_slot": time_slot, "footfall_time": footfall_time})
print(result)
lJsonResult = result.json()
if lJsonResult['ResponseCode'] == 200:
print("Data Send")
info("Data Sent to the server successfully: ")
except Exception as e:
print("Failed to send json to server....", e)

Why i am getting 400 Bad Request error when sending json data in Flask?

I am trying to write a small restful api application, i am using Chrome Postman extension for sending requests to the app .
I believe that my code does not have mistakes but every time i am sending post request a 400 Bad Request error raising , here is my code:
#api_route.route('/api', methods=['GET'])
def api():
return jsonify({'message':'Api v1.0'})
#api_route.route('/api', methods=['POST'])
def create_user():
data = request.get_json()
if data:
hashed_password = generate_password_hash(data['password'], method='sha256')
api = Api(email=data['email'], password=hashed_password)
db.session.add(api)
db.session.commit()
return jsonify({'message', 'New User Created!'})
The json data that i am sending looks like this:
{"email" : "Test", "password" : "123123123"}
Why i am getting the 400 error ??
Update:
Screenshots for the requests using Postman:
GET Request
POST Request
Here i am initiating api route inside api controller :
from flask import Blueprint
api_route = Blueprint(
'api',
__name__
)
from . import views
then i am registering it inside def create_app() function :
from .api import api_route
app.register_blueprint(api_route)
Here are the extensions that i am using in my application:
toolbar = DebugToolbarExtension()
assets_env = Environment()
cache = Cache()
moment = Moment()
htmlminify = HTMLMIN()
csrf = CSRFProtect()
jac = JAC()
googlemap = GoogleMaps()
session = Session()
principal = Principal()
I solved the problem, i've initiated CSRFProtect with app so i need to include X-CSRFToken in all my requests, so i have two choices:
1 - To include the csrf_token in request.headers for all the requests
2 - Using #csrf.exempt decorator that coming with flask_wtf.csrf
For now i am using #csrf.exempt, so it become like this:
#api_route.route('/api', methods=['GET','POST'])
#csrf.exempt
def create_user():
if request.method == 'GET':
return jsonify({'message' : 'API v1.0'})
elif request.method == 'POST':
data = request.get_json()
hashed_password = generate_password_hash(data['password'], method='sha256')
new_user_api = Api(email=data['email'], password=hashed_password)
db.session.add(new_user_api)
db.session.commit()
return jsonify({'message' : 'New user created!'})
return return jsonify({'message' : 'No user has been added!'})
Thanks for #MrPyCharm for his interests , salute :) .
A good approach would be to structure your views as follows:
Instead of creating view with same route for different request methods, you can handle the request methods in the same view:
#api_route.route('/api', methods=['GET', 'POST'])
def api():
if request.method == 'GET':
return jsonify({'message':'Api v1.0'})
else:
data = request.get_json(force=True)
if data:
hashed_password = generate_password_hash(data['password'], method='sha256')
api = Api(email=data['email'], password=hashed_password)
db.session.add(api)
db.session.commit()
return jsonify({'message': 'New User Created!'})
# Just in case the if condition didn't satisfy
return None
A note for anyone else experiencing this with PostMan and Flask - you will also hit a HTTP 404 if your URL in PostMan is HTTPS but your Flask app only handles HTTP.