From 58e6734308237c54a77377fa1c6bed41528b6eaf Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Sat, 25 Feb 2023 00:26:15 -0300 Subject: [PATCH] Add the "send picture" extension file --- extensions/send_pictures/script.py | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 extensions/send_pictures/script.py diff --git a/extensions/send_pictures/script.py b/extensions/send_pictures/script.py new file mode 100644 index 00000000..de961b9d --- /dev/null +++ b/extensions/send_pictures/script.py @@ -0,0 +1,56 @@ +import base64 +from io import BytesIO + +import gradio as gr +import modules.chat as chat +import modules.shared as shared +from modules.bot_picture import caption_image + +params = { +} + +input_hijack = { + 'state': False, + 'value': [] +} + +def generate_chat_picture(picture, name1, name2): + text = f'*{name1} sends {name2} a picture that contains the following: "{caption_image(picture)}"*' + buffer = BytesIO() + picture.save(buffer, format="JPEG") + img_str = base64.b64encode(buffer.getvalue()).decode('utf-8') + visible_text = f'' + return text, visible_text + +def input_modifier(string): + """ + This function is applied to your text inputs before + they are fed into the model. + """ + + return string + +def output_modifier(string): + """ + This function is applied to the model outputs. + """ + + return string + +def bot_prefix_modifier(string): + """ + This function is only applied in chat mode. It modifies + the prefix text for the Bot and can be used to bias its + behavior. + """ + + return string + +def ui(): + picture_select = gr.Image(label='Send a picture', type='pil') + + function_call = 'chat.cai_chatbot_wrapper' if shared.args.cai_chat else 'chat.chatbot_wrapper' + picture_select.upload(lambda picture, name1, name2: input_hijack.update({"state": True, "value": generate_chat_picture(picture, name1, name2)}), [picture_select, shared.gradio['name1'], shared.gradio['name2']], None) + picture_select.upload(eval(function_call), shared.input_params, shared.gradio['display'], show_progress=shared.args.no_stream) + picture_select.upload(lambda : None, [], [picture_select], show_progress=False) + #parser.add_argument('--picture', action='store_true', help='Adds an ability to send pictures in chat UI modes. Captions are generated by BLIP.')