MCPcopy Create free account
hub / github.com/chenfei-wu/TaskMatrix / Text2Image

Class Text2Image

visual_chatgpt.py:275–298  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

273
274
275class Text2Image:
276 def __init__(self, device):
277 print(f"Initializing Text2Image to {device}")
278 self.device = device
279 self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
280 self.pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5",
281 torch_dtype=self.torch_dtype)
282 self.pipe.to(device)
283 self.a_prompt = 'best quality, extremely detailed'
284 self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
285 'fewer digits, cropped, worst quality, low quality'
286
287 @prompts(name="Generate Image From User Input Text",
288 description="useful when you want to generate an image from a user input text and save it to a file. "
289 "like: generate an image of an object or something, or generate an image that includes some objects. "
290 "The input to this tool should be a string, representing the text used to generate image. ")
291 def inference(self, text):
292 image_filename = os.path.join('image', f"{str(uuid.uuid4())[:8]}.png")
293 prompt = text + ', ' + self.a_prompt
294 image = self.pipe(prompt, negative_prompt=self.n_prompt).images[0]
295 image.save(image_filename)
296 print(
297 f"\nProcessed Text2Image, Input Text: {text}, Output Image: {image_filename}")
298 return image_filename
299
300
301class ImageCaptioning:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected