Process image in text. Args: text: str, text. image: Optional, image path / url / PIL image.
(text, image=None)
| 63 | return response |
| 64 | |
| 65 | def process_image(text, image=None): |
| 66 | '''Process image in text. |
| 67 | Args: |
| 68 | text: str, text. |
| 69 | image: Optional, image path / url / PIL image. |
| 70 | ''' |
| 71 | image_position = text.rfind("<img>") + 5 |
| 72 | if image_position < 5: |
| 73 | return text, image_position, None |
| 74 | # extract path from <img></img> using re |
| 75 | image_path = re.findall(r"<img>(.*?)</img>", text) |
| 76 | image_path = image_path[-1] if image_path[-1] else None |
| 77 | if image_path is not None: |
| 78 | assert image is None, "image and image_path cannot be both not None." |
| 79 | text = text.replace(image_path, "") |
| 80 | image_path = image_path.strip() |
| 81 | # url |
| 82 | if image_path.startswith("http"): |
| 83 | response = requests.get(image_path, timeout=10) |
| 84 | image = Image.open(BytesIO(response.content)) |
| 85 | # local path |
| 86 | else: |
| 87 | image = Image.open(image_path) |
| 88 | if image is not None and isinstance(image, Image.Image): |
| 89 | processor = BlipImageEvalProcessor(224) |
| 90 | image = processor(image.convert('RGB')) |
| 91 | image = image.unsqueeze(0) |
| 92 | return text, image_position, image |
| 93 | |
| 94 | |
| 95 | def chat(image_path, model, tokenizer, |
no test coverage detected