Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,7 +33,7 @@ parsed_descriptions_queue = deque()
|
|
| 33 |
|
| 34 |
# Usage limits
|
| 35 |
MAX_DESCRIPTIONS = 30
|
| 36 |
-
MAX_IMAGES =
|
| 37 |
|
| 38 |
# Preload models and checkpoints
|
| 39 |
print("Preloading models and checkpoints...")
|
|
@@ -90,7 +90,9 @@ def generate_description_prompt(user_prompt, text_generator):
|
|
| 90 |
try:
|
| 91 |
generated_text = text_generator(injected_prompt, max_length=max_length, num_return_sequences=1, truncation=True)[0]['generated_text']
|
| 92 |
generated_descriptions = re.findall(r'\[([^\[\]]+)\]', generated_text) # Extract descriptions enclosed in brackets
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
print(f"Error generating descriptions: {e}")
|
| 96 |
return None
|
|
@@ -100,22 +102,33 @@ def format_descriptions(descriptions):
|
|
| 100 |
return formatted_descriptions
|
| 101 |
|
| 102 |
@spaces.GPU
|
| 103 |
-
def generate_descriptions(user_prompt, seed_words_input, batch_size=100, max_iterations=
|
| 104 |
-
descriptions =
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
return list(parsed_descriptions_queue)[:MAX_IMAGES]
|
| 108 |
|
| 109 |
@spaces.GPU(duration=120)
|
| 110 |
-
def generate_images(parsed_descriptions, max_iterations=
|
| 111 |
-
# Limit the number of descriptions passed to the image generator to MAX_IMAGES (
|
| 112 |
if len(parsed_descriptions) > MAX_IMAGES:
|
| 113 |
parsed_descriptions = parsed_descriptions[:MAX_IMAGES]
|
| 114 |
|
| 115 |
images = []
|
| 116 |
for prompt in parsed_descriptions:
|
| 117 |
try:
|
| 118 |
-
images.extend(pipe(prompt, num_inference_steps=4, height=1024, width=1024).images) # Set resolution to
|
| 119 |
except Exception as e:
|
| 120 |
print(f"Error generating image for prompt '{prompt}': {e}")
|
| 121 |
|
|
@@ -140,4 +153,4 @@ if __name__ == '__main__':
|
|
| 140 |
allow_flagging='never' # Disable flagging
|
| 141 |
)
|
| 142 |
|
| 143 |
-
interface.launch(share=True)
|
|
|
|
| 33 |
|
| 34 |
# Usage limits
|
| 35 |
MAX_DESCRIPTIONS = 30
|
| 36 |
+
MAX_IMAGES = 4 # Limit to 4 images
|
| 37 |
|
| 38 |
# Preload models and checkpoints
|
| 39 |
print("Preloading models and checkpoints...")
|
|
|
|
| 90 |
try:
|
| 91 |
generated_text = text_generator(injected_prompt, max_length=max_length, num_return_sequences=1, truncation=True)[0]['generated_text']
|
| 92 |
generated_descriptions = re.findall(r'\[([^\[\]]+)\]', generated_text) # Extract descriptions enclosed in brackets
|
| 93 |
+
# Filter descriptions to ensure they are at least 4 words long
|
| 94 |
+
filtered_descriptions = [desc for desc in generated_descriptions if len(desc.split()) >= 4]
|
| 95 |
+
return filtered_descriptions if filtered_descriptions else None
|
| 96 |
except Exception as e:
|
| 97 |
print(f"Error generating descriptions: {e}")
|
| 98 |
return None
|
|
|
|
| 102 |
return formatted_descriptions
|
| 103 |
|
| 104 |
@spaces.GPU
|
| 105 |
+
def generate_descriptions(user_prompt, seed_words_input, batch_size=100, max_iterations=2): # Set max_iterations to 2
|
| 106 |
+
descriptions = []
|
| 107 |
+
for _ in range(2): # Perform two iterations
|
| 108 |
+
new_descriptions = generate_description_prompt(user_prompt, text_generator)
|
| 109 |
+
if new_descriptions:
|
| 110 |
+
descriptions.extend(new_descriptions)
|
| 111 |
+
# Pick a random description to feed back into the seed bank for subject
|
| 112 |
+
random_description = random.choice(new_descriptions)
|
| 113 |
+
seed_words.append(random_description)
|
| 114 |
+
|
| 115 |
+
# Limit the number of descriptions to MAX_IMAGES (4)
|
| 116 |
+
if len(descriptions) > MAX_IMAGES:
|
| 117 |
+
descriptions = descriptions[:MAX_IMAGES]
|
| 118 |
+
|
| 119 |
+
parsed_descriptions_queue.extend(descriptions)
|
| 120 |
return list(parsed_descriptions_queue)[:MAX_IMAGES]
|
| 121 |
|
| 122 |
@spaces.GPU(duration=120)
|
| 123 |
+
def generate_images(parsed_descriptions, max_iterations=4): # Set max_iterations to 4
|
| 124 |
+
# Limit the number of descriptions passed to the image generator to MAX_IMAGES (4)
|
| 125 |
if len(parsed_descriptions) > MAX_IMAGES:
|
| 126 |
parsed_descriptions = parsed_descriptions[:MAX_IMAGES]
|
| 127 |
|
| 128 |
images = []
|
| 129 |
for prompt in parsed_descriptions:
|
| 130 |
try:
|
| 131 |
+
images.extend(pipe(prompt, num_inference_steps=4, height=1024, width=1024).images) # Set resolution to 1024 x 1024
|
| 132 |
except Exception as e:
|
| 133 |
print(f"Error generating image for prompt '{prompt}': {e}")
|
| 134 |
|
|
|
|
| 153 |
allow_flagging='never' # Disable flagging
|
| 154 |
)
|
| 155 |
|
| 156 |
+
interface.launch(share=True)
|