prompt_helper.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (C) 2025 AIDC-AI
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. """
  13. Prompt helper utilities
  14. Simple utilities for building prompts with optional prefixes.
  15. """
  16. def build_image_prompt(prompt: str, prefix: str = "") -> str:
  17. """
  18. Build final image prompt with optional prefix
  19. Args:
  20. prompt: User's raw prompt
  21. prefix: Optional prefix to add before the prompt
  22. Returns:
  23. Final prompt with prefix applied (if provided)
  24. Examples:
  25. >>> build_image_prompt("a cat", "")
  26. 'a cat'
  27. >>> build_image_prompt("a cat", "anime style")
  28. 'anime style, a cat'
  29. >>> build_image_prompt("a cat", " anime style ")
  30. 'anime style, a cat'
  31. """
  32. prefix = prefix.strip() if prefix else ""
  33. prompt = prompt.strip() if prompt else ""
  34. if prefix and prompt:
  35. return f"{prefix}, {prompt}"
  36. elif prefix:
  37. return prefix
  38. else:
  39. return prompt