From fe952e674a24a8887d892f9be871e032e28dae62 Mon Sep 17 00:00:00 2001 From: chandlergis Date: Mon, 20 May 2024 14:45:37 +0800 Subject: [PATCH] Add Python --- __init__.py | 4 +++ ragpapi.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 __init__.py create mode 100644 ragpapi.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..2d3f465 --- /dev/null +++ b/__init__.py @@ -0,0 +1,4 @@ +from . import ragpapi + +NODE_CLASS_MAPPINGS = {**ragpapi.NODE_CLASS_MAPPINGS,} +NODE_DISPLAY_NAME_MAPPINGS = {**ragpapi.NODE_DISPLAY_NAME_MAPPINGS,} diff --git a/ragpapi.py b/ragpapi.py new file mode 100644 index 0000000..fb68957 --- /dev/null +++ b/ragpapi.py @@ -0,0 +1,72 @@ +import os +import io +import json +import requests +import torch +import numpy as np +from io import BytesIO +from PIL import Image + +def tensor2pil(image): + return Image.fromarray(np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8)) + +def pil2tensor(image): + return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0) + +class ImageRequestNode: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "query": ("STRING", {"default": "美女 车 雪山", "multiline": True}), + "n": ("INT", {"default": 1, "min": 1, "max": 10}), + "bm25_n": ("INT", {"default": 500, "min": 1, "max": 1000}), + "retrieve_n": ("INT", {"default": 10, "min": 1, "max": 100}), + } + } + + RETURN_TYPES = ("IMAGE",) + FUNCTION = "fetch_image" + CATEGORY = "🔥Image Request" + + def fetch_image(self, query, n, bm25_n, retrieve_n): + url = "http://10.0.100.224:58000/rag_proxy/vl_result_fix" + payload = json.dumps({ + "query": query, + "n": n, + "bm25_n": bm25_n, + "retrieve_n": retrieve_n + }) + headers = { + 'Content-Type': 'application/json' + } + + response = requests.post(url, headers=headers, data=payload) + + if response.status_code == 200: + json_data = response.json() + if json_data['code'] == 200 and 'data' in json_data and len(json_data['data']) > 0: + image_url = json_data['data'][0]['url'] + image_response = requests.get(image_url) + if image_response.status_code == 200: + image_data = Image.open(BytesIO(image_response.content)) + output_t = pil2tensor(image_data) + return (output_t,) + else: + raise Exception(f"Failed to fetch image from URL: {image_response.status_code}") + else: + raise Exception(f"API response error: {json_data.get('message', 'Unknown error')}") + else: + raise Exception(f"Failed to fetch data: {response.status_code}") + +NODE_CLASS_MAPPINGS = { + "ImageRequestNode": ImageRequestNode, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "ImageRequestNode": "🔥Image Request Node", +} +