pngwn / gradio-imageslider

ImageSlider custom component for gradio.
https://huggingface.co/spaces/pngwn/gradio_imageslider
30 stars 5 forks source link

TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'NoneType' #8

Closed SudhanshuBlaze closed 10 months ago

SudhanshuBlaze commented 10 months ago

Code

import gradio as gr
import os
import cv2
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
import PIL
import numpy as np
import uuid
from gradio_imageslider import ImageSlider

img_colorization = pipeline(Tasks.image_colorization, model='iic/cv_ddcolor_image-colorization')
img_path = 'input.png'
##result = img_colorization(img_path)
##cv2.imwrite('result.png', result[OutputKeys.OUTPUT_IMG])
def color(image):
    output = img_colorization(image[...,::-1])
    result = output[OutputKeys.OUTPUT_IMG].astype(np.uint8)
    # result = result[...,::-1]
    # Generate a unique filename using UUID
    unique_imgfilename = str(uuid.uuid4()) + '.png'
    cv2.imwrite(unique_imgfilename, result)
    print('infer finished!')
    return (image, unique_imgfilename)

title = "old_photo_restoration"
description = "upload old photo, ddcolor image colorization"
examples = [['./input.jpg'],]

demo = gr.Interface(fn=color,inputs="image",outputs=ImageSlider(position=0.5,label='Colored image with slider-view'),examples=examples,title=title,description=description)

if __name__ == "__main__":
    demo.launch(share=False)

Error

(base) sudhanshukumar /old_photo_restoration % python app.py                      
2024-01-29 01:02:33,686 - modelscope - INFO - PyTorch version 1.12.0 Found.
2024-01-29 01:02:33,690 - modelscope - INFO - TensorFlow version 2.11.0 Found.
2024-01-29 01:02:33,690 - modelscope - INFO - Loading ast index from /Users/sudhanshukumar/.cache/modelscope/ast_indexer
2024-01-29 01:02:34,610 - modelscope - INFO - Loading done! Current index file version is 1.11.1, with md5 55d4569cd88d66301ce46e7c540041b2 and a total number of 956 components indexed
Traceback (most recent call last):
  File "/Users/sudhanshukumar/Documents/GitHub/old_photo_restoration/app.py", line 10, in <module>
    from gradio_imageslider import ImageSlider
  File "/Users/sudhanshukumar/opt/miniconda3/lib/python3.9/site-packages/gradio_imageslider/__init__.py", line 1, in <module>
    from .imageslider import ImageSlider
  File "/Users/sudhanshukumar/opt/miniconda3/lib/python3.9/site-packages/gradio_imageslider/imageslider.py", line 23, in <module>
    class SliderData(GradioRootModel):
  File "/Users/sudhanshukumar/opt/miniconda3/lib/python3.9/site-packages/pydantic/_internal/_model_construction.py", line 181, in __new__
    set_model_fields(cls, bases, config_wrapper, types_namespace)
  File "/Users/sudhanshukumar/opt/miniconda3/lib/python3.9/site-packages/pydantic/_internal/_model_construction.py", line 426, in set_model_fields
    fields, class_vars = collect_model_fields(cls, bases, config_wrapper, types_namespace, typevars_map=typevars_map)
  File "/Users/sudhanshukumar/opt/miniconda3/lib/python3.9/site-packages/pydantic/_internal/_fields.py", line 120, in collect_model_fields
    type_hints = get_cls_type_hints_lenient(cls, types_namespace)
  File "/Users/sudhanshukumar/opt/miniconda3/lib/python3.9/site-packages/pydantic/_internal/_typing_extra.py", line 212, in get_cls_type_hints_lenient
    hints[name] = eval_type_lenient(value, globalns, localns)
  File "/Users/sudhanshukumar/opt/miniconda3/lib/python3.9/site-packages/pydantic/_internal/_typing_extra.py", line 224, in eval_type_lenient
    return typing._eval_type(value, globalns, localns)  # type: ignore
  File "/Users/sudhanshukumar/opt/miniconda3/lib/python3.9/typing.py", line 292, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "/Users/sudhanshukumar/opt/miniconda3/lib/python3.9/typing.py", line 554, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'NoneType'
(base) sudhanshukumar /old_photo_restoration % 
pngwn commented 10 months ago

Ah yes. You'll need to use a newer version of python to use this package. 3.10 definitely works fine.

I'll update the pyproject.toml.

SudhanshuBlaze commented 10 months ago

Yup upgrading to python 3.10 worked

LWQ2EDU commented 9 months ago

Older version of Python could avoid this error by using pip install eval_type_backport, and rewriting image_variants and image_tuple

image_variants = Union[_Image.Image, np.ndarray, str, Path, None]

image_tuple = Union[
    Tuple[str, str],
    Tuple[_Image.Image, _Image.Image],
    Tuple[np.ndarray, np.ndarray],
    None
]
SudhanshuBlaze commented 9 months ago

Thanks

On Mon, 5 Feb 2024, 4:39 am LWQ2EDU, @.***> wrote:

older version of Python could avoid this error by using pip install eval_type_backport, and rewriting image_variants and 'image_tuple'

image_variants = Union[_Image.Image, np.ndarray, str, Path, None] image_tuple = Union[ Tuple[str, str], Tuple[_Image.Image, _Image.Image], Tuple[np.ndarray, np.ndarray], None ]

— Reply to this email directly, view it on GitHub https://github.com/pngwn/gradio-imageslider/issues/8#issuecomment-1926176317, or unsubscribe https://github.com/notifications/unsubscribe-auth/APG3R4HU6WY6RSTCXBQYRX3YSBH65AVCNFSM6AAAAABCORTKV2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMRWGE3TMMZRG4 . You are receiving this because you modified the open/close state.Message ID: @.***>

Tomas-Lee commented 5 months ago

Older version of Python could avoid this error by using pip install eval_type_backport, and rewriting image_variants and image_tuple

image_variants = Union[_Image.Image, np.ndarray, str, Path, None]

image_tuple = Union[
    Tuple[str, str],
    Tuple[_Image.Image, _Image.Image],
    Tuple[np.ndarray, np.ndarray],
    None
]

directly copying these lines to main.py seems to not work. I cannot figure out why? how to use the library (eval_type_backport)? I also found that the gradio_imageslider0.0.18 have "from __future import annotation " statement, so theoretically the error should not happen when using 0.0.18, but it did occur. but if we install the gradio_imageslider==0.0.17(rather than the current release 0.0.18), the problem can be solved.