| 1526 | return state, state |
| 1527 | |
| 1528 | def run_image(self, image, state, txt, lang): |
| 1529 | image_filename = os.path.join('image', f"{str(uuid.uuid4())[:8]}.png") |
| 1530 | print("======>Auto Resize Image...") |
| 1531 | img = Image.open(image.name) |
| 1532 | width, height = img.size |
| 1533 | ratio = min(512 / width, 512 / height) |
| 1534 | width_new, height_new = (round(width * ratio), round(height * ratio)) |
| 1535 | width_new = int(np.round(width_new / 64.0)) * 64 |
| 1536 | height_new = int(np.round(height_new / 64.0)) * 64 |
| 1537 | img = img.resize((width_new, height_new)) |
| 1538 | img = img.convert('RGB') |
| 1539 | img.save(image_filename, "PNG") |
| 1540 | print(f"Resize image form {width}x{height} to {width_new}x{height_new}") |
| 1541 | description = self.models['ImageCaptioning'].inference(image_filename) |
| 1542 | if lang == 'Chinese': |
| 1543 | Human_prompt = f'\nHuman: 提供一张名为 {image_filename}的图片。它的描述是: {description}。 这些信息帮助你理解这个图像,但是你应该使用工具来完成下面的任务,而不是直接从我的描述中想象。 如果你明白了, 说 \"收到\". \n' |
| 1544 | AI_prompt = "收到。 " |
| 1545 | else: |
| 1546 | Human_prompt = f'\nHuman: provide a figure named {image_filename}. The description is: {description}. This information helps you to understand this image, but you should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n' |
| 1547 | AI_prompt = "Received. " |
| 1548 | self.agent.memory.buffer = self.agent.memory.buffer + Human_prompt + 'AI: ' + AI_prompt |
| 1549 | state = state + [(f"*{image_filename}*", AI_prompt)] |
| 1550 | print(f"\nProcessed run_image, Input image: {image_filename}\nCurrent state: {state}\n" |
| 1551 | f"Current Memory: {self.agent.memory.buffer}") |
| 1552 | return state, state, f'{txt} {image_filename} ' |
| 1553 | |
| 1554 | |
| 1555 | if __name__ == '__main__': |