1 Star 0 Fork 0

liuzhe/Grounded-Segment-Anything-MindIE

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
chatbot.py 79.87 KB
一键复制 编辑 原始数据 按行查看 历史
liuzhe 提交于 2024-08-06 01:20 . init
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
# coding: utf-8
import os
import gradio as gr
import random
import torch
import cv2
import re
import uuid
from PIL import Image, ImageDraw, ImageOps
import math
import numpy as np
import argparse
import inspect
import shutil
import torchvision
import whisper
import matplotlib.pyplot as plt
from automatic_label_demo import load_model, load_image, get_grounding_output, show_box, show_mask, generate_tags, check_caption
from grounding_dino_demo import plot_boxes_to_image
from segment_anything import build_sam, SamAutomaticMaskGenerator, SamPredictor
from segment_anything.utils.amg import remove_small_regions
from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation
from transformers import pipeline, BlipProcessor, BlipForConditionalGeneration, BlipForQuestionAnswering
from transformers import AutoImageProcessor, UperNetForSemanticSegmentation
from diffusers import StableDiffusionPipeline, StableDiffusionInpaintPipeline, StableDiffusionInstructPix2PixPipeline
from diffusers import EulerAncestralDiscreteScheduler
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
from controlnet_aux import OpenposeDetector, MLSDdetector, HEDdetector
from langchain.agents.initialize import initialize_agent
from langchain.agents.tools import Tool
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain.llms.openai import OpenAI
VISUAL_CHATGPT_PREFIX = """Visual ChatGPT is designed to be able to assist with a wide range of text and visual related tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. Visual ChatGPT is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Visual ChatGPT is able to process and understand large amounts of text and images. As a language model, Visual ChatGPT can not directly read images, but it has a list of tools to finish different visual tasks. Each image will have a file name formed as "image/xxx.png", and Visual ChatGPT can invoke different tools to indirectly understand pictures. When talking about images, Visual ChatGPT is very strict to the file name and will never fabricate nonexistent files. When using tools to generate new image files, Visual ChatGPT is also known that the image may not be the same as the user's demand, and will use other visual question answering tools or description tools to observe the real image. Visual ChatGPT is able to use tools in a sequence, and is loyal to the tool observation outputs rather than faking the image content and image file name. It will remember to provide the file name from the last tool observation, if a new image is generated.
Human may provide new figures to Visual ChatGPT with a description. The description helps Visual ChatGPT to understand this image, but Visual ChatGPT should use tools to finish following tasks, rather than directly imagine from the description.
Overall, Visual ChatGPT is a powerful visual dialogue assistant tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics.
TOOLS:
------
Visual ChatGPT has access to the following tools:"""
VISUAL_CHATGPT_FORMAT_INSTRUCTIONS = """To use a tool, please use the following format:
```
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
```
When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
```
Thought: Do I need to use a tool? No
{ai_prefix}: [your response here]
```
"""
VISUAL_CHATGPT_SUFFIX = """You are very strict to the filename correctness and will never fake a file name if it does not exist.
You will remember to provide the image file name loyally if it's provided in the last tool observation.
Begin!
Previous conversation history:
{chat_history}
New input: {input}
Since Visual ChatGPT is a text language model, Visual ChatGPT must use tools to observe images rather than imagination.
The thoughts and observations are only visible for Visual ChatGPT, Visual ChatGPT should remember to repeat important information in the final response for Human.
Thought: Do I need to use a tool? {agent_scratchpad} Let's think step by step.
"""
VISUAL_CHATGPT_PREFIX_CN = """Visual ChatGPT 旨在能够协助完成范围广泛的文本和视觉相关任务,从回答简单的问题到提供对广泛主题的深入解释和讨论。 Visual ChatGPT 能够根据收到的输入生成类似人类的文本,使其能够进行听起来自然的对话,并提供连贯且与手头主题相关的响应。
Visual ChatGPT 能够处理和理解大量文本和图像。作为一种语言模型,Visual ChatGPT 不能直接读取图像,但它有一系列工具来完成不同的视觉任务。每张图片都会有一个文件名,格式为“image/xxx.png”,Visual ChatGPT可以调用不同的工具来间接理解图片。在谈论图片时,Visual ChatGPT 对文件名的要求非常严格,绝不会伪造不存在的文件。在使用工具生成新的图像文件时,Visual ChatGPT也知道图像可能与用户需求不一样,会使用其他视觉问答工具或描述工具来观察真实图像。 Visual ChatGPT 能够按顺序使用工具,并且忠于工具观察输出,而不是伪造图像内容和图像文件名。如果生成新图像,它将记得提供上次工具观察的文件名。
Human 可能会向 Visual ChatGPT 提供带有描述的新图形。描述帮助 Visual ChatGPT 理解这个图像,但 Visual ChatGPT 应该使用工具来完成以下任务,而不是直接从描述中想象。有些工具将会返回英文描述,但你对用户的聊天应当采用中文。
总的来说,Visual ChatGPT 是一个强大的可视化对话辅助工具,可以帮助处理范围广泛的任务,并提供关于范围广泛的主题的有价值的见解和信息。
工具列表:
------
Visual ChatGPT 可以使用这些工具:"""
VISUAL_CHATGPT_FORMAT_INSTRUCTIONS_CN = """用户使用中文和你进行聊天,但是工具的参数应当使用英文。如果要调用工具,你必须遵循如下格式:
```
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
```
当你不再需要继续调用工具,而是对观察结果进行总结回复时,你必须使用如下格式:
```
Thought: Do I need to use a tool? No
{ai_prefix}: [your response here]
```
"""
VISUAL_CHATGPT_SUFFIX_CN = """你对文件名的正确性非常严格,而且永远不会伪造不存在的文件。
开始!
因为Visual ChatGPT是一个文本语言模型,必须使用工具去观察图片而不是依靠想象。
推理想法和观察结果只对Visual ChatGPT可见,需要记得在最终回复时把重要的信息重复给用户,你只能给用户返回中文句子。我们一步一步思考。在你使用工具时,工具的参数只能是英文。
聊天历史:
{chat_history}
新输入: {input}
Thought: Do I need to use a tool? {agent_scratchpad}
"""
os.makedirs('image', exist_ok=True)
def seed_everything(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
return seed
def prompts(name, description):
def decorator(func):
func.name = name
func.description = description
return func
return decorator
def blend_gt2pt(old_image, new_image, sigma=0.15, steps=100):
new_size = new_image.size
old_size = old_image.size
easy_img = np.array(new_image)
gt_img_array = np.array(old_image)
pos_w = (new_size[0] - old_size[0]) // 2
pos_h = (new_size[1] - old_size[1]) // 2
kernel_h = cv2.getGaussianKernel(old_size[1], old_size[1] * sigma)
kernel_w = cv2.getGaussianKernel(old_size[0], old_size[0] * sigma)
kernel = np.multiply(kernel_h, np.transpose(kernel_w))
kernel[steps:-steps, steps:-steps] = 1
kernel[:steps, :steps] = kernel[:steps, :steps] / kernel[steps - 1, steps - 1]
kernel[:steps, -steps:] = kernel[:steps, -steps:] / kernel[steps - 1, -(steps)]
kernel[-steps:, :steps] = kernel[-steps:, :steps] / kernel[-steps, steps - 1]
kernel[-steps:, -steps:] = kernel[-steps:, -steps:] / kernel[-steps, -steps]
kernel = np.expand_dims(kernel, 2)
kernel = np.repeat(kernel, 3, 2)
weight = np.linspace(0, 1, steps)
top = np.expand_dims(weight, 1)
top = np.repeat(top, old_size[0] - 2 * steps, 1)
top = np.expand_dims(top, 2)
top = np.repeat(top, 3, 2)
weight = np.linspace(1, 0, steps)
down = np.expand_dims(weight, 1)
down = np.repeat(down, old_size[0] - 2 * steps, 1)
down = np.expand_dims(down, 2)
down = np.repeat(down, 3, 2)
weight = np.linspace(0, 1, steps)
left = np.expand_dims(weight, 0)
left = np.repeat(left, old_size[1] - 2 * steps, 0)
left = np.expand_dims(left, 2)
left = np.repeat(left, 3, 2)
weight = np.linspace(1, 0, steps)
right = np.expand_dims(weight, 0)
right = np.repeat(right, old_size[1] - 2 * steps, 0)
right = np.expand_dims(right, 2)
right = np.repeat(right, 3, 2)
kernel[:steps, steps:-steps] = top
kernel[-steps:, steps:-steps] = down
kernel[steps:-steps, :steps] = left
kernel[steps:-steps, -steps:] = right
pt_gt_img = easy_img[pos_h:pos_h + old_size[1], pos_w:pos_w + old_size[0]]
gaussian_gt_img = kernel * gt_img_array + (1 - kernel) * pt_gt_img # gt img with blur img
gaussian_gt_img = gaussian_gt_img.astype(np.int64)
easy_img[pos_h:pos_h + old_size[1], pos_w:pos_w + old_size[0]] = gaussian_gt_img
gaussian_img = Image.fromarray(easy_img)
return gaussian_img
def cut_dialogue_history(history_memory, keep_last_n_words=500):
if history_memory is None or len(history_memory) == 0:
return history_memory
tokens = history_memory.split()
n_tokens = len(tokens)
print(f"history_memory:{history_memory}, n_tokens: {n_tokens}")
if n_tokens < keep_last_n_words:
return history_memory
paragraphs = history_memory.split('\n')
last_n_tokens = n_tokens
while last_n_tokens >= keep_last_n_words:
last_n_tokens -= len(paragraphs[0].split(' '))
paragraphs = paragraphs[1:]
return '\n' + '\n'.join(paragraphs)
def get_new_image_name(org_img_name, func_name="update"):
head_tail = os.path.split(org_img_name)
head = head_tail[0]
tail = head_tail[1]
name_split = tail.split('.')[0].split('_')
this_new_uuid = str(uuid.uuid4())[:4]
if len(name_split) == 1:
most_org_file_name = name_split[0]
else:
assert len(name_split) == 4
most_org_file_name = name_split[3]
recent_prev_file_name = name_split[0]
new_file_name = f'{this_new_uuid}_{func_name}_{recent_prev_file_name}_{most_org_file_name}.png'
return os.path.join(head, new_file_name)
class MaskFormer:
def __init__(self, device):
print(f"Initializing MaskFormer to {device}")
self.device = device
self.processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
self.model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined").to(device)
def inference(self, image_path, text):
threshold = 0.5
min_area = 0.02
padding = 20
original_image = Image.open(image_path)
image = original_image.resize((512, 512))
inputs = self.processor(text=text, images=image, padding="max_length", return_tensors="pt").to(self.device)
with torch.no_grad():
outputs = self.model(**inputs)
mask = torch.sigmoid(outputs[0]).squeeze().cpu().numpy() > threshold
area_ratio = len(np.argwhere(mask)) / (mask.shape[0] * mask.shape[1])
if area_ratio < min_area:
return None
true_indices = np.argwhere(mask)
mask_array = np.zeros_like(mask, dtype=bool)
for idx in true_indices:
padded_slice = tuple(slice(max(0, i - padding), i + padding + 1) for i in idx)
mask_array[padded_slice] = True
visual_mask = (mask_array * 255).astype(np.uint8)
image_mask = Image.fromarray(visual_mask)
return image_mask.resize(original_image.size)
class ImageEditing:
def __init__(self, device):
print(f"Initializing ImageEditing to {device}")
self.device = device
self.mask_former = MaskFormer(device=self.device)
self.revision = 'fp16' if 'cuda' in device else None
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.inpaint = StableDiffusionInpaintPipeline.from_pretrained(
"runwayml/stable-diffusion-inpainting", revision=self.revision, torch_dtype=self.torch_dtype).to(device)
@prompts(name="Replace Something From The Photo",
description="useful when you want to replace an object from the object description or "
"location with another object from its description. "
"The input to this tool should be a comma separated string of three, "
"representing the image_path, the object to be replaced, the object to be replaced with ")
def inference_replace(self, inputs):
image_path, to_be_replaced_txt, replace_with_txt = inputs.split(",")
original_image = Image.open(image_path)
original_size = original_image.size
mask_image = self.mask_former.inference(image_path, to_be_replaced_txt)
updated_image = self.inpaint(prompt=replace_with_txt, image=original_image.resize((512, 512)),
mask_image=mask_image.resize((512, 512))).images[0]
updated_image_path = get_new_image_name(image_path, func_name="replace-something")
updated_image = updated_image.resize(original_size)
updated_image.save(updated_image_path)
print(
f"\nProcessed ImageEditing, Input Image: {image_path}, Replace {to_be_replaced_txt} to {replace_with_txt}, "
f"Output Image: {updated_image_path}")
return updated_image_path
class InstructPix2Pix:
def __init__(self, device):
print(f"Initializing InstructPix2Pix to {device}")
self.device = device
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained("timbrooks/instruct-pix2pix",
safety_checker=None,
torch_dtype=self.torch_dtype).to(device)
self.pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(self.pipe.scheduler.config)
@prompts(name="Instruct Image Using Text",
description="useful when you want to the style of the image to be like the text. "
"like: make it look like a painting. or make it like a robot. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the text. ")
def inference(self, inputs):
"""Change style of image."""
print("===>Starting InstructPix2Pix Inference")
image_path, text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
original_image = Image.open(image_path)
image = self.pipe(text, image=original_image, num_inference_steps=40, image_guidance_scale=1.2).images[0]
updated_image_path = get_new_image_name(image_path, func_name="pix2pix")
image.save(updated_image_path)
print(f"\nProcessed InstructPix2Pix, Input Image: {image_path}, Instruct Text: {text}, "
f"Output Image: {updated_image_path}")
return updated_image_path
class Text2Image:
def __init__(self, device):
print(f"Initializing Text2Image to {device}")
self.device = device
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5",
torch_dtype=self.torch_dtype)
self.pipe.to(device)
self.a_prompt = 'best quality, extremely detailed'
self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
'fewer digits, cropped, worst quality, low quality'
@prompts(name="Generate Image From User Input Text",
description="useful when you want to generate an image from a user input text and save it to a file. "
"like: generate an image of an object or something, or generate an image that includes some objects. "
"The input to this tool should be a string, representing the text used to generate image. ")
def inference(self, text):
image_filename = os.path.join('image', f"{str(uuid.uuid4())[:8]}.png")
prompt = text + ', ' + self.a_prompt
image = self.pipe(prompt, negative_prompt=self.n_prompt).images[0]
image.save(image_filename)
print(
f"\nProcessed Text2Image, Input Text: {text}, Output Image: {image_filename}")
return image_filename
class ImageCaptioning:
def __init__(self, device):
print(f"Initializing ImageCaptioning to {device}")
self.device = device
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
self.model = BlipForConditionalGeneration.from_pretrained(
"Salesforce/blip-image-captioning-base", torch_dtype=self.torch_dtype).to(self.device)
@prompts(name="Get Photo Description",
description="useful when you want to know what is inside the photo. receives image_path as input. "
"The input to this tool should be a string, representing the image_path. ")
def inference(self, image_path):
inputs = self.processor(Image.open(image_path), return_tensors="pt").to(self.device, self.torch_dtype)
out = self.model.generate(**inputs)
captions = self.processor.decode(out[0], skip_special_tokens=True)
print(f"\nProcessed ImageCaptioning, Input Image: {image_path}, Output Text: {captions}")
return captions
class Image2Canny:
def __init__(self, device):
print("Initializing Image2Canny")
self.low_threshold = 100
self.high_threshold = 200
@prompts(name="Edge Detection On Image",
description="useful when you want to detect the edge of the image. "
"like: detect the edges of this image, or canny detection on image, "
"or perform edge detection on this image, or detect the canny image of this image. "
"The input to this tool should be a string, representing the image_path")
def inference(self, inputs):
image = Image.open(inputs)
image = np.array(image)
canny = cv2.Canny(image, self.low_threshold, self.high_threshold)
canny = canny[:, :, None]
canny = np.concatenate([canny, canny, canny], axis=2)
canny = Image.fromarray(canny)
updated_image_path = get_new_image_name(inputs, func_name="edge")
canny.save(updated_image_path)
print(f"\nProcessed Image2Canny, Input Image: {inputs}, Output Text: {updated_image_path}")
return updated_image_path
class CannyText2Image:
def __init__(self, device):
print(f"Initializing CannyText2Image to {device}")
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-canny",
torch_dtype=self.torch_dtype)
self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
torch_dtype=self.torch_dtype)
self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
self.pipe.to(device)
self.seed = -1
self.a_prompt = 'best quality, extremely detailed'
self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
'fewer digits, cropped, worst quality, low quality'
@prompts(name="Generate Image Condition On Canny Image",
description="useful when you want to generate a new real image from both the user description and a canny image."
" like: generate a real image of a object or something from this canny image,"
" or generate a new real image of a object or something from this edge image. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the user description. ")
def inference(self, inputs):
image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
image = Image.open(image_path)
self.seed = random.randint(0, 65535)
seed_everything(self.seed)
prompt = f'{instruct_text}, {self.a_prompt}'
image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
guidance_scale=9.0).images[0]
updated_image_path = get_new_image_name(image_path, func_name="canny2image")
image.save(updated_image_path)
print(f"\nProcessed CannyText2Image, Input Canny: {image_path}, Input Text: {instruct_text}, "
f"Output Text: {updated_image_path}")
return updated_image_path
class Image2Line:
def __init__(self, device):
print("Initializing Image2Line")
self.detector = MLSDdetector.from_pretrained('lllyasviel/ControlNet')
@prompts(name="Line Detection On Image",
description="useful when you want to detect the straight line of the image. "
"like: detect the straight lines of this image, or straight line detection on image, "
"or perform straight line detection on this image, or detect the straight line image of this image. "
"The input to this tool should be a string, representing the image_path")
def inference(self, inputs):
image = Image.open(inputs)
mlsd = self.detector(image)
updated_image_path = get_new_image_name(inputs, func_name="line-of")
mlsd.save(updated_image_path)
print(f"\nProcessed Image2Line, Input Image: {inputs}, Output Line: {updated_image_path}")
return updated_image_path
class LineText2Image:
def __init__(self, device):
print(f"Initializing LineText2Image to {device}")
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-mlsd",
torch_dtype=self.torch_dtype)
self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
torch_dtype=self.torch_dtype
)
self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
self.pipe.to(device)
self.seed = -1
self.a_prompt = 'best quality, extremely detailed'
self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
'fewer digits, cropped, worst quality, low quality'
@prompts(name="Generate Image Condition On Line Image",
description="useful when you want to generate a new real image from both the user description "
"and a straight line image. "
"like: generate a real image of a object or something from this straight line image, "
"or generate a new real image of a object or something from this straight lines. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the user description. ")
def inference(self, inputs):
image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
image = Image.open(image_path)
self.seed = random.randint(0, 65535)
seed_everything(self.seed)
prompt = f'{instruct_text}, {self.a_prompt}'
image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
guidance_scale=9.0).images[0]
updated_image_path = get_new_image_name(image_path, func_name="line2image")
image.save(updated_image_path)
print(f"\nProcessed LineText2Image, Input Line: {image_path}, Input Text: {instruct_text}, "
f"Output Text: {updated_image_path}")
return updated_image_path
class Image2Hed:
def __init__(self, device):
print("Initializing Image2Hed")
self.detector = HEDdetector.from_pretrained('lllyasviel/ControlNet')
@prompts(name="Hed Detection On Image",
description="useful when you want to detect the soft hed boundary of the image. "
"like: detect the soft hed boundary of this image, or hed boundary detection on image, "
"or perform hed boundary detection on this image, or detect soft hed boundary image of this image. "
"The input to this tool should be a string, representing the image_path")
def inference(self, inputs):
image = Image.open(inputs)
hed = self.detector(image)
updated_image_path = get_new_image_name(inputs, func_name="hed-boundary")
hed.save(updated_image_path)
print(f"\nProcessed Image2Hed, Input Image: {inputs}, Output Hed: {updated_image_path}")
return updated_image_path
class HedText2Image:
def __init__(self, device):
print(f"Initializing HedText2Image to {device}")
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-hed",
torch_dtype=self.torch_dtype)
self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
torch_dtype=self.torch_dtype
)
self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
self.pipe.to(device)
self.seed = -1
self.a_prompt = 'best quality, extremely detailed'
self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
'fewer digits, cropped, worst quality, low quality'
@prompts(name="Generate Image Condition On Soft Hed Boundary Image",
description="useful when you want to generate a new real image from both the user description "
"and a soft hed boundary image. "
"like: generate a real image of a object or something from this soft hed boundary image, "
"or generate a new real image of a object or something from this hed boundary. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the user description")
def inference(self, inputs):
image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
image = Image.open(image_path)
self.seed = random.randint(0, 65535)
seed_everything(self.seed)
prompt = f'{instruct_text}, {self.a_prompt}'
image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
guidance_scale=9.0).images[0]
updated_image_path = get_new_image_name(image_path, func_name="hed2image")
image.save(updated_image_path)
print(f"\nProcessed HedText2Image, Input Hed: {image_path}, Input Text: {instruct_text}, "
f"Output Image: {updated_image_path}")
return updated_image_path
class Image2Scribble:
def __init__(self, device):
print("Initializing Image2Scribble")
self.detector = HEDdetector.from_pretrained('lllyasviel/ControlNet')
@prompts(name="Sketch Detection On Image",
description="useful when you want to generate a scribble of the image. "
"like: generate a scribble of this image, or generate a sketch from this image, "
"detect the sketch from this image. "
"The input to this tool should be a string, representing the image_path")
def inference(self, inputs):
image = Image.open(inputs)
scribble = self.detector(image, scribble=True)
updated_image_path = get_new_image_name(inputs, func_name="scribble")
scribble.save(updated_image_path)
print(f"\nProcessed Image2Scribble, Input Image: {inputs}, Output Scribble: {updated_image_path}")
return updated_image_path
class ScribbleText2Image:
def __init__(self, device):
print(f"Initializing ScribbleText2Image to {device}")
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-scribble",
torch_dtype=self.torch_dtype)
self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
torch_dtype=self.torch_dtype
)
self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
self.pipe.to(device)
self.seed = -1
self.a_prompt = 'best quality, extremely detailed'
self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
'fewer digits, cropped, worst quality, low quality'
@prompts(name="Generate Image Condition On Sketch Image",
description="useful when you want to generate a new real image from both the user description and "
"a scribble image or a sketch image. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the user description")
def inference(self, inputs):
image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
image = Image.open(image_path)
self.seed = random.randint(0, 65535)
seed_everything(self.seed)
prompt = f'{instruct_text}, {self.a_prompt}'
image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
guidance_scale=9.0).images[0]
updated_image_path = get_new_image_name(image_path, func_name="scribble2image")
image.save(updated_image_path)
print(f"\nProcessed ScribbleText2Image, Input Scribble: {image_path}, Input Text: {instruct_text}, "
f"Output Image: {updated_image_path}")
return updated_image_path
class Image2Pose:
def __init__(self, device):
print("Initializing Image2Pose")
self.detector = OpenposeDetector.from_pretrained('lllyasviel/ControlNet')
@prompts(name="Pose Detection On Image",
description="useful when you want to detect the human pose of the image. "
"like: generate human poses of this image, or generate a pose image from this image. "
"The input to this tool should be a string, representing the image_path")
def inference(self, inputs):
image = Image.open(inputs)
pose = self.detector(image)
updated_image_path = get_new_image_name(inputs, func_name="human-pose")
pose.save(updated_image_path)
print(f"\nProcessed Image2Pose, Input Image: {inputs}, Output Pose: {updated_image_path}")
return updated_image_path
class PoseText2Image:
def __init__(self, device):
print(f"Initializing PoseText2Image to {device}")
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-openpose",
torch_dtype=self.torch_dtype)
self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
torch_dtype=self.torch_dtype)
self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
self.pipe.to(device)
self.num_inference_steps = 20
self.seed = -1
self.unconditional_guidance_scale = 9.0
self.a_prompt = 'best quality, extremely detailed'
self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit,' \
' fewer digits, cropped, worst quality, low quality'
@prompts(name="Generate Image Condition On Pose Image",
description="useful when you want to generate a new real image from both the user description "
"and a human pose image. "
"like: generate a real image of a human from this human pose image, "
"or generate a new real image of a human from this pose. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the user description")
def inference(self, inputs):
image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
image = Image.open(image_path)
self.seed = random.randint(0, 65535)
seed_everything(self.seed)
prompt = f'{instruct_text}, {self.a_prompt}'
image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
guidance_scale=9.0).images[0]
updated_image_path = get_new_image_name(image_path, func_name="pose2image")
image.save(updated_image_path)
print(f"\nProcessed PoseText2Image, Input Pose: {image_path}, Input Text: {instruct_text}, "
f"Output Image: {updated_image_path}")
return updated_image_path
class Image2Seg:
def __init__(self, device):
print("Initializing Image2Seg")
self.image_processor = AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-small")
self.image_segmentor = UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-small")
self.ade_palette = [[120, 120, 120], [180, 120, 120], [6, 230, 230], [80, 50, 50],
[4, 200, 3], [120, 120, 80], [140, 140, 140], [204, 5, 255],
[230, 230, 230], [4, 250, 7], [224, 5, 255], [235, 255, 7],
[150, 5, 61], [120, 120, 70], [8, 255, 51], [255, 6, 82],
[143, 255, 140], [204, 255, 4], [255, 51, 7], [204, 70, 3],
[0, 102, 200], [61, 230, 250], [255, 6, 51], [11, 102, 255],
[255, 7, 71], [255, 9, 224], [9, 7, 230], [220, 220, 220],
[255, 9, 92], [112, 9, 255], [8, 255, 214], [7, 255, 224],
[255, 184, 6], [10, 255, 71], [255, 41, 10], [7, 255, 255],
[224, 255, 8], [102, 8, 255], [255, 61, 6], [255, 194, 7],
[255, 122, 8], [0, 255, 20], [255, 8, 41], [255, 5, 153],
[6, 51, 255], [235, 12, 255], [160, 150, 20], [0, 163, 255],
[140, 140, 140], [250, 10, 15], [20, 255, 0], [31, 255, 0],
[255, 31, 0], [255, 224, 0], [153, 255, 0], [0, 0, 255],
[255, 71, 0], [0, 235, 255], [0, 173, 255], [31, 0, 255],
[11, 200, 200], [255, 82, 0], [0, 255, 245], [0, 61, 255],
[0, 255, 112], [0, 255, 133], [255, 0, 0], [255, 163, 0],
[255, 102, 0], [194, 255, 0], [0, 143, 255], [51, 255, 0],
[0, 82, 255], [0, 255, 41], [0, 255, 173], [10, 0, 255],
[173, 255, 0], [0, 255, 153], [255, 92, 0], [255, 0, 255],
[255, 0, 245], [255, 0, 102], [255, 173, 0], [255, 0, 20],
[255, 184, 184], [0, 31, 255], [0, 255, 61], [0, 71, 255],
[255, 0, 204], [0, 255, 194], [0, 255, 82], [0, 10, 255],
[0, 112, 255], [51, 0, 255], [0, 194, 255], [0, 122, 255],
[0, 255, 163], [255, 153, 0], [0, 255, 10], [255, 112, 0],
[143, 255, 0], [82, 0, 255], [163, 255, 0], [255, 235, 0],
[8, 184, 170], [133, 0, 255], [0, 255, 92], [184, 0, 255],
[255, 0, 31], [0, 184, 255], [0, 214, 255], [255, 0, 112],
[92, 255, 0], [0, 224, 255], [112, 224, 255], [70, 184, 160],
[163, 0, 255], [153, 0, 255], [71, 255, 0], [255, 0, 163],
[255, 204, 0], [255, 0, 143], [0, 255, 235], [133, 255, 0],
[255, 0, 235], [245, 0, 255], [255, 0, 122], [255, 245, 0],
[10, 190, 212], [214, 255, 0], [0, 204, 255], [20, 0, 255],
[255, 255, 0], [0, 153, 255], [0, 41, 255], [0, 255, 204],
[41, 0, 255], [41, 255, 0], [173, 0, 255], [0, 245, 255],
[71, 0, 255], [122, 0, 255], [0, 255, 184], [0, 92, 255],
[184, 255, 0], [0, 133, 255], [255, 214, 0], [25, 194, 194],
[102, 255, 0], [92, 0, 255]]
@prompts(name="Segmentation On Image",
description="useful when you want to detect segmentations of the image. "
"like: segment this image, or generate segmentations on this image, "
"or perform segmentation on this image. "
"The input to this tool should be a string, representing the image_path")
def inference(self, inputs):
image = Image.open(inputs)
pixel_values = self.image_processor(image, return_tensors="pt").pixel_values
with torch.no_grad():
outputs = self.image_segmentor(pixel_values)
seg = self.image_processor.post_process_semantic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) # height, width, 3
palette = np.array(self.ade_palette)
for label, color in enumerate(palette):
color_seg[seg == label, :] = color
color_seg = color_seg.astype(np.uint8)
segmentation = Image.fromarray(color_seg)
updated_image_path = get_new_image_name(inputs, func_name="segmentation")
segmentation.save(updated_image_path)
print(f"\nProcessed Image2Seg, Input Image: {inputs}, Output Pose: {updated_image_path}")
return updated_image_path
class SegText2Image:
def __init__(self, device):
print(f"Initializing SegText2Image to {device}")
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-seg",
torch_dtype=self.torch_dtype)
self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
torch_dtype=self.torch_dtype)
self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
self.pipe.to(device)
self.seed = -1
self.a_prompt = 'best quality, extremely detailed'
self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit,' \
' fewer digits, cropped, worst quality, low quality'
@prompts(name="Generate Image Condition On Segmentations",
description="useful when you want to generate a new real image from both the user description and segmentations. "
"like: generate a real image of a object or something from this segmentation image, "
"or generate a new real image of a object or something from these segmentations. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the user description")
def inference(self, inputs):
image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
image = Image.open(image_path)
self.seed = random.randint(0, 65535)
seed_everything(self.seed)
prompt = f'{instruct_text}, {self.a_prompt}'
image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
guidance_scale=9.0).images[0]
updated_image_path = get_new_image_name(image_path, func_name="segment2image")
image.save(updated_image_path)
print(f"\nProcessed SegText2Image, Input Seg: {image_path}, Input Text: {instruct_text}, "
f"Output Image: {updated_image_path}")
return updated_image_path
class Image2Depth:
def __init__(self, device):
print("Initializing Image2Depth")
self.depth_estimator = pipeline('depth-estimation')
@prompts(name="Predict Depth On Image",
description="useful when you want to detect depth of the image. like: generate the depth from this image, "
"or detect the depth map on this image, or predict the depth for this image. "
"The input to this tool should be a string, representing the image_path")
def inference(self, inputs):
image = Image.open(inputs)
depth = self.depth_estimator(image)['depth']
depth = np.array(depth)
depth = depth[:, :, None]
depth = np.concatenate([depth, depth, depth], axis=2)
depth = Image.fromarray(depth)
updated_image_path = get_new_image_name(inputs, func_name="depth")
depth.save(updated_image_path)
print(f"\nProcessed Image2Depth, Input Image: {inputs}, Output Depth: {updated_image_path}")
return updated_image_path
class DepthText2Image:
def __init__(self, device):
print(f"Initializing DepthText2Image to {device}")
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.controlnet = ControlNetModel.from_pretrained(
"fusing/stable-diffusion-v1-5-controlnet-depth", torch_dtype=self.torch_dtype)
self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
torch_dtype=self.torch_dtype)
self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
self.pipe.to(device)
self.seed = -1
self.a_prompt = 'best quality, extremely detailed'
self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit,' \
' fewer digits, cropped, worst quality, low quality'
@prompts(name="Generate Image Condition On Depth",
description="useful when you want to generate a new real image from both the user description and depth image. "
"like: generate a real image of a object or something from this depth image, "
"or generate a new real image of a object or something from the depth map. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the user description")
def inference(self, inputs):
image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
image = Image.open(image_path)
self.seed = random.randint(0, 65535)
seed_everything(self.seed)
prompt = f'{instruct_text}, {self.a_prompt}'
image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
guidance_scale=9.0).images[0]
updated_image_path = get_new_image_name(image_path, func_name="depth2image")
image.save(updated_image_path)
print(f"\nProcessed DepthText2Image, Input Depth: {image_path}, Input Text: {instruct_text}, "
f"Output Image: {updated_image_path}")
return updated_image_path
class Image2Normal:
def __init__(self, device):
print("Initializing Image2Normal")
self.depth_estimator = pipeline("depth-estimation", model="Intel/dpt-hybrid-midas")
self.bg_threhold = 0.4
@prompts(name="Predict Normal Map On Image",
description="useful when you want to detect norm map of the image. "
"like: generate normal map from this image, or predict normal map of this image. "
"The input to this tool should be a string, representing the image_path")
def inference(self, inputs):
image = Image.open(inputs)
original_size = image.size
image = self.depth_estimator(image)['predicted_depth'][0]
image = image.numpy()
image_depth = image.copy()
image_depth -= np.min(image_depth)
image_depth /= np.max(image_depth)
x = cv2.Sobel(image, cv2.CV_32F, 1, 0, ksize=3)
x[image_depth < self.bg_threhold] = 0
y = cv2.Sobel(image, cv2.CV_32F, 0, 1, ksize=3)
y[image_depth < self.bg_threhold] = 0
z = np.ones_like(x) * np.pi * 2.0
image = np.stack([x, y, z], axis=2)
image /= np.sum(image ** 2.0, axis=2, keepdims=True) ** 0.5
image = (image * 127.5 + 127.5).clip(0, 255).astype(np.uint8)
image = Image.fromarray(image)
image = image.resize(original_size)
updated_image_path = get_new_image_name(inputs, func_name="normal-map")
image.save(updated_image_path)
print(f"\nProcessed Image2Normal, Input Image: {inputs}, Output Depth: {updated_image_path}")
return updated_image_path
class NormalText2Image:
def __init__(self, device):
print(f"Initializing NormalText2Image to {device}")
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.controlnet = ControlNetModel.from_pretrained(
"fusing/stable-diffusion-v1-5-controlnet-normal", torch_dtype=self.torch_dtype)
self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
torch_dtype=self.torch_dtype)
self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
self.pipe.to(device)
self.seed = -1
self.a_prompt = 'best quality, extremely detailed'
self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit,' \
' fewer digits, cropped, worst quality, low quality'
@prompts(name="Generate Image Condition On Normal Map",
description="useful when you want to generate a new real image from both the user description and normal map. "
"like: generate a real image of a object or something from this normal map, "
"or generate a new real image of a object or something from the normal map. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the user description")
def inference(self, inputs):
image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
image = Image.open(image_path)
self.seed = random.randint(0, 65535)
seed_everything(self.seed)
prompt = f'{instruct_text}, {self.a_prompt}'
image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
guidance_scale=9.0).images[0]
updated_image_path = get_new_image_name(image_path, func_name="normal2image")
image.save(updated_image_path)
print(f"\nProcessed NormalText2Image, Input Normal: {image_path}, Input Text: {instruct_text}, "
f"Output Image: {updated_image_path}")
return updated_image_path
class VisualQuestionAnswering:
def __init__(self, device):
print(f"Initializing VisualQuestionAnswering to {device}")
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.device = device
self.processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
self.model = BlipForQuestionAnswering.from_pretrained(
"Salesforce/blip-vqa-base", torch_dtype=self.torch_dtype).to(self.device)
@prompts(name="Answer Question About The Image",
description="useful when you need an answer for a question based on an image. "
"like: what is the background color of the last image, how many cats in this figure, what is in this figure. "
"The input to this tool should be a comma separated string of two, representing the image_path and the question")
def inference(self, inputs):
image_path, question = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
raw_image = Image.open(image_path).convert('RGB')
inputs = self.processor(raw_image, question, return_tensors="pt").to(self.device, self.torch_dtype)
out = self.model.generate(**inputs)
answer = self.processor.decode(out[0], skip_special_tokens=True)
print(f"\nProcessed VisualQuestionAnswering, Input Image: {image_path}, Input Question: {question}, "
f"Output Answer: {answer}")
return answer
class InfinityOutPainting:
template_model = True # Add this line to show this is a template model.
def __init__(self, ImageCaptioning, ImageEditing, VisualQuestionAnswering):
self.llm = OpenAI(temperature=0)
self.ImageCaption = ImageCaptioning
self.ImageEditing = ImageEditing
self.ImageVQA = VisualQuestionAnswering
self.a_prompt = 'best quality, extremely detailed'
self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
'fewer digits, cropped, worst quality, low quality'
def get_BLIP_vqa(self, image, question):
inputs = self.ImageVQA.processor(image, question, return_tensors="pt").to(self.ImageVQA.device,
self.ImageVQA.torch_dtype)
out = self.ImageVQA.model.generate(**inputs)
answer = self.ImageVQA.processor.decode(out[0], skip_special_tokens=True)
print(f"\nProcessed VisualQuestionAnswering, Input Question: {question}, Output Answer: {answer}")
return answer
def get_BLIP_caption(self, image):
inputs = self.ImageCaption.processor(image, return_tensors="pt").to(self.ImageCaption.device,
self.ImageCaption.torch_dtype)
out = self.ImageCaption.model.generate(**inputs)
BLIP_caption = self.ImageCaption.processor.decode(out[0], skip_special_tokens=True)
return BLIP_caption
def check_prompt(self, prompt):
check = f"Here is a paragraph with adjectives. " \
f"{prompt} " \
f"Please change all plural forms in the adjectives to singular forms. "
return self.llm(check)
def get_imagine_caption(self, image, imagine):
BLIP_caption = self.get_BLIP_caption(image)
background_color = self.get_BLIP_vqa(image, 'what is the background color of this image')
style = self.get_BLIP_vqa(image, 'what is the style of this image')
imagine_prompt = f"let's pretend you are an excellent painter and now " \
f"there is an incomplete painting with {BLIP_caption} in the center, " \
f"please imagine the complete painting and describe it" \
f"you should consider the background color is {background_color}, the style is {style}" \
f"You should make the painting as vivid and realistic as possible" \
f"You can not use words like painting or picture" \
f"and you should use no more than 50 words to describe it"
caption = self.llm(imagine_prompt) if imagine else BLIP_caption
caption = self.check_prompt(caption)
print(f'BLIP observation: {BLIP_caption}, ChatGPT imagine to {caption}') if imagine else print(
f'Prompt: {caption}')
return caption
def resize_image(self, image, max_size=1000000, multiple=8):
aspect_ratio = image.size[0] / image.size[1]
new_width = int(math.sqrt(max_size * aspect_ratio))
new_height = int(new_width / aspect_ratio)
new_width, new_height = new_width - (new_width % multiple), new_height - (new_height % multiple)
return image.resize((new_width, new_height))
def dowhile(self, original_img, tosize, expand_ratio, imagine, usr_prompt):
old_img = original_img
while (old_img.size != tosize):
prompt = self.check_prompt(usr_prompt) if usr_prompt else self.get_imagine_caption(old_img, imagine)
crop_w = 15 if old_img.size[0] != tosize[0] else 0
crop_h = 15 if old_img.size[1] != tosize[1] else 0
old_img = ImageOps.crop(old_img, (crop_w, crop_h, crop_w, crop_h))
temp_canvas_size = (expand_ratio * old_img.width if expand_ratio * old_img.width < tosize[0] else tosize[0],
expand_ratio * old_img.height if expand_ratio * old_img.height < tosize[1] else tosize[
1])
temp_canvas, temp_mask = Image.new("RGB", temp_canvas_size, color="white"), Image.new("L", temp_canvas_size,
color="white")
x, y = (temp_canvas.width - old_img.width) // 2, (temp_canvas.height - old_img.height) // 2
temp_canvas.paste(old_img, (x, y))
temp_mask.paste(0, (x, y, x + old_img.width, y + old_img.height))
resized_temp_canvas, resized_temp_mask = self.resize_image(temp_canvas), self.resize_image(temp_mask)
image = self.ImageEditing.inpaint(prompt=prompt, image=resized_temp_canvas, mask_image=resized_temp_mask,
height=resized_temp_canvas.height, width=resized_temp_canvas.width,
num_inference_steps=50).images[0].resize(
(temp_canvas.width, temp_canvas.height), Image.ANTIALIAS)
image = blend_gt2pt(old_img, image)
old_img = image
return old_img
@prompts(name="Extend An Image",
description="useful when you need to extend an image into a larger image."
"like: extend the image into a resolution of 2048x1024, extend the image into 2048x1024. "
"The input to this tool should be a comma separated string of two, representing the image_path and the resolution of widthxheight")
def inference(self, inputs):
image_path, resolution = inputs.split(',')
width, height = resolution.split('x')
tosize = (int(width), int(height))
image = Image.open(image_path)
image = ImageOps.crop(image, (10, 10, 10, 10))
out_painted_image = self.dowhile(image, tosize, 4, True, False)
updated_image_path = get_new_image_name(image_path, func_name="outpainting")
out_painted_image.save(updated_image_path)
print(f"\nProcessed InfinityOutPainting, Input Image: {image_path}, Input Resolution: {resolution}, "
f"Output Image: {updated_image_path}")
return updated_image_path
#############################################New Tool#############################################
class Grounded_dino_sam_inpainting:
def __init__(self, device):
print(f"Initializing BLIP")
self.device = device
self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
self.blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
self.blip_model = BlipForConditionalGeneration.from_pretrained(
"Salesforce/blip-image-captioning-large", torch_dtype=self.torch_dtype
).to(self.device)
print(f"Initializing GroundingDINO")
self.dino_model = load_model(
model_config_path="GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py",
model_checkpoint_path="groundingdino_swint_ogc.pth",
device=self.device
)
print(f"Initializing Segment Anthing")
self.sam_model = build_sam(checkpoint="sam_vit_h_4b8939.pth").to(self.device)
print(f"Initializing Stable Diffusion")
self.sd_pipe = StableDiffusionInpaintPipeline.from_pretrained(
"runwayml/stable-diffusion-inpainting", torch_dtype=self.torch_dtype
).to(self.device)
@prompts(name="Get Photo Description",
description="useful when you want to know what is inside the photo. receives image_path as input. "
"The input to this tool should be a string, representing the image_path. ")
def inference_caption(self, image_path):
inputs = self.blip_processor(Image.open(image_path), return_tensors="pt").to(self.device, self.torch_dtype)
out = self.blip_model.generate(**inputs)
captions = self.blip_processor.decode(out[0], skip_special_tokens=True)
print(f"\nProcessed ImageCaptioning, Input Image: {image_path}, Output Text: {captions}")
return captions
def _detect_object(self, image_path, text_prompt, func_name):
image_pil, image = load_image(image_path)
boxes_filt, scores, pred_phrases = get_grounding_output(
self.dino_model, image, text_prompt, 0.3, 0.25, device=self.device
)
# use NMS to handle overlapped boxes
print(f"Before NMS: {boxes_filt.shape[0]} boxes")
nms_idx = torchvision.ops.nms(boxes_filt, scores, 0.5).numpy().tolist()
boxes_filt = boxes_filt[nms_idx]
pred_phrases = [pred_phrases[idx] for idx in nms_idx]
print(f"After NMS: {boxes_filt.shape[0]} boxes")
size = image_pil.size
pred_dict = {
"boxes": boxes_filt,
"size": [size[1], size[0]], # H,W
"labels": pred_phrases,
}
image_with_box = plot_boxes_to_image(image_pil, pred_dict)[0]
updated_image_path = get_new_image_name(image_path, func_name)
image_with_box.save(updated_image_path)
return updated_image_path
@prompts(name="Detect One Object In Image",
description="useful when you want to detect the specific object in the image. "
"like: detect the black dog in the image. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the description of specific object.")
def inference_detect_one_object(self, inputs):
image_path, text_prompt = inputs.split(',')
print(f"\nInput Text Prompt: {text_prompt}")
updated_image_path = self._detect_object(image_path, text_prompt, func_name="det-object")
print(f"Processed DetectOneObject, Input Image: {image_path}, Output Image: {updated_image_path}")
return updated_image_path
@prompts(name="Detect Multiple Objects In Image",
description="useful when you want to detect two or more specific objects in the image. "
"like: detect the black dog and white cat in the image. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the description of multiple specific objects. "
"Different description should be separated by symbol '&', "
"like 'black dog & white cat'. ")
def inference_detect_multi_object(self, inputs):
image_path, text_prompt = inputs.split(',')
processed_text_prompt = text_prompt.replace(' &', ',')
print(f"\nOriginal Text Prompt: {text_prompt}, Input Text Prompt: {processed_text_prompt}")
updated_image_path = self._detect_object(image_path, text_prompt, func_name="det-objects")
print(f"Processed DetectMultiObject, Input Image: {image_path}, Output Image: {updated_image_path}")
return updated_image_path
# modified from https://github.com/Cheems-Seminar/segment-anything-and-name-it/blob/58408f1e4e340f565c5ef6b0c71920cdcd30b213/chatbot.py#L1046
@prompts(name="Segment Anything in Image",
description="useful when you want to segment anything in the image. "
"like: segment anything in the image. "
"The input to this tool should be a string, representing the image_path. ")
def inference_segment_anything(self, image_path):
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
mask_generator = SamAutomaticMaskGenerator(self.sam_model)
anns = mask_generator.generate(image)
plt.figure(figsize=(10, 10))
plt.imshow(image)
sorted_anns = sorted(anns, key=(lambda x: x['area']), reverse=True)
ax = plt.gca()
ax.set_autoscale_on(False)
for ann in sorted_anns:
m = ann['segmentation']
img = np.ones((m.shape[0], m.shape[1], 3))
color_mask = np.random.random((1, 3)).tolist()[0]
for i in range(3):
img[:,:,i] = color_mask[i]
ax.imshow(np.dstack((img, m*0.35)))
plt.axis('off')
updated_image_path = get_new_image_name(image_path, func_name="seg-any")
plt.savefig(updated_image_path, bbox_inches='tight', dpi=300, pad_inches=0.0)
print(f"\nProcessed SegmentAnything, Input Image: {image_path}, Output Image: {updated_image_path}")
return updated_image_path
def _segment_object(self, image_path, text_prompt, func_name):
image_pil, image = load_image(image_path)
# run grounding dino model
boxes_filt, scores, pred_phrases = get_grounding_output(
self.dino_model, image, text_prompt, 0.25, 0.2, device=self.device
)
# initialize SAM
predictor = SamPredictor(self.sam_model)
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
predictor.set_image(image)
size = image_pil.size
H, W = size[1], size[0]
for i in range(boxes_filt.size(0)):
boxes_filt[i] = boxes_filt[i] * torch.Tensor([W, H, W, H])
boxes_filt[i][:2] -= boxes_filt[i][2:] / 2
boxes_filt[i][2:] += boxes_filt[i][:2]
boxes_filt = boxes_filt.cpu()
# use NMS to handle overlapped boxes
print(f"Before NMS: {boxes_filt.shape[0]} boxes")
nms_idx = torchvision.ops.nms(boxes_filt, scores, 0.5).numpy().tolist()
boxes_filt = boxes_filt[nms_idx]
pred_phrases = [pred_phrases[idx] for idx in nms_idx]
print(f"After NMS: {boxes_filt.shape[0]} boxes")
# generate mask
transformed_boxes = predictor.transform.apply_boxes_torch(boxes_filt, image.shape[:2])
masks, _, _ = predictor.predict_torch(
point_coords = None,
point_labels = None,
boxes = transformed_boxes.to(self.device),
multimask_output = False,
)
# remove the mask when area < area_thresh (in pixels)
new_masks = []
for mask in masks:
# reshape to be used in remove_small_regions()
mask = mask.cpu().numpy().squeeze()
mask, _ = remove_small_regions(mask, 100, mode="holes")
mask, _ = remove_small_regions(mask, 100, mode="islands")
new_masks.append(torch.as_tensor(mask).unsqueeze(0))
masks = torch.stack(new_masks, dim=0)
# add box and mask in the image
plt.figure(figsize=(10, 10))
plt.imshow(image)
for mask in masks:
show_mask(mask.cpu().numpy(), plt.gca(), random_color=True)
for box, label in zip(boxes_filt, pred_phrases):
show_box(box.numpy(), plt.gca(), label)
plt.axis('off')
updated_image_path = get_new_image_name(image_path, func_name)
plt.savefig(updated_image_path, bbox_inches='tight', dpi=300, pad_inches=0.0)
return updated_image_path, pred_phrases
@prompts(name="Segment One Object In Image",
description="useful when you want to segment the specific object in the image. "
"like: segment the black dog in the image, or mask the black dog in the image. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the description of specific object.")
def inference_segment_one_object(self, inputs):
image_path, text_prompt = inputs.split(',')
print(f"\nInput Text Prompt: {text_prompt}")
updated_image_path, _ = self._segment_object(image_path, text_prompt, func_name="seg-object")
print(f"Processed SegmentOneObject, Input Image: {image_path}, Output Image: {updated_image_path}")
return updated_image_path
@prompts(name="Segment Multiple Object In Image",
description="useful when you want to segment two or more specific objects in the image. "
"like: segment the black dog and white cat in the image. "
"The input to this tool should be a comma separated string of two, "
"representing the image_path and the description of multiple specific objects. "
"Different description should be separated by symbol '&', "
"like 'black dog & white cat'. ")
def inference_segment_multi_object(self, inputs):
image_path, text_prompt = inputs.split(',')
processed_text_prompt = text_prompt.replace(' &', ',')
print("\nOriginal Text Prompt: {text_prompt}, Input Text Prompt: {processed_text_prompt}, ")
updated_image_path, _ = self._segment_object(image_path, text_prompt, func_name="seg-objects")
print(f"Processed SegmentMultiObject, Input Image: {image_path}, Output Image: {updated_image_path}")
return updated_image_path
@prompts(name="Auto Label the Image",
description="useful when you want to label the image automatically. "
"like: help me label the image. "
"The input to this tool should be a string, representing the image_path. ")
def inference_auto_segment_object(self, image_path):
inputs = self.blip_processor(Image.open(image_path), return_tensors="pt").to(self.device, self.torch_dtype)
out = self.blip_model.generate(**inputs)
caption = self.blip_processor.decode(out[0], skip_special_tokens=True)
text_prompt = generate_tags(caption, split=",")
print(f"\nCaption: {caption}")
print(f"Tags: {text_prompt}")
updated_image_path, pred_phrases = self._segment_object(image_path, text_prompt, func_name="auto-label")
caption = check_caption(caption, pred_phrases)
print(f"Revise caption with number: {caption}")
print(f"Processed SegmentMultiObject, Input Image: {image_path}, Caption: {caption}, "
f"Text Prompt: {text_prompt}, Output Image: {updated_image_path}")
return updated_image_path
def _inpainting(self, image_path, to_be_replaced_txt, replace_with_txt, func_name):
image_pil, image = load_image(image_path)
# run grounding dino model
boxes_filt, scores, pred_phrases = get_grounding_output(
self.dino_model, image, to_be_replaced_txt, 0.3, 0.25, device=self.device
)
# initialize SAM
predictor = SamPredictor(self.sam_model)
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
predictor.set_image(image)
size = image_pil.size
H, W = size[1], size[0]
for i in range(boxes_filt.size(0)):
boxes_filt[i] = boxes_filt[i] * torch.Tensor([W, H, W, H])
boxes_filt[i][:2] -= boxes_filt[i][2:] / 2
boxes_filt[i][2:] += boxes_filt[i][:2]
boxes_filt = boxes_filt.cpu()
# generate mask
transformed_boxes = predictor.transform.apply_boxes_torch(boxes_filt, image.shape[:2])
masks, _, _ = predictor.predict_torch(
point_coords = None,
point_labels = None,
boxes = transformed_boxes.to(self.device),
multimask_output = False,
)
# inpainting pipeline
mask = masks[0][0].cpu().numpy() # simply choose the first mask, which will be refine in the future release
mask_pil = Image.fromarray(mask).resize((512, 512))
image_pil = Image.fromarray(image).resize((512, 512))
image = self.sd_pipe(prompt=replace_with_txt, image=image_pil, mask_image=mask_pil).images[0]
updated_image_path = get_new_image_name(image_path, func_name)
image.save(updated_image_path)
return updated_image_path
@prompts(name="Replace Something From The Photo",
description="useful when you want to replace an object from the object description or "
"location with another object from its description. "
"The input to this tool should be a comma separated string of three, "
"representing the image_path, the object to be replaced, the object to be replaced with ")
def inference_replace(self, inputs):
image_path, to_be_replaced_txt, replace_with_txt = inputs.split(",")
print(f"\nReplace {to_be_replaced_txt} to {replace_with_txt}")
updated_image_path = self._inpainting(image_path, to_be_replaced_txt, replace_with_txt, 'replace-something')
print(f"Processed ImageEditing, Input Image: {image_path}, Output Image: {updated_image_path}")
return updated_image_path
#############################################New Tool#############################################
class ConversationBot:
def __init__(self, load_dict):
# load_dict = {'VisualQuestionAnswering':'cuda:0', 'ImageCaptioning':'cuda:1',...}
print(f"Initializing VisualChatGPT, load_dict={load_dict}")
if 'ImageCaptioning' not in load_dict and 'Grounded_dino_sam_inpainting' not in load_dict:
raise ValueError("You have to load ImageCaptioning or Grounded_dino_sam_inpainting as a basic function for VisualChatGPT")
self.models = {}
# Load Basic Foundation Models
for class_name, device in load_dict.items():
self.models[class_name] = globals()[class_name](device=device)
# Load Template Foundation Models
for class_name, module in globals().items():
if getattr(module, 'template_model', False):
template_required_names = {k for k in inspect.signature(module.__init__).parameters.keys() if k!='self'}
loaded_names = set([type(e).__name__ for e in self.models.values()])
if template_required_names.issubset(loaded_names):
self.models[class_name] = globals()[class_name](
**{name: self.models[name] for name in template_required_names})
self.tools = []
for instance in self.models.values():
for e in dir(instance):
if e.startswith('inference'):
func = getattr(instance, e)
self.tools.append(Tool(name=func.name, description=func.description, func=func))
self.llm = OpenAI(temperature=0)
self.memory = ConversationBufferMemory(memory_key="chat_history", output_key='output')
def run_text(self, text, state):
self.agent.memory.buffer = cut_dialogue_history(self.agent.memory.buffer, keep_last_n_words=500)
res = self.agent({"input": text.strip()})
res['output'] = res['output'].replace("\\", "/")
response = re.sub('(image/[-\w]*.png)', lambda m: f'![](/file={m.group(0)})*{m.group(0)}*', res['output'])
state = state + [(text, response)]
print(f"\nProcessed run_text, Input text: {text}\nCurrent state: {state}\n"
f"Current Memory: {self.agent.memory.buffer}")
return state, state
def run_image(self, image, state, txt, lang):
# image_filename = os.path.join('image', f"{str(uuid.uuid4())[:8]}.png")
# print("======>Auto Resize Image...")
# img = Image.open(image.name)
# width, height = img.size
# ratio = min(512 / width, 512 / height)
# width_new, height_new = (round(width * ratio), round(height * ratio))
# width_new = int(np.round(width_new / 64.0)) * 64
# height_new = int(np.round(height_new / 64.0)) * 64
# img = img.resize((width_new, height_new))
# img = img.convert('RGB')
# img.save(image_filename)
# img.save(image_filename, "PNG")
# print(f"Resize image form {width}x{height} to {width_new}x{height_new}")
## Directly use original image for better results
suffix = image.name.split('.')[-1]
image_filename = os.path.join('image', f"{str(uuid.uuid4())[:8]}.{suffix}")
shutil.copy(image.name, image_filename)
if 'Grounded_dino_sam_inpainting' in self.models:
description = self.models['Grounded_dino_sam_inpainting'].inference_caption(image_filename)
else:
description = self.models['ImageCaptioning'].inference(image_filename)
if lang == 'Chinese':
Human_prompt = f'\nHuman: 提供一张名为 {image_filename}的图片。它的描述是: {description}。 这些信息帮助你理解这个图像,但是你应该使用工具来完成下面的任务,而不是直接从我的描述中想象。 如果你明白了, 说 \"收到\". \n'
AI_prompt = "收到。 "
else:
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'
AI_prompt = "Received. "
self.agent.memory.buffer = self.agent.memory.buffer + Human_prompt + 'AI: ' + AI_prompt
state = state + [(f"![](/file={image_filename})*{image_filename}*", AI_prompt)]
print(f"\nProcessed run_image, Input image: {image_filename}\nCurrent state: {state}\n"
f"Current Memory: {self.agent.memory.buffer}")
return state, state, f'{txt} {image_filename} '
def init_agent(self, openai_api_key, lang):
self.memory.clear() #clear previous history
if lang=='English':
PREFIX, FORMAT_INSTRUCTIONS, SUFFIX = VISUAL_CHATGPT_PREFIX, VISUAL_CHATGPT_FORMAT_INSTRUCTIONS, VISUAL_CHATGPT_SUFFIX
place = "Enter text and press enter, or upload an image"
label_clear = "Clear"
else:
PREFIX, FORMAT_INSTRUCTIONS, SUFFIX = VISUAL_CHATGPT_PREFIX_CN, VISUAL_CHATGPT_FORMAT_INSTRUCTIONS_CN, VISUAL_CHATGPT_SUFFIX_CN
place = "输入文字并回车,或者上传图片"
label_clear = "清除"
self.llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
self.agent = initialize_agent(
self.tools,
self.llm,
agent="conversational-react-description",
verbose=True,
memory=self.memory,
return_intermediate_steps=True,
agent_kwargs={'prefix': PREFIX, 'format_instructions': FORMAT_INSTRUCTIONS, 'suffix': SUFFIX}, )
return gr.update(visible = True), gr.update(visible = True)
whisper_model = whisper.load_model("base").to('cuda:0')
def speech_recognition(speech_file):
# whisper
# load audio and pad/trim it to fit 30 seconds
audio = whisper.load_audio(speech_file)
audio = whisper.pad_or_trim(audio)
# make log-Mel spectrogram and move to the same device as the model
mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device)
# detect the spoken language
_, probs = whisper_model.detect_language(mel)
speech_language = max(probs, key=probs.get)
print(f'\nDetect Language: {speech_language}')
# decode the audio
options = whisper.DecodingOptions(fp16 = False)
result = whisper.decode(whisper_model, mel, options)
print(result.text)
return result.text
if __name__ == '__main__':
load_dict = {'Grounded_dino_sam_inpainting': 'cuda:0'}
# load_dict = {'ImageCaptioning': 'cuda:0'}
bot = ConversationBot(load_dict)
with gr.Blocks(css="#chatbot {overflow:auto; height:500px;}") as demo:
gr.Markdown("<h3><center>ChatBot</center></h3>")
gr.Markdown(
"""This is a demo to the work [Grounded-Segment-Anything](https://github.com/IDEA-Research/Grounded-Segment-Anything).<br>
"""
)
with gr.Row():
lang = gr.Radio(choices=['Chinese', 'English'], value='English', label='Language')
openai_api_key_textbox = gr.Textbox(
placeholder="Paste your OpenAI API key here to start ChatBot(sk-...) and press Enter ↵️",
show_label=False,
lines=1,
type="password",
)
chatbot = gr.Chatbot(elem_id="chatbot", label="ChatBot")
state = gr.State([])
with gr.Row(visible=False) as input_raws:
with gr.Column(scale=0.7):
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter, or upload an image").style(container=False)
with gr.Column(scale=0.10, min_width=0):
run = gr.Button("🏃‍♂️Run")
with gr.Column(scale=0.10, min_width=0):
clear = gr.Button("🔄Clear️")
with gr.Column(scale=0.10, min_width=0):
btn = gr.UploadButton("🖼️Upload", file_types=["image"])
with gr.Row(visible=False, equal_height=True) as audio_raw:
with gr.Column(scale=0.85):
audio = gr.Audio(source="microphone", type="filepath", label="Just say it!")
with gr.Column(scale=0.15):
transcribe = gr.Button("Transcribe")
gr.Examples(
examples=[
"Describe this image",
"Detect the dog",
"Detect the dog and the cat",
"Segment anything",
"Segment the dog",
"Help me label the image",
"Replace the dog with a cat",
],
inputs=txt
)
openai_api_key_textbox.submit(bot.init_agent, [openai_api_key_textbox, lang], [input_raws, audio_raw])
transcribe.click(speech_recognition, inputs=[audio], outputs=[txt])
txt.submit(bot.run_text, [txt, state], [chatbot, state])
txt.submit(lambda: "", None, txt)
run.click(bot.run_text, [txt, state], [chatbot, state])
run.click(lambda: "", None, txt)
btn.upload(bot.run_image, [btn, state, txt, lang], [chatbot, state, txt])
clear.click(bot.memory.clear)
clear.click(lambda: [], None, chatbot)
clear.click(lambda: [], None, state)
demo.launch(server_name="0.0.0.0", server_port=10010)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/coding-lz/Grounded-Segment-Anything-MindIE.git
git@gitee.com:coding-lz/Grounded-Segment-Anything-MindIE.git
coding-lz
Grounded-Segment-Anything-MindIE
Grounded-Segment-Anything-MindIE
master

搜索帮助