drakosfire commited on
Commit
3fe250d
1 Parent(s): a04510d

Ready to deplay as api only

Browse files
Dockerfile DELETED
@@ -1,161 +0,0 @@
1
- # Stage 1: Build Cuda toolkit
2
- <<<<<<< HEAD
3
- FROM ubuntu:22.04 as cuda-setup
4
-
5
-
6
- ARG DEBIAN_FRONTEND=noninteractive
7
-
8
- # Install necessary libraries including libxml2
9
- RUN apt-get update && \
10
- apt-get install -y gcc libxml2 && \
11
- apt-get clean && \
12
- rm -rf /var/lib/apt/lists/*
13
-
14
- COPY cuda_12.4.0_550.54.14_linux.run .
15
-
16
- # Install wget, download cuda-toolkit and run
17
- RUN chmod +x cuda_12.4.0_550.54.14_linux.run && \
18
- ./cuda_12.4.0_550.54.14_linux.run --silent --toolkit --override
19
-
20
- # Second Stage: Copy necessary CUDA directories install flash-attn
21
- FROM ubuntu:22.04 as base-layer
22
-
23
- # Copy the CUDA toolkit from the first stage
24
- COPY --from=cuda-setup /usr/local/cuda-12.4 /usr/local/cuda-12.4
25
-
26
- # Set environment variables to enable CUDA commands
27
- ENV PATH=/usr/local/cuda-12.4/bin:${PATH}
28
- ENV LD_LIBRARY_PATH=/usr/local/cuda-12.4/lib64:${LD_LIBRARY_PATH}
29
-
30
- # Install Python, pip, and virtualenv
31
- RUN apt-get update && \
32
- apt-get install -y python3 python3-pip python3-venv git && \
33
- apt-get clean && \
34
- rm -rf /var/lib/apt/lists/*
35
-
36
- # Create a virtual environment and install dependencies
37
- RUN python3 -m venv /venv
38
- ENV PATH="/venv/bin:$PATH"
39
- FROM ubuntu:22.04 as cuda-setup
40
-
41
-
42
- ARG DEBIAN_FRONTEND=noninteractive
43
-
44
- # Install necessary libraries including libxml2
45
- RUN apt-get update && \
46
- apt-get install -y gcc libxml2 && \
47
- apt-get clean && \
48
- rm -rf /var/lib/apt/lists/*
49
-
50
- COPY cuda_12.4.0_550.54.14_linux.run .
51
-
52
- # Install wget, download cuda-toolkit and run
53
- RUN chmod +x cuda_12.4.0_550.54.14_linux.run && \
54
- ./cuda_12.4.0_550.54.14_linux.run --silent --toolkit --override
55
-
56
- # Second Stage: Copy necessary CUDA directories install flash-attn
57
- FROM ubuntu:22.04 as base-layer
58
-
59
- # Copy the CUDA toolkit from the first stage
60
- COPY --from=cuda-setup /usr/local/cuda-12.4 /usr/local/cuda-12.4
61
-
62
- # Set environment variables to enable CUDA commands
63
- ENV PATH=/usr/local/cuda-12.4/bin:${PATH}
64
- ENV LD_LIBRARY_PATH=/usr/local/cuda-12.4/lib64:${LD_LIBRARY_PATH}
65
-
66
- # Install Python, pip, and virtualenv
67
- RUN apt-get update && \
68
- apt-get install -y python3 python3-pip python3-venv git && \
69
- apt-get clean && \
70
- rm -rf /var/lib/apt/lists/*
71
-
72
- # Create a virtual environment and install dependencies
73
- RUN python3 -m venv /venv
74
- ENV PATH="/venv/bin:$PATH"
75
-
76
- # Llama.cpp requires the ENV variable be set to signal the CUDA build and be built with the CMAKE variables from pip for python use
77
- ENV LLAMA_CUBLAS=1
78
- RUN pip install --no-cache-dir torch packaging wheel && \
79
- pip install flash-attn && \
80
- RUN pip install --no-cache-dir torch packaging wheel && \
81
- pip install flash-attn && \
82
- pip install gradio && \
83
- CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install llama_cpp_python==0.2.55 && \
84
- CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install llama_cpp_python==0.2.55 && \
85
- =======
86
- FROM drakosfire/cuda-base:latest as base-layer
87
-
88
- # Llama.cpp requires the ENV variable be set to signal the CUDA build and be built with the CMAKE variables from pip for python use
89
- ENV LLAMA_CUBLAS=1
90
- RUN apt-get update && \
91
- apt-get install -y python3 python3-pip python3-venv && \
92
- pip install gradio && \
93
- CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install llama-cpp-python && \
94
- >>>>>>> 9a956dd (Polished and launch to Hugging Face)
95
- pip install pillow && \
96
- pip install diffusers && \
97
- pip install accelerate && \
98
- pip install transformers && \
99
- <<<<<<< HEAD
100
- pip install peft && \
101
- pip install pip install PyGithub
102
-
103
-
104
- FROM ubuntu:22.04 as final-layer
105
-
106
- COPY --from=base-layer /usr/local/cuda-12.4 /usr/local/cuda-12.4
107
- COPY --from=base-layer /venv /venv
108
-
109
- ENV PATH=/usr/local/cuda-12.4/bin:/venv/bin:${PATH}
110
- ENV LD_LIBRARY_PATH=/usr/local/cuda-12.4/lib64:${LD_LIBRARY_PATH}
111
- ENV LLAMA_CPP_LIB=/venv/lib/python3.10/site-packages/llama_cpp/libllama.so
112
- ENV VIRTUAL_ENV=/venv
113
-
114
- # Install Python and create a user
115
- RUN apt-get update && apt-get install -y python3 python3-venv && apt-get clean && rm -rf /var/lib/apt/lists/* && \
116
- useradd -m -u 1000 user
117
-
118
-
119
- # Install Python and create a user
120
- RUN apt-get update && apt-get install -y python3 python3-venv && apt-get clean && rm -rf /var/lib/apt/lists/* && \
121
- useradd -m -u 1000 user
122
-
123
- ENV PATH="$VIRTUAL_ENV/bin:$PATH"
124
- # Set working directory and user
125
- COPY . /home/user/app
126
- # Set working directory and user
127
- COPY . /home/user/app
128
- WORKDIR /home/user/app
129
- RUN chown -R user:user /home/user/app/ && \
130
- mkdir -p /home/user/app/output && \
131
- chown -R user:user /home/user/app/image_temp && \
132
- chown -R user:user /home/user/app/output
133
- =======
134
- pip install peft
135
-
136
- FROM base-layer as final-layer
137
-
138
- RUN useradd -m -u 1000 user
139
-
140
- # mkdir -p /home/user/.cache && \
141
- # chmod 777 /home/user/.cache && \
142
- # chown -R user:user /home/user/app/
143
- # Set environment variables for copied builds of cuda and flash-attn in /venv
144
-
145
-
146
- ENV PATH=/usr/local/cuda-12.4/bin:/venv/bin:${PATH}
147
- ENV LD_LIBRARY_PATH=/usr/local/cuda-12.4/lib64:${LD_LIBRARY_PATH}
148
-
149
- ENV VIRTUAL_ENV=/venv
150
- RUN python3 -m venv $VIRTUAL_ENV
151
- ENV PATH="$VIRTUAL_ENV/bin:$PATH"
152
- # Set working directory and user
153
- WORKDIR /home/user/app
154
- >>>>>>> 9a956dd (Polished and launch to Hugging Face)
155
-
156
- USER user
157
-
158
- # Set the entrypoint
159
- EXPOSE 8000
160
-
161
- ENTRYPOINT ["python", "main.py"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ExampleCards/CarvedWoodenDinosaurToy.png ADDED

Git LFS Details

  • SHA256: aa2ca06939fa4db1c30631f01f389e40f2b3f8018e91cfbb127eed56f31e70e8
  • Pointer size: 131 Bytes
  • Size of remote file: 898 kB
ExampleCards/CookieMimic.png ADDED

Git LFS Details

  • SHA256: 10a137ac3cf8bcb509423f626788af61c27989847cd8c6b23bf36e59443a3bde
  • Pointer size: 132 Bytes
  • Size of remote file: 1.39 MB
ExampleCards/Ridiculouslycutekitten.png ADDED

Git LFS Details

  • SHA256: 863ef9235c94e60fd7176db237e3939f67c6a22bce3015712ff2336a49065f83
  • Pointer size: 131 Bytes
  • Size of remote file: 489 kB
README.md CHANGED
@@ -1,38 +1,31 @@
1
- <<<<<<< HEAD
2
- ---
3
- <<<<<<< HEAD
4
- license: mit
5
- title: Collectible Card Generator
6
- short_description: Use LLM and SD to make custom collectible cards
7
- app_file: main.py
8
- sdk: docker
9
- app_port: 8000
10
- ---
11
- =======
12
- title: CollectibleCardGenerator
13
- emoji: 🐠
14
- colorFrom: green
15
- colorTo: yellow
16
- sdk: docker
17
- pinned: false
18
- license: mit
19
- ---
20
-
21
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
22
- >>>>>>> f6d0508 (initial commit)
23
-
24
- ---
25
- title: CollectibleCardGenerator
26
- emoji: 🐠
27
- colorFrom: green
28
- colorTo: yellow
29
- sdk: docker
30
- pinned: false
31
- license: mit
32
- ---
33
-
34
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
35
- =======
36
- # CardGenerator
37
- Takes user input and generates a collectible card with custom or LLM generated text and image generation
38
- >>>>>>> 9a956dd (Polished and launch to Hugging Face)
 
1
+ # Drakosfire's Dungeons and Dragons Item Card Generator
2
+
3
+ Welcome to the Drakosfire's Dungeons and Dragons Item Card Generator! This innovative tool harnesses the power of AI to generate unique item cards for your D&D adventures.
4
+
5
+ ## Overview
6
+
7
+ This generator leverages an API call to [Replicate](https://replicate.com/) using Llama 3 70b, combined with a custom fine-tuned version of the Stable Diffusion SDXL model. You can find more about the specific modelthis project was based on at [Civitai](https://civitai.com/models/129681/sdxl-faetastic).
8
+
9
+ ## Key Features
10
+
11
+ - **Custom Fine-Tuning**: The backbone of this generator is a fine-tuned Stable Diffusion SDXL model, specifically optimized for generating high-quality, fantasy-themed images on a unique card border that is themed by your imagination.
12
+
13
+ - **Consistent Card Design**: To ensure each card maintains a uniform appearance, a LoRA (Locally Optimized Representation Approximation) technique was used. This involved training the model with a hand-crafted dataset of card border images, enabling the system to generate new cards with consistent text and image spaces where the generated text and images can be elegantly integrated.
14
+
15
+ ## How It Works
16
+
17
+ 1. Your intitial text along with the prompt is sent to Llama 3 70b to generate a structured python dictionary.
18
+ 2. This new text will populate in interactive text fields. If it isn't perfect you can edit the text to fit your item.
19
+ 3. THe final text field is the Stable Diffusion prompt, these generate like one sentence stories describing the scene of your item. This field can also be edited.
20
+ 4. **Image and Text Generation**: Now generate 4 card template without text and pick your favorite.
21
+ 5. Finally, add text to your favorite template.
22
+ 3. **Result**: The final product is a beautifully crafted D&D item card, ready for use in your gaming sessions.
23
+
24
+ ## Example Cards
25
+
26
+ Below are a few examples of the item cards generated using our tool:
27
+
28
+ ![Example Card 1](url-to-example-card1)
29
+ ![Example Card 2](url-to-example-card2)
30
+
31
+ We hope you enjoy enhancing your Dungeons and Dragons experience with this unique tool. Happy adventuring!
 
 
 
 
 
 
 
__pycache__/img2img.cpython-310.pyc CHANGED
Binary files a/__pycache__/img2img.cpython-310.pyc and b/__pycache__/img2img.cpython-310.pyc differ
 
__pycache__/main.cpython-310.pyc CHANGED
Binary files a/__pycache__/main.cpython-310.pyc and b/__pycache__/main.cpython-310.pyc differ
 
__pycache__/user_input.cpython-310.pyc CHANGED
Binary files a/__pycache__/user_input.cpython-310.pyc and b/__pycache__/user_input.cpython-310.pyc differ
 
__pycache__/utilities.cpython-310.pyc CHANGED
Binary files a/__pycache__/utilities.cpython-310.pyc and b/__pycache__/utilities.cpython-310.pyc differ
 
img2img.py CHANGED
@@ -5,69 +5,40 @@ import time
5
  import utilities as u
6
  import card_generator as card
7
  from PIL import Image
 
 
 
8
 
9
- pipe = None
10
  start_time = time.time()
11
- torch.backends.cuda.matmul.allow_tf32 = True
12
- model_path = "/media/drakosfire/Shared/models/stable-diffusion/card-generator-v1/card-generator-v1.safetensors"
13
- lora_path = "/media/drakosfire/Shared/models/stable-diffusion/card-generator-v1/blank-card-template-5.safetensors"
14
- detail_lora_path = "/media/drakosfire/Shared/models/stable-diffusion/card-generator-v1/add-detail-xl.safetensors"
15
- mimic_lora_path = :"/media/drakosfire/Shared/CardGenerator/models/stable-diffusion/Loras/EnvyMimicXL01.safetensors"
16
  temp_image_path = "./image_temp/"
17
- card_pre_prompt = " blank magic card,high resolution, detailed intricate high quality border, textbox, high quality detailed magnum opus drawing of a "
18
- negative_prompts = "text, words, numbers, letters"
19
- image_list = []
20
-
21
- class img_generator():
22
 
23
- def load_img_gen(self,prompt, item, mimic = None):
24
- print(f"Input Prompt = {prompt} + Item : {item}")
25
- prompt = card_pre_prompt + item + ' ' + prompt
26
- print(prompt)
27
-
28
-
29
- pipe = StableDiffusionXLImg2ImgPipeline.from_single_file(model_path,
30
- custom_pipeline="low_stable_diffusion",
31
- torch_dtype=torch.float16,
32
- variant="fp16").to("cuda")
33
- # Load LoRAs for controlling image
34
- #pipe.load_lora_weights(lora_path, weight_name = "blank-card-template-5.safetensors",adapter_name = 'blank-card-template')
35
- pipe.load_lora_weights(detail_lora_path, weight_name = "add-detail-xl.safetensors", adapter_name = "add-detail-xl")
36
-
37
- # If mimic keyword has been detected, load the mimic LoRA and set adapter values
38
- if mimic:
39
- print("MIMIC!")
40
- pipe.load_lora_weights(mimic_lora_path, weight_name = "EnvyMimicXL01.safetensors", adapter_name = "EnvyMimicXL")
41
- pipe.set_adapters(['blank-card-template', "add-detail-xl", "EnvyMimicXL"], adapter_weights = [0.9,0.9,1.0])
42
- else :
43
- pipe.set_adapters([ "add-detail-xl"], adapter_weights = [0.9])
44
- pipe.enable_vae_slicing()
45
- return pipe, prompt
46
 
47
- def preview_and_generate_image(self,x,pipe, prompt, user_input_template, item):
48
- img_start = time.time()
49
- image = pipe(prompt=prompt,
50
- strength = .9,
51
- guidance_scale = 5,
52
- image= user_input_template,
53
- negative_prompt = negative_prompts,
54
- num_inference_steps=40,
55
- height = 1024, width = 768).images[0]
56
-
57
- image = image.save(temp_image_path+str(x) + f"{item}.png")
58
- output_image_path = temp_image_path+str(x) + f"{item}.png"
59
- img_time = time.time() - img_start
60
- img_its = 50/img_time
61
- print(f"image gen time = {img_time} and {img_its} it/s")
62
-
63
- # Delete the image variable to keep VRAM open to load the LLM
64
- del image
65
- print(f"Memory after del {torch.cuda.memory_allocated()}")
66
- print(image_list)
67
- total_time = time.time() - start_time
68
- print(total_time)
69
 
70
- return output_image_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
 
73
 
 
5
  import utilities as u
6
  import card_generator as card
7
  from PIL import Image
8
+ import replicate
9
+ from pathlib import Path
10
+
11
 
 
12
  start_time = time.time()
 
 
 
 
 
13
  temp_image_path = "./image_temp/"
 
 
 
 
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ def preview_and_generate_image(num_images,sd_prompt, user_input_template, item):
18
+ # Replicate is expecting a path
19
+ card_template = Path(user_input_template[0][0])
20
+ print(card_template)
21
+ img_start = time.time()
22
+ output=replicate.run(
23
+ "drakosfire/card-generator-v1:4e46bb44e6444d104b58e34a6d2ab66833aa84ba0dfe84ae57ef63d8e15da467",
24
+ input={
25
+ "item":item,
26
+ "sd_prompt":sd_prompt,
27
+ "num_images":num_images,
28
+ "input_template":card_template
29
+
30
+ }
31
+ )
32
+
33
+
34
+ img_time = time.time() - img_start
35
+ img_its = 35/img_time
36
+ print(f"image gen time = {img_time} and {img_its} it/s")
37
+
38
+ total_time = time.time() - start_time
39
+ print(total_time)
40
+
41
+ return output
42
 
43
 
44
 
main.py CHANGED
@@ -1,6 +1,5 @@
 
1
  import img2img
2
- from img2img import img_generator
3
-
4
  import card_generator as card
5
  import utilities as u
6
  import ctypes
@@ -107,20 +106,20 @@ with gr.Blocks() as demo:
107
  def delete_temp_images():
108
  image_list = u.directory_contents('./image_temp')
109
  u.delete_files(image_list)
110
- img2img.image_list.clear()
111
 
112
  # Called when pressing button to generate image, updates gallery by returning the list of image URLs
113
  def generate_image_update_gallery(num_img, sd_prompt,item_name, built_template):
114
  delete_temp_images()
115
  print(f"sd_prompt is a {type(sd_prompt)}")
116
  image_list = []
117
- image_generator = img_generator()
118
- img_gen, prompt = image_generator.load_img_gen(sd_prompt, item_name)
119
  for x in range(num_img):
120
- preview = image_generator.preview_and_generate_image(x,img_gen, prompt, built_template, item_name)
121
  image_list.append(preview)
122
  yield image_list
123
- #generate_gallery.change(image_list)
124
  del preview
125
  u.reclaim_mem()
126
 
@@ -189,7 +188,8 @@ with gr.Blocks() as demo:
189
  object_fit = "contain",
190
  height = "auto",
191
  elem_id = "Template Gallery",
192
- interactive=True)
 
193
 
194
  seed_image_gallery.select(assign_img_path, outputs = selected_seed_image)
195
  built_template_gallery.upload(u.receive_upload, inputs=built_template_gallery, outputs= selected_seed_image)
@@ -258,7 +258,7 @@ with gr.Blocks() as demo:
258
  generate_final_item_card = gr.Button(value = "Add Text", elem_id = "Generate user card")
259
 
260
 
261
- card_gen_button.click(fn = generate_image_update_gallery, inputs =[num_image_to_generate,item_sd_prompt_output,item_name_output,built_template], outputs= generate_gallery)
262
  generate_gallery.select(assign_img_path, outputs = selected_generated_image)
263
 
264
  # Button logice calls function when button object is pressed, passing inputs and passing output to components
 
1
+ import replicate
2
  import img2img
 
 
3
  import card_generator as card
4
  import utilities as u
5
  import ctypes
 
106
  def delete_temp_images():
107
  image_list = u.directory_contents('./image_temp')
108
  u.delete_files(image_list)
109
+ u.image_list.clear()
110
 
111
  # Called when pressing button to generate image, updates gallery by returning the list of image URLs
112
  def generate_image_update_gallery(num_img, sd_prompt,item_name, built_template):
113
  delete_temp_images()
114
  print(f"sd_prompt is a {type(sd_prompt)}")
115
  image_list = []
116
+
117
+
118
  for x in range(num_img):
119
+ preview = img2img.preview_and_generate_image(x,sd_prompt, built_template, item_name)
120
  image_list.append(preview)
121
  yield image_list
122
+
123
  del preview
124
  u.reclaim_mem()
125
 
 
188
  object_fit = "contain",
189
  height = "auto",
190
  elem_id = "Template Gallery",
191
+ interactive=True,
192
+ type="filepath")
193
 
194
  seed_image_gallery.select(assign_img_path, outputs = selected_seed_image)
195
  built_template_gallery.upload(u.receive_upload, inputs=built_template_gallery, outputs= selected_seed_image)
 
258
  generate_final_item_card = gr.Button(value = "Add Text", elem_id = "Generate user card")
259
 
260
 
261
+ card_gen_button.click(fn = generate_image_update_gallery, inputs =[num_image_to_generate,item_sd_prompt_output,item_name_output,built_template_gallery], outputs= generate_gallery)
262
  generate_gallery.select(assign_img_path, outputs = selected_generated_image)
263
 
264
  # Button logice calls function when button object is pressed, passing inputs and passing output to components
models DELETED
@@ -1 +0,0 @@
1
- /media/drakosfire/Shared/models
 
 
user_input.py CHANGED
@@ -96,7 +96,7 @@ def prompt_user_input():
96
  print(image_path)
97
 
98
  card.render_text_on_card(user_card_image, output_dict)
99
- u.delete_files(img2img.image_list)
100
 
101
 
102
 
 
96
  print(image_path)
97
 
98
  card.render_text_on_card(user_card_image, output_dict)
99
+ u.delete_files(u.image_list)
100
 
101
 
102
 
utilities.py CHANGED
@@ -7,6 +7,8 @@ import requests
7
  import os
8
  from PIL import Image
9
  from pathlib import Path
 
 
10
  # Utility Functions to be called from all modules
11
 
12
  # Function to return a list of keys of a nested dictionary using it's key value (item or creature)
 
7
  import os
8
  from PIL import Image
9
  from pathlib import Path
10
+
11
+ image_list = []
12
  # Utility Functions to be called from all modules
13
 
14
  # Function to return a list of keys of a nested dictionary using it's key value (item or creature)