Advertisement
Guest User

HTMX generate images view

a guest
Dec 9th, 2024
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. {% extends "base.html" %}
  2. {% load static %}
  3. {% block head_title %}Generate Images{% endblock %}
  4. {% block content %}
  5. <div class="container mx-auto p-4" x-data="imageGenerator()">
  6.  
  7. <!-- Category Selection -->
  8. <div class="card bg-base-100 shadow-xl mb-4 overflow-hidden">
  9. <div class="card-body">
  10. <h1 class="text-2xl font-bold">Generate Images for {{ person_project.name }}</h1>
  11. <h2 class="card-title">Categories</h2>
  12. <div class="overflow-x-auto pb-2" style="scrollbar-width: thin;">
  13. <div class="inline-flex space-x-1">
  14. {% for category in categories %}
  15. <button
  16. class="btn btn-sm shrink-0 w-24 h-auto min-h-[2.5rem] normal-case text-xs whitespace-normal"
  17. :class="selectedCategory === '{{ category }}' ? 'btn-primary text-white' : 'btn-outline'"
  18. @click="selectCategory('{{ category }}')"
  19. hx-get="{% url 'sample_images' category.id person_project.id %}"
  20. hx-trigger="click"
  21. hx-target="#sampleImages"
  22. hx-vals='{"category": "{{ category }}"}'
  23. >
  24. <span class="block text-center w-full">{{ category.name }}</span>
  25. </button>
  26. {% endfor %}
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31.  
  32. <!-- Sample Images -->
  33. <div id="sampleImages" class="card bg-base-100 shadow-xl mb-4 overflow-hidden">
  34. <div class="card-body">
  35. <h2 class="card-title">Pick a category above to start</h2>
  36. <div class="overflow-x-auto whitespace-nowrap pb-2" style="scrollbar-width: thin;">
  37. <div class="inline-flex space-x-2">
  38. <!-- populated by HTMX -->
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. <!-- Main Section -->
  44. <div class="card bg-base-100 shadow-xl mb-4">
  45. <div class="card-body">
  46. <form hx-post="{% url 'generate_image' person_project.id %}"
  47. hx-target="#generatedImage"
  48. hx-swap="innerHTML"
  49. hx-indicator="#loading-indicator"
  50. hx-disabled-elt="find button">
  51. {% csrf_token %}
  52. <div class="form-control">
  53. <label class="label cursor-pointer justify-start">
  54. <input type="checkbox" name="upscale" class="checkbox checkbox-primary">
  55. <span class="label-text ml-2">Generate in HD <small>(HD for print and higher resolution, non HD for faster generation)</small></span>
  56. </label>
  57. </div>
  58. <div class="form-control">
  59. <label class="label">
  60. <span class="label-text">Image prompt</span>
  61. </label>
  62. <textarea
  63. name="prompt"
  64. placeholder="Describe the image you want to generate or select a sample image"
  65. x-model="prompt"
  66. class="textarea textarea-bordered w-full"
  67. rows="3"
  68. id="prompt-input"
  69. ></textarea>
  70. </div>
  71. <div class="form-control mt-4">
  72.  
  73. <template x-if="customLora">
  74. <div class="flex gap-2">
  75. <div class="badge badge-primary badge-lg text-white" x-text="customLora"></div>
  76. <input type="hidden" name="custom_lora" x-model="customLora">
  77. </div>
  78. </template>
  79. </div>
  80. <div class="flex items-center gap-4">
  81. <button
  82. type="submit"
  83. class="btn btn-primary text-white mt-2"
  84. >
  85. <i data-lucide="wand-sparkles" class="w-6 h-6 mx-auto text-white"></i>
  86. Generate
  87. </button>
  88.  
  89. <!-- Inline loading indicator -->
  90. <div id="loading-indicator" class="htmx-indicator flex items-center">
  91. <img
  92. src="{% static 'mainapp/squid_peach_logo_text_250.png' %}"
  93. class="h-6 w-12 object-contain"
  94. style="animation: pulse-and-rotate 2s infinite;"
  95. >
  96. <span class="ml-2 text-sm">Creating...<br>
  97. <small>It should take less than 1 minute...</small></span>
  98. </div>
  99. </div>
  100. <p class="text-sm text-gray-400 mt-2">Every generation is unique, even with the same prompt!</p>
  101. </form>
  102.  
  103. <!-- Container for generated images -->
  104. <div id="generatedImage" class="w-full mt-4">
  105. <!-- Generated image will be displayed here -->
  106. </div>
  107. <div id="generatedImagesGallery" class="card bg-base-100 shadow-xl"
  108. hx-post="{% url 'generated_images_gallery' person_project.id %}"
  109. hx-trigger="load"
  110. hx-indicator="#loading-gallery-indicator"
  111. hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'>
  112. <!-- Gallery will be loaded via HTMX -->
  113. </div>
  114. <div id="loading-gallery-indicator" class="htmx-indicator">
  115. <div class="flex items-center justify-center p-4">
  116. <span class="loading loading-spinner loading-lg text-primary"></span>
  117. <span class="text-lg ml-4">Loading generated images...</span>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. </div>
  123.  
  124. <script>
  125. function imageGenerator() {
  126. return {
  127. selectedCategory: '',
  128. prompt: '',
  129. customLora: '',
  130. selectCategory(category) {
  131. this.selectedCategory = category;
  132. },
  133. setPrompt(newPrompt, newCustomLora) {
  134. // Decode Unicode escape sequences
  135. this.prompt = JSON.parse('"' + newPrompt.replace(/\"/g, '\\"') + '"');
  136. this.customLora = newCustomLora || '';
  137. }
  138. }
  139. }
  140. </script>
  141. {% endblock %}
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement