Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,17 @@ import sys
|
|
| 4 |
from pathlib import Path
|
| 5 |
from PIL import Image
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def load_models_from_file(filename):
|
| 8 |
with open(filename, 'r') as f:
|
| 9 |
return [line.strip() for line in f]
|
|
@@ -28,6 +39,7 @@ def set_model(current_model):
|
|
| 28 |
current_model = models[current_model]
|
| 29 |
return gr.update(label=(f"{current_model}"))
|
| 30 |
|
|
|
|
| 31 |
def list_saved_prompts_and_images():
|
| 32 |
saved_prompts = os.listdir('saved_prompts')
|
| 33 |
saved_images = os.listdir('saved_images')
|
|
@@ -41,15 +53,23 @@ def list_saved_prompts_and_images():
|
|
| 41 |
|
| 42 |
return html_str
|
| 43 |
|
|
|
|
| 44 |
def send_it1(inputs, model_choice):
|
| 45 |
proc1 = models2[model_choice]
|
| 46 |
output1 = proc1(inputs)
|
| 47 |
-
# ... Existing code for directory creation and file saving ...
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
css=""""""
|
| 55 |
|
|
@@ -72,9 +92,11 @@ with gr.Blocks(css=css) as myface:
|
|
| 72 |
</head>
|
| 73 |
</html>
|
| 74 |
""")
|
|
|
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column(scale=100):
|
| 77 |
saved_output = gr.HTML(label="Saved Prompts and Images")
|
|
|
|
| 78 |
with gr.Row():
|
| 79 |
with gr.Tab("Title"):
|
| 80 |
gr.HTML("""<title>Prompt to Generate Image</title><div style="text-align: center; max-width: 1500px; margin: 0 auto;">
|
|
@@ -132,10 +154,13 @@ with gr.Blocks(css=css) as myface:
|
|
| 132 |
def short_prompt(inputs):
|
| 133 |
return(inputs)
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
| 140 |
myface.queue(concurrency_count=200)
|
| 141 |
myface.launch(inline=True, show_api=False, max_threads=400)
|
|
|
|
| 4 |
from pathlib import Path
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
+
# Coder: Create directories if they don't exist
|
| 8 |
+
if not os.path.exists('saved_prompts'):
|
| 9 |
+
os.makedirs('saved_prompts')
|
| 10 |
+
|
| 11 |
+
if not os.path.exists('saved_images'):
|
| 12 |
+
os.makedirs('saved_images')
|
| 13 |
+
|
| 14 |
+
# Humanities: Elegant function to generate a safe filename 📝
|
| 15 |
+
def generate_safe_filename(text):
|
| 16 |
+
return re.sub('[^a-zA-Z0-9]', '_', text)
|
| 17 |
+
|
| 18 |
def load_models_from_file(filename):
|
| 19 |
with open(filename, 'r') as f:
|
| 20 |
return [line.strip() for line in f]
|
|
|
|
| 39 |
current_model = models[current_model]
|
| 40 |
return gr.update(label=(f"{current_model}"))
|
| 41 |
|
| 42 |
+
# Analysis: Function to list saved prompts and images 📊
|
| 43 |
def list_saved_prompts_and_images():
|
| 44 |
saved_prompts = os.listdir('saved_prompts')
|
| 45 |
saved_images = os.listdir('saved_images')
|
|
|
|
| 53 |
|
| 54 |
return html_str
|
| 55 |
|
| 56 |
+
# Coder: Modified function to save the prompt and image 🖼️
|
| 57 |
def send_it1(inputs, model_choice):
|
| 58 |
proc1 = models2[model_choice]
|
| 59 |
output1 = proc1(inputs)
|
|
|
|
| 60 |
|
| 61 |
+
safe_filename = generate_safe_filename(inputs[0])
|
| 62 |
+
image_path = f"saved_images/{safe_filename}.png"
|
| 63 |
+
prompt_path = f"saved_prompts/{safe_filename}.txt"
|
| 64 |
+
|
| 65 |
+
with open(prompt_path, 'w') as f:
|
| 66 |
+
f.write(inputs[0])
|
| 67 |
+
|
| 68 |
+
Image.fromarray(output1).save(image_path)
|
| 69 |
+
|
| 70 |
+
return output1
|
| 71 |
+
|
| 72 |
+
|
| 73 |
|
| 74 |
css=""""""
|
| 75 |
|
|
|
|
| 92 |
</head>
|
| 93 |
</html>
|
| 94 |
""")
|
| 95 |
+
|
| 96 |
with gr.Row():
|
| 97 |
with gr.Column(scale=100):
|
| 98 |
saved_output = gr.HTML(label="Saved Prompts and Images")
|
| 99 |
+
|
| 100 |
with gr.Row():
|
| 101 |
with gr.Tab("Title"):
|
| 102 |
gr.HTML("""<title>Prompt to Generate Image</title><div style="text-align: center; max-width: 1500px; margin: 0 auto;">
|
|
|
|
| 154 |
def short_prompt(inputs):
|
| 155 |
return(inputs)
|
| 156 |
|
| 157 |
+
use_short.click(short_prompt,inputs=[input_text],outputs=magic1)
|
| 158 |
+
see_prompts.click(text_it1,inputs=[input_text],outputs=magic1)
|
| 159 |
+
|
| 160 |
+
# Reasoning: Link functions to Gradio components 🎛️
|
| 161 |
+
model_name1.change(set_model, inputs=model_name1, outputs=[output1])
|
| 162 |
+
run.click(send_it1, inputs=[magic1, model_name1], outputs=[output1])
|
| 163 |
+
saved_output.set(list_saved_prompts_and_images)
|
| 164 |
+
|
| 165 |
myface.queue(concurrency_count=200)
|
| 166 |
myface.launch(inline=True, show_api=False, max_threads=400)
|