Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,8 +19,12 @@ timeout = 100
|
|
| 19 |
# Initialize the prompt enhancer client
|
| 20 |
prompt_enhancer = Client("K00B404/mistral-nemo-prompt-enhancer")
|
| 21 |
|
| 22 |
-
def enhance_prompt(prompt):
|
| 23 |
"""Enhance the given prompt using the Mistral Nemo prompt enhancer."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
system_message = "You are an expert at writing detailed, high-quality image generation prompts. Enhance the given prompt by adding relevant artistic details, style elements, and quality descriptors. Keep the original intent but make it more elaborate and specific."
|
| 26 |
enhanced = prompt_enhancer.predict(
|
|
@@ -39,9 +43,9 @@ def enhance_prompt(prompt):
|
|
| 39 |
return prompt # Fall back to original prompt if enhancement fails
|
| 40 |
|
| 41 |
# Function to query the API and return the generated image
|
| 42 |
-
def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
|
| 43 |
if prompt == "" or prompt is None:
|
| 44 |
-
return None
|
| 45 |
|
| 46 |
key = random.randint(0, 999)
|
| 47 |
|
|
@@ -52,16 +56,16 @@ def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
| 52 |
prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
|
| 53 |
print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
|
| 54 |
|
| 55 |
-
# Enhance the prompt using the Mistral Nemo model
|
| 56 |
-
|
| 57 |
|
| 58 |
# Add some extra flair to the prompt
|
| 59 |
-
|
| 60 |
-
print(f'\033[1mGeneration {key} final prompt:\033[0m {
|
| 61 |
|
| 62 |
# Prepare the payload for the API call, including width and height
|
| 63 |
payload = {
|
| 64 |
-
"inputs":
|
| 65 |
"is_negative": is_negative,
|
| 66 |
"steps": steps,
|
| 67 |
"cfg_scale": cfg_scale,
|
|
@@ -86,11 +90,11 @@ def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
| 86 |
# Convert the response content into an image
|
| 87 |
image_bytes = response.content
|
| 88 |
image = Image.open(io.BytesIO(image_bytes))
|
| 89 |
-
print(f'\033[1mGeneration {key} completed!\033[0m ({
|
| 90 |
-
return image
|
| 91 |
except Exception as e:
|
| 92 |
print(f"Error when trying to open the image: {e}")
|
| 93 |
-
return None
|
| 94 |
|
| 95 |
# CSS to style the app
|
| 96 |
css = """
|
|
@@ -122,6 +126,11 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
|
| 122 |
# Accordion for advanced settings
|
| 123 |
with gr.Row():
|
| 124 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
negative_prompt = gr.Textbox(
|
| 126 |
label="Negative Prompt",
|
| 127 |
placeholder="What should not be in the image",
|
|
@@ -150,11 +159,15 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
|
| 150 |
with gr.Row():
|
| 151 |
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
# Bind the button to the query function with all inputs
|
| 154 |
text_button.click(
|
| 155 |
query,
|
| 156 |
-
inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height],
|
| 157 |
-
outputs=image_output
|
| 158 |
)
|
| 159 |
|
| 160 |
# Launch the Gradio app
|
|
|
|
| 19 |
# Initialize the prompt enhancer client
|
| 20 |
prompt_enhancer = Client("K00B404/mistral-nemo-prompt-enhancer")
|
| 21 |
|
| 22 |
+
def enhance_prompt(prompt, enable_enhancement=True):
|
| 23 |
"""Enhance the given prompt using the Mistral Nemo prompt enhancer."""
|
| 24 |
+
if not enable_enhancement:
|
| 25 |
+
print(f'\033[1mPrompt enhancement disabled, using original prompt:\033[0m {prompt}')
|
| 26 |
+
return prompt
|
| 27 |
+
|
| 28 |
try:
|
| 29 |
system_message = "You are an expert at writing detailed, high-quality image generation prompts. Enhance the given prompt by adding relevant artistic details, style elements, and quality descriptors. Keep the original intent but make it more elaborate and specific."
|
| 30 |
enhanced = prompt_enhancer.predict(
|
|
|
|
| 43 |
return prompt # Fall back to original prompt if enhancement fails
|
| 44 |
|
| 45 |
# Function to query the API and return the generated image
|
| 46 |
+
def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024, enable_enhancement=True):
|
| 47 |
if prompt == "" or prompt is None:
|
| 48 |
+
return None, None
|
| 49 |
|
| 50 |
key = random.randint(0, 999)
|
| 51 |
|
|
|
|
| 56 |
prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
|
| 57 |
print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
|
| 58 |
|
| 59 |
+
# Enhance the prompt using the Mistral Nemo model if enabled
|
| 60 |
+
enhanced_prompt = enhance_prompt(prompt, enable_enhancement)
|
| 61 |
|
| 62 |
# Add some extra flair to the prompt
|
| 63 |
+
final_prompt = f"{enhanced_prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
| 64 |
+
print(f'\033[1mGeneration {key} final prompt:\033[0m {final_prompt}')
|
| 65 |
|
| 66 |
# Prepare the payload for the API call, including width and height
|
| 67 |
payload = {
|
| 68 |
+
"inputs": final_prompt,
|
| 69 |
"is_negative": is_negative,
|
| 70 |
"steps": steps,
|
| 71 |
"cfg_scale": cfg_scale,
|
|
|
|
| 90 |
# Convert the response content into an image
|
| 91 |
image_bytes = response.content
|
| 92 |
image = Image.open(io.BytesIO(image_bytes))
|
| 93 |
+
print(f'\033[1mGeneration {key} completed!\033[0m ({final_prompt})')
|
| 94 |
+
return image, enhanced_prompt
|
| 95 |
except Exception as e:
|
| 96 |
print(f"Error when trying to open the image: {e}")
|
| 97 |
+
return None, None
|
| 98 |
|
| 99 |
# CSS to style the app
|
| 100 |
css = """
|
|
|
|
| 126 |
# Accordion for advanced settings
|
| 127 |
with gr.Row():
|
| 128 |
with gr.Accordion("Advanced Settings", open=False):
|
| 129 |
+
enable_enhancement = gr.Checkbox(
|
| 130 |
+
label="Enable Prompt Enhancement",
|
| 131 |
+
value=True,
|
| 132 |
+
description="Enable or disable automatic prompt enhancement"
|
| 133 |
+
)
|
| 134 |
negative_prompt = gr.Textbox(
|
| 135 |
label="Negative Prompt",
|
| 136 |
placeholder="What should not be in the image",
|
|
|
|
| 159 |
with gr.Row():
|
| 160 |
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
|
| 161 |
|
| 162 |
+
# Text output area to display the enhanced prompt
|
| 163 |
+
with gr.Row():
|
| 164 |
+
prompt_output = gr.Textbox(label="Enhanced Prompt", elem_id="prompt-output")
|
| 165 |
+
|
| 166 |
# Bind the button to the query function with all inputs
|
| 167 |
text_button.click(
|
| 168 |
query,
|
| 169 |
+
inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height, enable_enhancement],
|
| 170 |
+
outputs=[image_output, prompt_output]
|
| 171 |
)
|
| 172 |
|
| 173 |
# Launch the Gradio app
|