script.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import gradio as gr
  2. import os
  3. # get the current directory of the script
  4. current_dir = os.path.dirname(os.path.abspath(__file__))
  5. # check if the bias_options.txt file exists, if not, create it
  6. bias_file = os.path.join(current_dir, "bias_options.txt")
  7. if not os.path.isfile(bias_file):
  8. with open(bias_file, "w") as f:
  9. f.write("*I am so happy*\n*I am so sad*\n*I am so excited*\n*I am so bored*\n*I am so angry*")
  10. # read bias options from the text file
  11. with open(bias_file, "r") as f:
  12. bias_options = [line.strip() for line in f.readlines()]
  13. params = {
  14. "activate": True,
  15. "bias string": " *I am so happy*",
  16. "use custom string": False,
  17. }
  18. def input_modifier(string):
  19. """
  20. This function is applied to your text inputs before
  21. they are fed into the model.
  22. """
  23. return string
  24. def output_modifier(string):
  25. """
  26. This function is applied to the model outputs.
  27. """
  28. return string
  29. def bot_prefix_modifier(string):
  30. """
  31. This function is only applied in chat mode. It modifies
  32. the prefix text for the Bot and can be used to bias its
  33. behavior.
  34. """
  35. if params['activate']:
  36. if params['use custom string']:
  37. return f'{string} {params["custom string"].strip()} '
  38. else:
  39. return f'{string} {params["bias string"].strip()} '
  40. else:
  41. return string
  42. def ui():
  43. # Gradio elements
  44. activate = gr.Checkbox(value=params['activate'], label='Activate character bias')
  45. dropdown_string = gr.Dropdown(choices=bias_options, value=params["bias string"], label='Character bias', info='To edit the options in this dropdown edit the "bias_options.txt" file')
  46. use_custom_string = gr.Checkbox(value=False, label='Use custom bias textbox instead of dropdown')
  47. custom_string = gr.Textbox(value="", placeholder="Enter custom bias string", label="Custom Character Bias", info='To use this textbox activate the checkbox above')
  48. # Event functions to update the parameters in the backend
  49. def update_bias_string(x):
  50. if x:
  51. params.update({"bias string": x})
  52. else:
  53. params.update({"bias string": dropdown_string.get()})
  54. return x
  55. def update_custom_string(x):
  56. params.update({"custom string": x})
  57. dropdown_string.change(update_bias_string, dropdown_string, None)
  58. custom_string.change(update_custom_string, custom_string, None)
  59. activate.change(lambda x: params.update({"activate": x}), activate, None)
  60. use_custom_string.change(lambda x: params.update({"use custom string": x}), use_custom_string, None)
  61. # Group elements together depending on the selected option
  62. def bias_string_group():
  63. if use_custom_string.value:
  64. return gr.Group([use_custom_string, custom_string])
  65. else:
  66. return dropdown_string