Spaces:
Running
Running
update code
Browse files- app.py +76 -40
- builtin_plan.json +1 -15
- cllm/agents/tog/interpretor.py +3 -2
- cllm/agents/tog/responser.py +1 -1
- cllm/services/nlp/api.py +2 -2
- cllm/services/nlp/llms/chat_models.py +4 -1
app.py
CHANGED
@@ -17,11 +17,12 @@ import whisper
|
|
17 |
import fire
|
18 |
import gradio as gr
|
19 |
import gradio.themes.base as ThemeBase
|
20 |
-
from gradio.themes.utils import colors,
|
21 |
-
import
|
22 |
import sys
|
23 |
|
24 |
sys.path.append(os.getcwd())
|
|
|
25 |
from cllm.agents.builtin import plans
|
26 |
from cllm.services.general.api import remote_logging
|
27 |
from cllm.agents import container, FILE_EXT
|
@@ -199,6 +200,38 @@ class InteractionLoop:
|
|
199 |
return self.add_audio(history, audio, role=role, append=append)
|
200 |
return history, ""
|
201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
def add_text(self, history, text, role="assistant", append=False):
|
203 |
if history is None:
|
204 |
return history, ""
|
@@ -326,10 +359,7 @@ class InteractionLoop:
|
|
326 |
for result_per_step, executed_solutions, wrapped_outputs in results:
|
327 |
tool_name = json.dumps(result_per_step[0], ensure_ascii=False)
|
328 |
args = json.dumps(result_per_step[1], ensure_ascii=False)
|
329 |
-
|
330 |
-
ret = f"Internal error: {result_per_step[2]}"
|
331 |
-
else:
|
332 |
-
ret = json.dumps(result_per_step[2], ensure_ascii=False)
|
333 |
history, _ = self.add_text(
|
334 |
history,
|
335 |
f"Call **{tool_name}:**<br> **Args**: {plain2md(args)}<br> **Ret**: {plain2md(ret)}",
|
@@ -552,37 +582,43 @@ def app(controller="cllm.agents.tog.Controller", https=False, **kwargs):
|
|
552 |
|
553 |
with gr.Column(scale=5):
|
554 |
with gr.Tabs():
|
555 |
-
with gr.Tab("
|
556 |
-
|
557 |
-
sources="upload",
|
558 |
interactive=True,
|
559 |
type="filepath",
|
560 |
)
|
561 |
-
# with gr.Row():
|
562 |
-
# mask_submit_btn = gr.Button("Segment", variant="primary")
|
563 |
with gr.Row():
|
564 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
565 |
|
566 |
with gr.Tab("Plan"):
|
567 |
planbot = gr.JSON(elem_classes="json")
|
568 |
|
569 |
with gr.Tab("Memory"):
|
570 |
memory_table = gr.DataFrame(
|
571 |
-
# value=convert_dict_to_frame(builtin_resources),
|
572 |
label="Memory",
|
573 |
headers=["Resource", "Type"],
|
574 |
row_count=5,
|
575 |
wrap=True,
|
576 |
)
|
577 |
-
gr.Examples(
|
578 |
-
[
|
579 |
-
osp.join("./assets/resources", item)
|
580 |
-
for item in builtin_resources.keys()
|
581 |
-
if item.endswith(".png")
|
582 |
-
],
|
583 |
-
inputs=[image_mask],
|
584 |
-
label="File Examples",
|
585 |
-
)
|
586 |
|
587 |
chatbot.like(
|
588 |
loop.vote,
|
@@ -595,14 +631,8 @@ def app(controller="cllm.agents.tog.Controller", https=False, **kwargs):
|
|
595 |
chatbot,
|
596 |
],
|
597 |
)
|
598 |
-
reply_inputs = [user_state,
|
599 |
-
reply_outputs = [
|
600 |
-
user_state,
|
601 |
-
# image_mask,
|
602 |
-
chatbot,
|
603 |
-
memory_table,
|
604 |
-
# planbot,
|
605 |
-
]
|
606 |
|
607 |
add_text = [
|
608 |
partial(loop.add_text, role="human"),
|
@@ -633,7 +663,7 @@ def app(controller="cllm.agents.tog.Controller", https=False, **kwargs):
|
|
633 |
upload_btn.upload(
|
634 |
loop.add_file,
|
635 |
inputs=[user_state, chatbot, upload_btn],
|
636 |
-
outputs=[user_state,
|
637 |
)
|
638 |
record.click(
|
639 |
on_switch_input,
|
@@ -641,7 +671,7 @@ def app(controller="cllm.agents.tog.Controller", https=False, **kwargs):
|
|
641 |
[state_input, text, audio],
|
642 |
)
|
643 |
|
644 |
-
|
645 |
loop.save_point, [user_state, chatbot], [user_state, chatbot]
|
646 |
).then(loop.plan, reply_inputs, reply_inputs).then(
|
647 |
loop.execute, reply_inputs, reply_inputs
|
@@ -649,22 +679,28 @@ def app(controller="cllm.agents.tog.Controller", https=False, **kwargs):
|
|
649 |
loop.reply, [user_state, chatbot], reply_outputs
|
650 |
)
|
651 |
|
652 |
-
|
653 |
loop.add_file,
|
654 |
-
inputs=[user_state, chatbot,
|
655 |
-
outputs=[user_state,
|
656 |
)
|
657 |
-
|
658 |
loop.add_file,
|
659 |
-
inputs=[user_state, chatbot,
|
660 |
-
outputs=[user_state,
|
|
|
|
|
|
|
|
|
|
|
|
|
661 |
)
|
662 |
|
663 |
if https:
|
664 |
demo.queue().launch(
|
665 |
server_name="0.0.0.0",
|
666 |
-
|
667 |
-
|
668 |
ssl_verify=False,
|
669 |
show_api=False,
|
670 |
allowed_paths=[
|
|
|
17 |
import fire
|
18 |
import gradio as gr
|
19 |
import gradio.themes.base as ThemeBase
|
20 |
+
from gradio.themes.utils import colors, sizes
|
21 |
+
from gradio.components.image_editor import Brush
|
22 |
import sys
|
23 |
|
24 |
sys.path.append(os.getcwd())
|
25 |
+
|
26 |
from cllm.agents.builtin import plans
|
27 |
from cllm.services.general.api import remote_logging
|
28 |
from cllm.agents import container, FILE_EXT
|
|
|
200 |
return self.add_audio(history, audio, role=role, append=append)
|
201 |
return history, ""
|
202 |
|
203 |
+
def add_sketch(self, user_state, history, sketch):
|
204 |
+
if user_state.get("resources", None) is None:
|
205 |
+
user_state["resources"] = OrderedDict()
|
206 |
+
|
207 |
+
if sketch is None or "layers" not in sketch:
|
208 |
+
return user_state, None, history, None
|
209 |
+
|
210 |
+
mask = None
|
211 |
+
for layer in sketch["layers"]:
|
212 |
+
alpha = layer[:, :, 3:] // 255
|
213 |
+
if mask is None:
|
214 |
+
mask = np.ones_like(layer[:, :, :3]) * 255
|
215 |
+
mask = mask * (1 - alpha) + layer[:, :, :3] * alpha
|
216 |
+
|
217 |
+
ext = "png"
|
218 |
+
r_type = "scribble"
|
219 |
+
new_filename = self._gen_new_name(r_type, ext)
|
220 |
+
saved_path = get_real_path(new_filename)
|
221 |
+
if ext in FILE_EXT["image"]:
|
222 |
+
Image.fromarray(mask).save(saved_path, "png")
|
223 |
+
user_state["sketch_image"] = new_filename
|
224 |
+
|
225 |
+
logger.info(f"add file: {saved_path}")
|
226 |
+
user_state["resources"][new_filename] = r_type
|
227 |
+
history, _ = self.add_text(history, (saved_path,), role="human", append=False)
|
228 |
+
history, _ = self.add_text(
|
229 |
+
history, f"Recieved file {new_filename}", role="assistant", append=False
|
230 |
+
)
|
231 |
+
memory = convert_dict_to_frame(user_state["resources"])
|
232 |
+
|
233 |
+
return user_state, history, memory
|
234 |
+
|
235 |
def add_text(self, history, text, role="assistant", append=False):
|
236 |
if history is None:
|
237 |
return history, ""
|
|
|
359 |
for result_per_step, executed_solutions, wrapped_outputs in results:
|
360 |
tool_name = json.dumps(result_per_step[0], ensure_ascii=False)
|
361 |
args = json.dumps(result_per_step[1], ensure_ascii=False)
|
362 |
+
ret = json.dumps(result_per_step[2], ensure_ascii=False)
|
|
|
|
|
|
|
363 |
history, _ = self.add_text(
|
364 |
history,
|
365 |
f"Call **{tool_name}:**<br> **Args**: {plain2md(args)}<br> **Ret**: {plain2md(ret)}",
|
|
|
582 |
|
583 |
with gr.Column(scale=5):
|
584 |
with gr.Tabs():
|
585 |
+
with gr.Tab("Click"):
|
586 |
+
click_image = gr.Image(
|
587 |
+
sources=["upload", "clipboard"],
|
588 |
interactive=True,
|
589 |
type="filepath",
|
590 |
)
|
|
|
|
|
591 |
with gr.Row():
|
592 |
+
click_image_submit_btn = gr.Button(
|
593 |
+
"Upload", variant="primary"
|
594 |
+
)
|
595 |
+
gr.Examples(
|
596 |
+
[
|
597 |
+
osp.join("./assets/resources", item)
|
598 |
+
for item in builtin_resources.keys()
|
599 |
+
if item.endswith(".png")
|
600 |
+
],
|
601 |
+
inputs=[click_image],
|
602 |
+
label="File Examples",
|
603 |
+
)
|
604 |
+
|
605 |
+
with gr.Tab("Draw"):
|
606 |
+
sketch = gr.Sketchpad(
|
607 |
+
sources=(), brush=Brush(colors=["#000000"])
|
608 |
+
)
|
609 |
+
with gr.Row():
|
610 |
+
sketch_submit_btn = gr.Button("Upload", variant="primary")
|
611 |
|
612 |
with gr.Tab("Plan"):
|
613 |
planbot = gr.JSON(elem_classes="json")
|
614 |
|
615 |
with gr.Tab("Memory"):
|
616 |
memory_table = gr.DataFrame(
|
|
|
617 |
label="Memory",
|
618 |
headers=["Resource", "Type"],
|
619 |
row_count=5,
|
620 |
wrap=True,
|
621 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
622 |
|
623 |
chatbot.like(
|
624 |
loop.vote,
|
|
|
631 |
chatbot,
|
632 |
],
|
633 |
)
|
634 |
+
reply_inputs = [user_state, click_image, chatbot, planbot]
|
635 |
+
reply_outputs = [user_state, chatbot, memory_table]
|
|
|
|
|
|
|
|
|
|
|
|
|
636 |
|
637 |
add_text = [
|
638 |
partial(loop.add_text, role="human"),
|
|
|
663 |
upload_btn.upload(
|
664 |
loop.add_file,
|
665 |
inputs=[user_state, chatbot, upload_btn],
|
666 |
+
outputs=[user_state, click_image, chatbot, memory_table],
|
667 |
)
|
668 |
record.click(
|
669 |
on_switch_input,
|
|
|
671 |
[state_input, text, audio],
|
672 |
)
|
673 |
|
674 |
+
click_image.select(
|
675 |
loop.save_point, [user_state, chatbot], [user_state, chatbot]
|
676 |
).then(loop.plan, reply_inputs, reply_inputs).then(
|
677 |
loop.execute, reply_inputs, reply_inputs
|
|
|
679 |
loop.reply, [user_state, chatbot], reply_outputs
|
680 |
)
|
681 |
|
682 |
+
click_image.upload(
|
683 |
loop.add_file,
|
684 |
+
inputs=[user_state, chatbot, click_image],
|
685 |
+
outputs=[user_state, click_image, chatbot, memory_table],
|
686 |
)
|
687 |
+
click_image_submit_btn.click(
|
688 |
loop.add_file,
|
689 |
+
inputs=[user_state, chatbot, click_image],
|
690 |
+
outputs=[user_state, click_image, chatbot, memory_table],
|
691 |
+
)
|
692 |
+
|
693 |
+
sketch_submit_btn.click(
|
694 |
+
loop.add_sketch,
|
695 |
+
inputs=[user_state, chatbot, sketch],
|
696 |
+
outputs=[user_state, chatbot, memory_table],
|
697 |
)
|
698 |
|
699 |
if https:
|
700 |
demo.queue().launch(
|
701 |
server_name="0.0.0.0",
|
702 |
+
ssl_certfile="./certificate/cert.pem",
|
703 |
+
ssl_keyfile="./certificate/key.pem",
|
704 |
ssl_verify=False,
|
705 |
show_api=False,
|
706 |
allowed_paths=[
|
builtin_plan.json
CHANGED
@@ -1,15 +1 @@
|
|
1 |
-
{
|
2 |
-
"you know what I want": [
|
3 |
-
[
|
4 |
-
{
|
5 |
-
"tool_name": "text_to_image",
|
6 |
-
"inputs": {
|
7 |
-
"text": "a dog"
|
8 |
-
},
|
9 |
-
"outputs": [
|
10 |
-
"image"
|
11 |
-
]
|
12 |
-
}
|
13 |
-
]
|
14 |
-
]
|
15 |
-
}
|
|
|
1 |
+
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cllm/agents/tog/interpretor.py
CHANGED
@@ -94,13 +94,14 @@ class Interpretor:
|
|
94 |
zip(action.outputs, tool_outputs)
|
95 |
):
|
96 |
memory[arg_name] = arg_value
|
97 |
-
if arg_value is None:
|
98 |
outputs.append(arg_value)
|
|
|
99 |
wrapped_outputs.append(
|
100 |
auto_type(
|
101 |
arg_name,
|
102 |
DataType.TEXT,
|
103 |
-
|
104 |
)
|
105 |
)
|
106 |
continue
|
|
|
94 |
zip(action.outputs, tool_outputs)
|
95 |
):
|
96 |
memory[arg_name] = arg_value
|
97 |
+
if arg_value is None or isinstance(arg_value, Exception):
|
98 |
outputs.append(arg_value)
|
99 |
+
arg_value = f"{arg_value}"
|
100 |
wrapped_outputs.append(
|
101 |
auto_type(
|
102 |
arg_name,
|
103 |
DataType.TEXT,
|
104 |
+
arg_value,
|
105 |
)
|
106 |
)
|
107 |
continue
|
cllm/agents/tog/responser.py
CHANGED
@@ -56,7 +56,7 @@ def generate_response(user_input, solution, output_files):
|
|
56 |
logger.info("##### Response Generation #####")
|
57 |
logger.info(content)
|
58 |
|
59 |
-
chat = ChatOpenAI(model_name="gpt-3.5-turbo-
|
60 |
messages = [SystemMessage(content=content)]
|
61 |
output = chat(messages)
|
62 |
logger.info(output)
|
|
|
56 |
logger.info("##### Response Generation #####")
|
57 |
logger.info(content)
|
58 |
|
59 |
+
chat = ChatOpenAI(model_name="gpt-3.5-turbo-16k")
|
60 |
messages = [SystemMessage(content=content)]
|
61 |
output = chat(messages)
|
62 |
logger.info(output)
|
cllm/services/nlp/api.py
CHANGED
@@ -34,7 +34,7 @@ __ALL__ = [
|
|
34 |
]
|
35 |
|
36 |
|
37 |
-
HOST =
|
38 |
PORT = os.environ.get("CLLM_SERVICES_PORT", 10056)
|
39 |
|
40 |
|
@@ -63,7 +63,7 @@ def question_answering_with_context(context: str, question: str, **kwargs):
|
|
63 |
|
64 |
|
65 |
def openai_chat_model(input_msg: str, **kwargs):
|
66 |
-
chat = ChatOpenAI()
|
67 |
chat_log = []
|
68 |
default_sys_msg = "Your name is ControlLLM, an AI-powered assistant developed by OpenGVLab from Shanghai AI Lab. You need to respond to user requests based on the following information."
|
69 |
sys_msg = kwargs.get("sys_msg", default_sys_msg)
|
|
|
34 |
]
|
35 |
|
36 |
|
37 |
+
HOST = "localhost"
|
38 |
PORT = os.environ.get("CLLM_SERVICES_PORT", 10056)
|
39 |
|
40 |
|
|
|
63 |
|
64 |
|
65 |
def openai_chat_model(input_msg: str, **kwargs):
|
66 |
+
chat = ChatOpenAI(model_name="gpt-3.5-turbo-16k")
|
67 |
chat_log = []
|
68 |
default_sys_msg = "Your name is ControlLLM, an AI-powered assistant developed by OpenGVLab from Shanghai AI Lab. You need to respond to user requests based on the following information."
|
69 |
sys_msg = kwargs.get("sys_msg", default_sys_msg)
|
cllm/services/nlp/llms/chat_models.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
import openai
|
3 |
import requests
|
4 |
from typing import (
|
@@ -39,6 +40,8 @@ class ChatOpenAI:
|
|
39 |
self.model_kwargs = model_kwargs
|
40 |
self.api_key = os.environ.get("OPENAI_API_KEY", openai_api_key)
|
41 |
self.base_url = os.environ.get("OPENAI_BASE_URL", openai_base_url)
|
|
|
|
|
42 |
|
43 |
def __call__(self, messages: List[BaseMessage], **kwargs):
|
44 |
stream = kwargs.get("stream", False)
|
@@ -210,7 +213,7 @@ class ChatLLAMA2(SimpleChatModel):
|
|
210 |
|
211 |
|
212 |
if __name__ == "__main__":
|
213 |
-
chat = ChatOpenAI()
|
214 |
msg = [
|
215 |
SystemMessage(content="You are a helpful assistant."),
|
216 |
HumanMessage(content="Hello!"),
|
|
|
1 |
import os
|
2 |
+
from turtle import mode
|
3 |
import openai
|
4 |
import requests
|
5 |
from typing import (
|
|
|
40 |
self.model_kwargs = model_kwargs
|
41 |
self.api_key = os.environ.get("OPENAI_API_KEY", openai_api_key)
|
42 |
self.base_url = os.environ.get("OPENAI_BASE_URL", openai_base_url)
|
43 |
+
# openai.api_key = self.api_key
|
44 |
+
# openai.base_url = self.base_url
|
45 |
|
46 |
def __call__(self, messages: List[BaseMessage], **kwargs):
|
47 |
stream = kwargs.get("stream", False)
|
|
|
213 |
|
214 |
|
215 |
if __name__ == "__main__":
|
216 |
+
chat = ChatOpenAI(model_name="gpt-3.5-turbo-1106")
|
217 |
msg = [
|
218 |
SystemMessage(content="You are a helpful assistant."),
|
219 |
HumanMessage(content="Hello!"),
|