-1

I am trying to upload an food image and trying to detect the picture using clarifai food detection model which is in JSON format and display results in my html page I am taking sample code from here

from flask import Flask,render_template,request,redirect,url_for ,session
from PIL import Image
import pandas as pandas
from werkzeug.utils import secure_filename
from clarifai_grpc.grpc import api
from clarifai_grpc.grpc.api import service_pb2, resources_pb2
from clarifai_grpc.grpc.api.status import status_code_pb2
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import service_pb2_grpc
stub = service_pb2_grpc. V2Stub( ClarifaiChannel.get_grpc_channel())

app=Flask(__name__,template_folder='templates',static_folder='static')
app.secret_key='a'


@app.route('/',methods=['POST','GET'])
def food():
    metadata = (('authorization', 'Key {XXXXXXX}'))
    if request.method =='POST' :
        file = request.files['Image']
        image = Image.open(file)
        my_request =service_pb2.PostModelOutputsRequest(model_id='9504135848be0dd2c39bdab0002f78e9',inputs=[resources_pb2.Input(data=resources_pb2.Data(image=resources_pb2.Image(base64=file.getvalue())))])
        response = stub.PostModelOutputs(my_request,metadata=metadata)
        print(response)
        if response.status.code != status_code_pb2.SUCCESS: 
            raise Exception("Request failed, status code: " + str(response.status.code))
      
        names = []
        confidences = []
        for concept in response.outputs[0].data.concepts:
            names.append(concept.name)
            confidences.append(concept.value)

        return render_template('button.html',img=image,searchResults=names)
    return render_template('button.html')


if __name__=="__main__":
    app.run(debug=True ,host='0.0.0.0')
     

But I get this error

ValueError: too many values to unpack (expected 2)

and it shows error in this particular line

response =stub.PostModelOutputs(my_request,metadata=metadata)

How do solve this error?? Thanks in advance

Safee987
  • 39
  • 4

1 Answers1

0

According to this API doc example, metadata should be as follows:

metadata = (('authorization', 'Key ' + PAT),)

You should remove the {} in your string.

  • I tried your solution I got this exception `Exception: Request failed, status code: 11102` why my request is not getting through – Safee987 Nov 14 '22 at 10:13
  • I'd suggest you accept the current solution and open a different question for your next question as it is not part of the original question. For your status code, reference https://docs.clarifai.com/api-guide/advanced-topics/status-codes/ – Isaac Chung Nov 14 '22 at 13:49