replicate / cog

Containers for machine learning
https://cog.run
Apache License 2.0
7.46k stars 513 forks source link

Compatibility issues with Cog Pydantic < 2.* #1384

Open sCarita opened 7 months ago

sCarita commented 7 months ago

Hello everyone,

I am building an image that needs segments-ai python package, which requires pydantic to be above version 2.*. The problem is that if I install the pydantic python package above version 2, I get an error during the "Validating model schema..." phase.

Validating model schema...

Traceback (most recent call last):
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/site-packages/cog/command/openapi_schema.py", line 10, in <module>
    from ..server.http import create_app
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/site-packages/cog/server/http.py", line 16, in <module>
    from fastapi import Body, FastAPI, Header, HTTPException, Path, Response
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/site-packages/fastapi/__init__.py", line 7, in <module>
    from .applications import FastAPI as FastAPI
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/site-packages/fastapi/applications.py", line 16, in <module>
    from fastapi import routing
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/site-packages/fastapi/routing.py", line 22, in <module>
    from fastapi import params
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/site-packages/fastapi/params.py", line 4, in <module>
    from pydantic.fields import FieldInfo, Undefined
ImportError: cannot import name 'Undefined' from 'pydantic.fields' (/root/.pyenv/versions/3.10.10/lib/python3.10/site-packages/pydantic/fields.py)

It seems that the fastapi version used by cog is expecting pydantic to be below version 2, where "Undefined" object exists...

My current cog version is: cog version 0.8.6 (built 2023-08-07T21:51:56Z)

Any ideas on how to workaround this are gladly accepted.

Thank you for your time.

sCarita commented 7 months ago

I understood we can control the fastapi version on the cog.yaml via python_packages key. So I did update, but now I get a new error which seems to be related with the internals of cog using pydantic v1 related options.

Traceback (most recent call last):
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/site-packages/cog/command/openapi_schema.py", line 10, in <module>
    from ..server.http import create_app
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/site-packages/cog/server/http.py", line 21, in <module>
    from pydantic.error_wrappers import ErrorWrapper
  File "/root/.pyenv/versions/3.10.10/lib/python3.10/site-packages/pydantic/_migration.py", line 302, in wrapper
    raise PydanticImportError(f'`{import_path}` has been removed in V2.')
pydantic.errors.PydanticImportError: `pydantic.error_wrappers:ErrorWrapper` has been removed in V2.

For further information visit https://errors.pydantic.dev/2.5/u/import-error

So I am trying to understand if all cog versions use:

from pydantic.error_wrappers import ErrorWrapper @ cog/server/http.py
sCarita commented 7 months ago

I am updating the source code now. to:

if request.id is not None and request.id != prediction_id:
        errors = [{
            'loc': ('body', 'id'),
            'msg': "prediction ID must match the ID supplied in the URL",
            'type': 'value_error',
        }]
        raise ValidationError(errors, PredictionRequest)
...

But when I am building... $ make

mkdir -p pkg/dockerfile/embed
cp dist/*.whl pkg/dockerfile/embed/cog.whl
CGO_ENABLED=0 go build -o cog \
    -ldflags "-X github.com/replicate/cog/pkg/global.Version=v0.9.0-beta10+dev -X github.com/replicate/cog/pkg/global.BuildTime=2023-11-19T20:42:00+0000 -w" \
    cmd/cog/cog.go
build command-line-arguments: cannot load embed: malformed module path "embed": missing dot in first path element
make: *** [Makefile:34: cog] Error 1
sCarita commented 7 months ago

I was able to solve the problem by updating the golang to version 1.20 as stated in go.mod. After it I was able to build my own cog with pydantic fixed. Had a few hiccups along the way, but it is working now.

cudanexus commented 4 months ago

@sCarita is there any new release or fix instructions for this, i am also getting the same error

  File "/root/.pyenv/versions/3.11.8/lib/python3.11/site-packages/cog/server/http.py", line 36, in <module>
    from pydantic.error_wrappers import ErrorWrapper
  File "/root/.pyenv/versions/3.11.8/lib/python3.11/site-packages/pydantic/_migration.py", line 302, in wrapper
    raise PydanticImportError(f'`{import_path}` has been removed in V2.')
pydantic.errors.PydanticImportError: `pydantic.error_wrappers:ErrorWrapper` has been removed in V2.

For further information visit https://errors.pydantic.dev/2.6/u/import-error
ⅹ Failed to get container status: exit status 1
aros0918 commented 3 months ago

I am also using cog for push my model to replicate but at the end of the process I got this error : Traceback (most recent call last): File "/root/.pyenv/versions/3.10.13/lib/python3.10/runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "/root/.pyenv/versions/3.10.13/lib/python3.10/runpy.py", line 86, in _run_code exec(code, run_globals) File "/root/.pyenv/versions/3.10.13/lib/python3.10/site-packages/cog/command/openapi_schema.py", line 25, in raise CogError(app.state.setup_result.logs) cog.errors.CogError: Error while loading predictor:

Traceback (most recent call last): File "/root/.pyenv/versions/3.10.13/lib/python3.10/site-packages/cog/server/http.py", line 137, in create_app predictor = load_predictor_from_ref(predictor_ref) File "/root/.pyenv/versions/3.10.13/lib/python3.10/site-packages/cog/predictor.py", line 187, in load_predictor_from_ref spec.loader.exec_module(module) File "", line 883, in exec_module File "", line 241, in _call_with_frames_removed File "/src/predict.py", line 1, in import gradio as gr File "/root/.pyenv/versions/3.10.13/lib/python3.10/site-packages/gradio/init.py", line 3, in import gradio._simple_templates File "/root/.pyenv/versions/3.10.13/lib/python3.10/site-packages/gradio/_simple_templates/init.py", line 1, in from .simpledropdown import SimpleDropdown File "/root/.pyenv/versions/3.10.13/lib/python3.10/site-packages/gradio/_simple_templates/simpledropdown.py", line 6, in from gradio.components.base import FormComponent File "/root/.pyenv/versions/3.10.13/lib/python3.10/site-packages/gradio/components/init.py", line 1, in from gradio.components.annotated_image import AnnotatedImage File "/root/.pyenv/versions/3.10.13/lib/python3.10/site-packages/gradio/components/annotated_image.py", line 13, in from gradio import processing_utils, utils File "/root/.pyenv/versions/3.10.13/lib/python3.10/site-packages/gradio/processing_utils.py", line 21, in from gradio import utils, wasm_utils File "/root/.pyenv/versions/3.10.13/lib/python3.10/site-packages/gradio/utils.py", line 48, in from gradio.data_classes import FileData File "/root/.pyenv/versions/3.10.13/lib/python3.10/site-packages/gradio/data_classes.py", line 18, in from pydantic import BaseModel, RootModel, ValidationError ImportError: cannot import name 'RootModel' from 'pydantic' (/root/.pyenv/versions/3.10.13/lib/python3.10/site-packages/pydantic/init.cpython-310-x86_64-linux-gnu.so)

how can i fix that problem?

andreasjansson commented 2 months ago

I don't believe this is fixed -- re-opening.