script.py 1.8 KB

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