clarifai- ValueError: too many values to unpack (expected 2)

281 views Asked by At

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

2

There are 2 answers

2
Isaac Chung On

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

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

You should remove the {} in your string.

0
mimarcel On

You should provide the file content, not the file path. file.getvalue() returns the path to the file on your machine, but you should provide its contents using file.read() like in the documentation.