script.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import base64
  2. from io import BytesIO
  3. import gradio as gr
  4. import modules.chat as chat
  5. import modules.shared as shared
  6. from modules.bot_picture import caption_image
  7. params = {
  8. }
  9. # If 'state' is 'temporary' or 'permanent', will hijack the next
  10. # chatbot wrapper call with a custom input text and optionally
  11. # custom output text
  12. input_hijack = {
  13. 'state': 'off',
  14. 'value': ["", ""]
  15. }
  16. def generate_chat_picture(picture, name1, name2):
  17. text = f'*{name1} sends {name2} a picture that contains the following: "{caption_image(picture)}"*'
  18. buffer = BytesIO()
  19. picture.save(buffer, format="JPEG")
  20. img_str = base64.b64encode(buffer.getvalue()).decode('utf-8')
  21. visible_text = f'<img src="data:image/jpeg;base64,{img_str}">'
  22. return text, visible_text
  23. def input_modifier(string):
  24. """
  25. This function is applied to your text inputs before
  26. they are fed into the model.
  27. """
  28. return string
  29. def output_modifier(string):
  30. """
  31. This function is applied to the model outputs.
  32. """
  33. return string
  34. def bot_prefix_modifier(string):
  35. """
  36. This function is only applied in chat mode. It modifies
  37. the prefix text for the Bot and can be used to bias its
  38. behavior.
  39. """
  40. return string
  41. def ui():
  42. picture_select = gr.Image(label='Send a picture', type='pil')
  43. function_call = 'chat.cai_chatbot_wrapper' if shared.args.cai_chat else 'chat.chatbot_wrapper'
  44. 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)
  45. picture_select.upload(eval(function_call), shared.input_params, shared.gradio['display'], show_progress=shared.args.no_stream)
  46. picture_select.upload(lambda : None, [], [picture_select], show_progress=False)
  47. #parser.add_argument('--picture', action='store_true', help='Adds an ability to send pictures in chat UI modes. Captions are generated by BLIP.')