-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmain.py
707 lines (620 loc) · 36 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
import os
import random
import re
import sys
import uuid
import cv2
import einops
import gradio as gr
import numpy as np
import pandas as pd
import torch
import whisper
from ControlNet.annotator.openpose import OpenposeDetector
from ControlNet.annotator.uniformer import UniformerDetector
from ControlNet.annotator.util import HWC3, resize_image
from ControlNet.cldm.ddim_hacked import DDIMSampler
from ControlNet.cldm.model import create_model, load_state_dict
from diffusers import (EulerAncestralDiscreteScheduler,
StableDiffusionInstructPix2PixPipeline,
StableDiffusionPipeline)
from langchain.agents import load_tools
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
from ldm.util import instantiate_from_config
from omegaconf import OmegaConf
from PIL import Image
from pytorch_lightning import seed_everything
from transformers import (AutoModelForCausalLM, AutoTokenizer,
BlipForConditionalGeneration,
BlipForQuestionAnswering, BlipProcessor,
CLIPSegForImageSegmentation, CLIPSegProcessor,
pipeline)
from TTS.api import TTS
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
AGENT_CHAIN_PREFIX = """AgentChain is designed to be able to assist with a wide range of text, visual and audio
related tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of
topics. AgentChain 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.
AgentChain is able to process and understand large amounts of text, images and audios. As a language model,
AgentChain can not directly read images or audio, but it has a list of tools to finish different visual, text, audio,
math and reasoning tasks. Each image will have a file name formed as "image/xxx.png", and AgentChain can invoke
different tools to indirectly understand pictures. Each audio will have a file name formed as "audio/xxx.wav",
and AgentChain can invoke different tools to indirectly understand audio. When talking about audio, AgentChain is very
strict to the file name and will never fabricate nonexistent files. When using tools to generate new image files,
AgentChain 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. AgentChain 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 AgentChain with a description. The description helps AgentChain to understand this
image, but AgentChain should use tools to finish following tasks, rather than directly imagine from the description.
Overall, AgentChain 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:
------
AgentChain has access to the following tools:"""
AGENT_CHAIN_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]
```
"""
AGENT_CHAIN_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 AgentChain is a text language model, AgentChain must use tools to observe images or audio rather than
imagination. The thoughts and observations are only visible for AgentChain, AgentChain should remember to repeat
important information in the final response for Human. Thought: Do I need to use a tool? {agent_scratchpad}"""
def cut_dialogue_history(history_memory, keep_last_n_words=500):
tokens = history_memory.split()
n_tokens = len(tokens)
print(f"hitory_memory:{history_memory}, n_tokens: {n_tokens}")
if n_tokens < keep_last_n_words:
return history_memory
else:
paragraphs = history_memory.split('\n')
last_n_tokens = n_tokens
while last_n_tokens >= keep_last_n_words:
last_n_tokens = 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())[0:4]
if len(name_split) == 1:
most_org_file_name = name_split[0]
recent_prev_file_name = name_split[0]
new_file_name = '{}_{}_{}_{}.png'.format(this_new_uuid, func_name, recent_prev_file_name, most_org_file_name)
else:
assert len(name_split) == 4
most_org_file_name = name_split[3]
recent_prev_file_name = name_split[0]
new_file_name = '{}_{}_{}_{}.png'.format(this_new_uuid, func_name, recent_prev_file_name, most_org_file_name)
return os.path.join(head, new_file_name)
def create_model(config_path, device):
config = OmegaConf.load(config_path)
OmegaConf.update(config, "model.params.cond_stage_config.params.device", device)
model = instantiate_from_config(config.model).cpu()
print(f'Loaded model config from [{config_path}]')
return model
class MaskFormer:
def __init__(self, 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(image.size)
class Pix2Pix:
def __init__(self, device):
print("Initializing Pix2Pix to %s" % device)
self.device = device
self.pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained("timbrooks/instruct-pix2pix",
torch_dtype=torch.float16,
safety_checker=None).to(device)
self.pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(self.pipe.scheduler.config)
def inference(self, inputs):
"""Change style of image."""
print("===>Starting Pix2Pix Inference")
image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
original_image = Image.open(image_path)
image = \
self.pipe(instruct_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)
return updated_image_path
class T2I:
def __init__(self, device):
print("Initializing T2I to %s" % device)
self.device = device
self.pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
self.text_refine_tokenizer = AutoTokenizer.from_pretrained("Gustavosta/MagicPrompt-Stable-Diffusion")
self.text_refine_model = AutoModelForCausalLM.from_pretrained("Gustavosta/MagicPrompt-Stable-Diffusion")
self.text_refine_gpt2_pipe = pipeline("text-generation", model=self.text_refine_model,
tokenizer=self.text_refine_tokenizer, device=self.device)
self.pipe.to(device)
def inference(self, text):
image_filename = os.path.join('image', str(uuid.uuid4())[0:8] + ".png")
refined_text = self.text_refine_gpt2_pipe(text)[0]["generated_text"]
print(f'{text} refined to {refined_text}')
image = self.pipe(refined_text).images[0]
image.save(image_filename)
print(f"Processed T2I.run, text: {text}, image_filename: {image_filename}")
return image_filename
class ImageCaptioning:
def __init__(self, device):
print("Initializing ImageCaptioning to %s" % device)
self.device = device
self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
self.model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(
self.device)
def inference(self, image_path):
inputs = self.processor(Image.open(image_path), return_tensors="pt").to(self.device)
out = self.model.generate(**inputs)
captions = self.processor.decode(out[0], skip_special_tokens=True)
return captions
class image2pose:
def __init__(self):
print("Direct human pose.")
self.detector = OpenposeDetector()
self.resolution = 512
def inference(self, inputs):
print("===>Starting image2pose Inference")
image = Image.open(inputs)
image = np.array(image)
image = HWC3(image)
detected_map, _ = self.detector(resize_image(image, self.resolution))
detected_map = HWC3(detected_map)
image = resize_image(image, self.resolution)
H, W, C = image.shape
detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)
updated_image_path = get_new_image_name(inputs, func_name="human-pose")
image = Image.fromarray(detected_map)
image.save(updated_image_path)
return updated_image_path
class pose2image:
def __init__(self, device):
print("Initialize the pose2image model...")
model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)
model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_openpose.pth', location='cpu'))
self.model = model.to(device)
self.device = device
self.ddim_sampler = DDIMSampler(self.model)
self.ddim_steps = 20
self.image_resolution = 512
self.num_samples = 1
self.save_memory = False
self.strength = 1.0
self.guess_mode = False
self.scale = 9.0
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'
def inference(self, inputs):
print("===>Starting pose2image Inference")
image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
image = Image.open(image_path)
image = np.array(image)
prompt = instruct_text
img = resize_image(HWC3(image), self.image_resolution)
H, W, C = img.shape
img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)
control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0
control = torch.stack([control for _ in range(self.num_samples)], dim=0)
control = einops.rearrange(control, 'b h w c -> b c h w').clone()
self.seed = random.randint(0, 65535)
seed_everything(self.seed)
if self.save_memory:
self.model.low_vram_shift(is_diffusing=False)
cond = {"c_concat": [control], "c_crossattn": [
self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}
un_cond = {"c_concat": None if self.guess_mode else [control],
"c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}
shape = (4, H // 8, W // 8)
self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in
range(13)] if self.guess_mode else ([self.strength] * 13)
samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False,
eta=0., unconditional_guidance_scale=self.scale,
unconditional_conditioning=un_cond)
if self.save_memory:
self.model.low_vram_shift(is_diffusing=False)
x_samples = self.model.decode_first_stage(samples)
x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0,
255).astype(
np.uint8)
updated_image_path = get_new_image_name(image_path, func_name="pose2image")
real_image = Image.fromarray(x_samples[0]) # default the index0 image
real_image.save(updated_image_path)
return updated_image_path
class image2seg:
def __init__(self):
print("Direct segmentations.")
self.detector = UniformerDetector()
self.resolution = 512
def inference(self, inputs):
print("===>Starting image2seg Inference")
image = Image.open(inputs)
image = np.array(image)
image = HWC3(image)
detected_map = self.detector(resize_image(image, self.resolution))
detected_map = HWC3(detected_map)
image = resize_image(image, self.resolution)
H, W, C = image.shape
detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)
updated_image_path = get_new_image_name(inputs, func_name="segmentation")
image = Image.fromarray(detected_map)
image.save(updated_image_path)
return updated_image_path
class seg2image:
def __init__(self, device):
print("Initialize the seg2image model...")
model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)
model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_seg.pth', location='cpu'))
self.model = model.to(device)
self.device = device
self.ddim_sampler = DDIMSampler(self.model)
self.ddim_steps = 20
self.image_resolution = 512
self.num_samples = 1
self.save_memory = False
self.strength = 1.0
self.guess_mode = False
self.scale = 9.0
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'
def inference(self, inputs):
print("===>Starting seg2image Inference")
image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
image = Image.open(image_path)
image = np.array(image)
prompt = instruct_text
img = resize_image(HWC3(image), self.image_resolution)
H, W, C = img.shape
img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)
control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0
control = torch.stack([control for _ in range(self.num_samples)], dim=0)
control = einops.rearrange(control, 'b h w c -> b c h w').clone()
self.seed = random.randint(0, 65535)
seed_everything(self.seed)
if self.save_memory:
self.model.low_vram_shift(is_diffusing=False)
cond = {"c_concat": [control], "c_crossattn": [
self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}
un_cond = {"c_concat": None if self.guess_mode else [control],
"c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}
shape = (4, H // 8, W // 8)
self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in
range(13)] if self.guess_mode else ([self.strength] * 13)
samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False,
eta=0., unconditional_guidance_scale=self.scale,
unconditional_conditioning=un_cond)
if self.save_memory:
self.model.low_vram_shift(is_diffusing=False)
x_samples = self.model.decode_first_stage(samples)
x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0,
255).astype(
np.uint8)
updated_image_path = get_new_image_name(image_path, func_name="segment2image")
real_image = Image.fromarray(x_samples[0]) # default the index0 image
real_image.save(updated_image_path)
return updated_image_path
class BLIPVQA:
def __init__(self, device):
print("Initializing BLIP VQA to %s" % device)
self.device = device
self.processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
self.model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base").to(self.device)
def get_answer_from_question_and_image(self, inputs):
image_path, question = inputs.split(",")
raw_image = Image.open(image_path).convert('RGB')
print(F'BLIPVQA :question :{question}')
inputs = self.processor(raw_image, question, return_tensors="pt").to(self.device)
out = self.model.generate(**inputs)
answer = self.processor.decode(out[0], skip_special_tokens=True)
return answer
class Whisper:
def __init__(self, device):
print("Initializing Whisper on device", device)
self.model = whisper.load_model("medium.en", device=device)
def transcribe(self, inputs):
return self.model.transcribe(inputs)['text']
class coqui_tts:
def __init__(self, device):
self.device = device
self.tts = TTS('tts_models/multilingual/multi-dataset/your_tts', gpu=self.device)
def gen_speech_from_text(self, inputs):
print("===>Starting text2speech Inference")
filename = os.path.join('audio', str(uuid.uuid4())[:8] + ".wav")
self.tts.tts_to_file(text=inputs, speaker=self.tts.speakers[0], language=self.tts.languages[0],
file_path=filename)
return "Audio generated in " + filename
class TableQA:
def __init__(self, device):
self.device = device
self.pipeline = pipeline(task="table-question-answering", model="google/tapas-large-finetuned-wtq",
device=self.device)
def get_answer_from_question_and_table(self, inputs):
table_path = inputs.split(",")[0]
questions = inputs.split(",")[1:]
table = pd.read_csv(table_path, dtype=str)
res = self.pipeline(table=table, query=questions)
return res['answer']
class TwilioCaller:
def parse_input(self, inputs):
try:
if 'and' in inputs:
text: str = inputs.split("and")[0]
phone_number = inputs.split("and")[1]
elif ',' in inputs:
text: str = inputs.split(",")[0]
phone_number = inputs.split(",")[1:]
if isinstance(phone_number, list):
phone_number = ",".join(phone_number)
else:
raise Exception('Could not make the call, the input is not well formatted. Must be a comma separated string')
except:
raise Exception('Could not parse your input. Must be a comma separated string')
text = text.replace('"', '').strip(' ')
phone_number = phone_number.replace('"', '').strip(' ')
if not re.match('\+[0-9]+', text) and not re.match('\+[0-9]+', phone_number):
raise Exception('Could not make the call, no phone number provided')
if re.match('\+[0-9]+', text) and not re.match('\+[0-9]+', phone_number):
text, phone_number = phone_number, text
return text, phone_number
def call_with_text(self, inputs):
import twilio
try:
text, phone_number = self.parse_input(inputs)
except Exception as e:
return str(e)
from twilio_lib import call_with_text
try:
call_with_text(text, phone_number)
except twilio.base.exceptions.TwilioRestException:
return 'Internal error, could not submit the call.'
return 'Call submitted, it should be received soon'
def call_with_audio(self, inputs):
audio_filename = inputs.split(",")[0]
phone_number = inputs.split(",")[1:]
from twilio_lib import call_with_audio
call_with_audio(audio_filename, phone_number)
return 'Call submitted, it should be received soon'
class ConversationBot:
def __init__(self):
print("Initializing AgentChain")
self.llm = OpenAI(temperature=0)
self.i2t = ImageCaptioning(device="cuda:1") # 1755
self.t2i = T2I(device="cuda:1") # 6677
self.image2pose = image2pose()
self.pose2image = pose2image(device="cuda:1") # 6681
self.BLIPVQA = BLIPVQA(device="cuda:1") # 2709
self.image2seg = image2seg()
self.seg2image = seg2image(device="cuda:1") # 5540
## up until now, comsuming 23362 MB on GPU
self.pix2pix = Pix2Pix(device="cuda:0") # 2795
self.coqui_tts = coqui_tts(device=False)
self.tableQA = TableQA(device="cuda:0")
self.whisper = Whisper(device="cuda:0")
self.twilio_caller = TwilioCaller()
self.extra_tools = ["serpapi", "llm-math", "python_repl", "requests", "terminal"]
self.memory = ConversationBufferMemory(memory_key="chat_history", output_key='output')
self.tools = [
Tool(name="Get Photo Description", func=self.i2t.inference,
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. "),
Tool(name="Generate Image From User Input Text", func=self.t2i.inference,
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. "),
Tool(name="Instruct Image Using Text", func=self.pix2pix.inference,
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. "),
Tool(name="Answer Question About The Image", func=self.BLIPVQA.get_answer_from_question_and_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"),
Tool(name="Segmentation On Image", func=self.image2seg.inference,
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"),
Tool(name="Generate Image Condition On Segmentations", func=self.seg2image.inference,
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"),
Tool(name="Pose Detection On Image", func=self.image2pose.inference,
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"),
Tool(name="Generate Image Condition On Pose Image", func=self.pose2image.inference,
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"),
Tool(name="Generate Text from Audio", func=self.whisper.transcribe,
description="useful when you want to generate text from audio. like: generate text from this audio, "
"or transcribe this audio, or listen to this audio. receives audio_path as input."
"The input to this tool should be a string, representing the audio_path"),
Tool(name="Generate Speech From Text", func=self.coqui_tts.gen_speech_from_text,
description="useful when you want to generate a speech from a text. like: generate a speech from "
"this text, or say this text in audio. "
"The input to this tool should be a string, representing the text to be converted to "
"speech."
),
Tool(name="Answer Question About The table", func=self.tableQA.get_answer_from_question_and_table,
description="useful when you need an answer for a question based on a table. like: what is the "
"maximum of the column age, or what is the sum of row 5 from the following table."
"The input to this tool should be a comma separated string, representing the "
"table_path and the questions"),
Tool(name="Call a phone number with text", func=self.twilio_caller.call_with_text,
description="useful when you need to call a phone number with a text input. like: call +4917424393190 and"
" tell him \"happy birthday\". The input to this tool should be a comma separate string "
"representing the text_input and the phone_number"),
# Tool(name="Call a phone number with audio", func=self.twilio_caller.call_with_audio,
# description="useful when you need to call a phone number with an audio file. like: call +4917424393190 and"
# " using audio file audio/smth.wav. Only use audio files mentioned by the user."
# "The input to this tool should be a comma separated string representing the audio file name and the phone_number"),
]
self.tools = self.tools + load_tools(self.extra_tools, llm=self.llm)
self.agent = initialize_agent(
self.tools,
self.llm,
agent="conversational-react-description",
verbose=True,
memory=self.memory,
return_intermediate_steps=True,
agent_kwargs={'prefix': AGENT_CHAIN_PREFIX, 'format_instructions': AGENT_CHAIN_FORMAT_INSTRUCTIONS,
'suffix': AGENT_CHAIN_SUFFIX}, )
def run_text(self, text, state):
print("===============Running run_text =============")
print("Inputs:", text, state)
print("======>Previous memory:\n %s" % self.agent.memory)
self.agent.memory.buffer = cut_dialogue_history(self.agent.memory.buffer, keep_last_n_words=500)
res = self.agent({"input": text})
print("======>Current memory:\n %s" % self.agent.memory)
response = re.sub('(image/\S*png)', lambda m: f'![](/file={m.group(0)})*{m.group(0)}*', res['output'])
audio_files = re.findall('(audio/\S*wav)', response)
if len(audio_files) > 0:
audio = audio_files[0]
else:
audio = None
state = state + [(text, response)]
print("Outputs:", state)
return state, state, audio
def run_image(self, image, state, txt):
print("===============Running run_image =============")
print("Inputs:", image, state)
print("======>Previous memory:\n %s" % self.agent.memory)
image_filename = os.path.join('image', str(uuid.uuid4())[0: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))
img = img.resize((width_new, height_new))
img = img.convert('RGB')
img.save(image_filename, "PNG")
print(f"Resize image form {width}x{height} to {width_new}x{height_new}")
description = self.i2t.inference(image_filename)
Human_prompt = "\nHuman: provide a figure named {}. The description is: {}. 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".format(
image_filename, description)
AI_prompt = "Received. "
self.agent.memory.buffer = self.agent.memory.buffer + Human_prompt + 'AI: ' + AI_prompt
print("======>Current memory:\n %s" % self.agent.memory)
state = state + [(f"![](/file={image_filename})*{image_filename}*", AI_prompt)]
print("Outputs:", state)
return state, state, txt + ' ' + image_filename + ' '
def run_audio(self, audio, state, txt):
print("===============Running run_audio =============")
print("Inputs:", audio, state)
print("======>Previous memory:\n %s" % self.agent.memory)
audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav")
import shutil
shutil.copyfile(audio, audio_filename)
transcribed_text = self.whisper.transcribe(audio_filename)
Human_prompt = "\nHuman: provide audio named {}. The description is: {}. This information helps you to " \
"understand this audio, but you should use tools to finish following tasks, " \
"rather than directly imagine from my description. If you understand, say \"Received\". \n".format(
audio_filename, transcribed_text)
AI_prompt = "Received. "
self.agent.memory.buffer = self.agent.memory.buffer + Human_prompt + 'AI: ' + AI_prompt
print("======>Current memory:\n %s" % self.agent.memory)
state = state + [(f"![](/file={audio_filename})*{audio_filename}*", AI_prompt)]
print("Outputs:", state)
return state, audio, state, txt + ' ' + audio_filename + ' '
def run_df(self, df, state, txt):
print("===============Running run_df =============")
print("Inputs:", df, state)
print("======>Previous memory:\n %s" % self.agent.memory)
csv_filename = os.path.join('csv', str(uuid.uuid4())[0:8] + ".csv")
df.to_csv(csv_filename, index=False)
Human_prompt = "\nHuman: provided a csv file named {}. You can specifically use the tool \"Answer Question About The table\" to understand this file. If you understand, say \"Received\". \n".format(
csv_filename)
AI_prompt = "Received. "
self.agent.memory.buffer = self.agent.memory.buffer + Human_prompt + 'AI: ' + AI_prompt
print("======>Current memory:\n %s" % self.agent.memory)
state = state + [(f"![](/file={csv_filename})*{csv_filename}*", AI_prompt)]
print("Outputs:", state)
return state, state, txt + ' ' + csv_filename + ' '
if __name__ == '__main__':
bot = ConversationBot()
with gr.Blocks(css="#chatbot .overflow-y-auto{height:500px}") as demo:
chatbot = gr.Chatbot(elem_id="chatbot", label="AgentChain")
state = gr.State([])
with gr.Row():
with gr.Column(scale=0.8):
txt = gr.Textbox(placeholder="Enter text and press enter", label='Instruct with text').style(
container=False)
with gr.Column(scale=0.2, min_width=0):
btn = gr.UploadButton("Upload Image", file_types=["image"])
with gr.Row():
with gr.Column(scale=0.5, min_width=0):
with gr.Row():
with gr.Column(scale=0.5, min_width=0):
input_audio = gr.Audio(source="upload", type="filepath", label="Upload Audio Input")
with gr.Column(scale=0.5, min_width=0):
output_audio = gr.Audio(type="filepath", label='Audio output', interactive=False)
with gr.Row():
clear = gr.Button("Clear Chat History")
with gr.Column(scale=0.5, min_width=0):
with gr.Row():
df = gr.DataFrame(interactive=True, row_count=1, col_count=1, headers=['Column1'], label="Give a Dataframe as input")
with gr.Row():
with gr.Column(scale=0.8, min_width=0):
persist_df = gr.Button("Upload the dataframe")
input_audio.upload(bot.run_audio, [input_audio, state, txt], [chatbot, output_audio, state, txt])
# audio.upload(bot.run_audio, [audio, state, txt], [chatbot, audio, state, txt])
txt.submit(bot.run_text, [txt, state], [chatbot, state, output_audio])
txt.submit(lambda: "", None, txt)
btn.upload(bot.run_image, [btn, state, txt], [chatbot, state, txt])
persist_df.click(bot.run_df, [df, state, txt], [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=7861)