script.py 2.0 KB

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