Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -42,26 +42,27 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
42 |
headers = {"Authorization": f"Bearer {huggingface_api_key}"}
|
43 |
|
44 |
if prompt == "" or prompt is None:
|
45 |
-
return None
|
46 |
|
47 |
key = random.randint(0, 999)
|
48 |
|
49 |
prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
|
50 |
print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
|
51 |
|
|
|
52 |
if enhance_prompt_option:
|
53 |
prompt = enhance_prompt(prompt)
|
54 |
print(f'\033[1mGeneration {key} enhanced prompt:\033[0m {prompt}')
|
55 |
|
56 |
-
|
57 |
-
print(f'\033[1mGeneration {key}:\033[0m {
|
58 |
|
59 |
# If seed is -1, generate a random seed and use it
|
60 |
if seed == -1:
|
61 |
seed = random.randint(1, 1000000000)
|
62 |
|
63 |
payload = {
|
64 |
-
"inputs":
|
65 |
"is_negative": is_negative,
|
66 |
"steps": steps,
|
67 |
"cfg_scale": cfg_scale,
|
@@ -80,16 +81,16 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
80 |
try:
|
81 |
image_bytes = response.content
|
82 |
image = Image.open(io.BytesIO(image_bytes))
|
83 |
-
print(f'\033[1mGeneration {key} completed!\033[0m ({
|
84 |
|
85 |
# Save the image to a file and return the file path and seed
|
86 |
output_path = f"./output_{key}.png"
|
87 |
image.save(output_path)
|
88 |
|
89 |
-
return output_path, seed
|
90 |
except Exception as e:
|
91 |
print(f"Error when trying to open the image: {e}")
|
92 |
-
return None, None
|
93 |
|
94 |
css = """
|
95 |
#app-container {
|
@@ -144,9 +145,11 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
|
144 |
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
|
145 |
with gr.Row():
|
146 |
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
|
|
|
147 |
seed_output = gr.Textbox(label="Seed Used", elem_id="seed-output")
|
|
|
148 |
|
149 |
# Adjust the click function to include the API key, use_dev, and enhance_prompt_option as inputs
|
150 |
-
text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, huggingface_api_key, use_dev, enhance_prompt_option], outputs=[image_output, seed_output])
|
151 |
|
152 |
-
app.launch(show_api=True, share=False
|
|
|
42 |
headers = {"Authorization": f"Bearer {huggingface_api_key}"}
|
43 |
|
44 |
if prompt == "" or prompt is None:
|
45 |
+
return None, None, None
|
46 |
|
47 |
key = random.randint(0, 999)
|
48 |
|
49 |
prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
|
50 |
print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
|
51 |
|
52 |
+
original_prompt = prompt
|
53 |
if enhance_prompt_option:
|
54 |
prompt = enhance_prompt(prompt)
|
55 |
print(f'\033[1mGeneration {key} enhanced prompt:\033[0m {prompt}')
|
56 |
|
57 |
+
final_prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
58 |
+
print(f'\033[1mGeneration {key}:\033[0m {final_prompt}')
|
59 |
|
60 |
# If seed is -1, generate a random seed and use it
|
61 |
if seed == -1:
|
62 |
seed = random.randint(1, 1000000000)
|
63 |
|
64 |
payload = {
|
65 |
+
"inputs": final_prompt,
|
66 |
"is_negative": is_negative,
|
67 |
"steps": steps,
|
68 |
"cfg_scale": cfg_scale,
|
|
|
81 |
try:
|
82 |
image_bytes = response.content
|
83 |
image = Image.open(io.BytesIO(image_bytes))
|
84 |
+
print(f'\033[1mGeneration {key} completed!\033[0m ({final_prompt})')
|
85 |
|
86 |
# Save the image to a file and return the file path and seed
|
87 |
output_path = f"./output_{key}.png"
|
88 |
image.save(output_path)
|
89 |
|
90 |
+
return output_path, seed, prompt if enhance_prompt_option else original_prompt
|
91 |
except Exception as e:
|
92 |
print(f"Error when trying to open the image: {e}")
|
93 |
+
return None, None, None
|
94 |
|
95 |
css = """
|
96 |
#app-container {
|
|
|
145 |
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
|
146 |
with gr.Row():
|
147 |
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
|
148 |
+
with gr.Row():
|
149 |
seed_output = gr.Textbox(label="Seed Used", elem_id="seed-output")
|
150 |
+
final_prompt_output = gr.Textbox(label="Final Prompt", elem_id="final-prompt-output")
|
151 |
|
152 |
# Adjust the click function to include the API key, use_dev, and enhance_prompt_option as inputs
|
153 |
+
text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, huggingface_api_key, use_dev, enhance_prompt_option], outputs=[image_output, seed_output, final_prompt_output])
|
154 |
|
155 |
+
app.launch(show_api=True, share=False)
|