【问题标题】:Python Clarifai program stopped working inspite of no changes made尽管没有进行任何更改,Python Clarifai 程序停止工作
【发布时间】:2022-06-15 01:51:18
【问题描述】:

我有以下 Python 代码来使用 Clarifai 标记图像。这是一个工作代码,在过去 6-8 个月内一直可用。但是,在过去的几天里,我一直收到下面提到的错误。请注意,我没有对代码的工作版本进行任何更改以使错误蔓延。

#python program to analyse an image and label it

'''
Dependencies:
pip install flask
pip install clarifai-grpc
pip install logging
'''

import json
from flask import Flask, render_template, request 
import logging
import os

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_pb2, status_code_pb2

channel = ClarifaiChannel.get_json_channel()

stub = service_pb2_grpc.V2Stub(channel)

# This will be used by every Clarifai endpoint call.
# The word 'Key' is required to precede the authentication key.
metadata = (('authorization', 'Key API_KEY_HERE'),)

webapp = Flask(__name__) #creating a web application for the current python file

#decorators
@webapp.route('/')
def index():
    return render_template('index.html', len=0)
  
@webapp.route('/' , methods = ['POST'])

def search():

    if request.form['searchByURL']:

        url = request.form['searchByURL']

        my_request = service_pb2.PostModelOutputsRequest(
            # This is the model ID of a publicly available General model.
            #You may use any other public or custom model ID.
            model_id='aaa03c23b3724a16a56b629203edc62c',
            inputs=[
              resources_pb2.Input(data=resources_pb2.Data(image=resources_pb2.Image(url=url)))
            ])
        response = stub.PostModelOutputs(my_request, metadata=metadata)

        if response.status.code != status_code_pb2.SUCCESS:
            message = ["You have reached the limit for today!"]
            return render_template('/index.html' , len = 1, searchResults = message) 
            

        concepts = []

        
        for concept in response.outputs[0].data.concepts:
            concepts.append(concept.name)

        concepts = concepts[0:10]
        return render_template('/index.html' , len = len(concepts), searchResults = concepts )



    elif request.files['searchByImage']:

        file = request.files['searchByImage']    
        file.save(file.filename)

        #IMAGE INPUT:
        with open(file.filename, "rb") as f:
            file_bytes = f.read()

        post_model_outputs_response = stub.PostModelOutputs(
            service_pb2.PostModelOutputsRequest(
                model_id="aaa03c23b3724a16a56b629203edc62c",
                version_id="aa7f35c01e0642fda5cf400f543e7c40",  # This is optional. Defaults to the latest model version.
                inputs=[
                    resources_pb2.Input(
                        data=resources_pb2.Data(
                            image=resources_pb2.Image(
                                base64=file_bytes
                            )
                        )
                    )
                ]
            ),
            metadata=metadata
        )
        
        if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
            message = ["You have reached the limit for today!"]
            return render_template('/index.html' , len = 1, searchResults = message)

        # Since we have one input, one output will exist here.
        output = post_model_outputs_response.outputs[0]
        os.remove(file.filename)
        concepts = []
        #Predicted concepts:
        for concept in output.data.concepts:
            concepts.append(concept.name)           
  
        concepts = concepts[0:10]


        return render_template('/index.html' , len = len(concepts), searchResults = concepts )

    else:
        return render_template('/index.html' , len = 1, searchResults = ["No Image entered!"] )

#run the server
if __name__ == "__main__":

    logging.basicConfig(filename = 'error.log' , level = logging.DEBUG, )
    webapp.run(debug=True)

错误:

Exception has occurred: ImportError
dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/grpc/_cython/cygrpc.cpython-310-darwin.so, 0x0002): tried: '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/grpc/_cython/cygrpc.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))
  File "/Users/eshaangupta/Desktop/Python-Level-4/Image Analyser.py", line 15, in <module>
    from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel

【问题讨论】:

    标签: python flask grpc clarifai


    【解决方案1】:

    您似乎正试图在与过去不同的架构上运行它。您一直在 x86 上运行(看起来可能是 MacOS),现在已经迁移到 arm 架构。我猜你已经升级到 M1 芯片 macbook,虽然你可能已经转移到不同的基于 ARM 的芯片。

    (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))

    这是 grpc 中的一个文件的问题 - 特别是库文件 cygrpc.cpython-310-darwin.so。我建议删除 gRPC 并重新安装,看看是否有助于解决问题。

    这样的事情可能会奏效:

    python -m pip install --force-reinstall grpcio
    
    

    (这是假设您系统中的python指向python3.10)。

    虽然我不确定你是如何安装它的,所以这只是一个猜测。

    【讨论】:

      猜你喜欢
      • 2021-11-28
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多