Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,13 @@ import gradio as gr
|
|
| 2 |
import threading
|
| 3 |
import os
|
| 4 |
import torch
|
|
|
|
| 5 |
|
|
|
|
| 6 |
os.environ["OMP_NUM_THREADS"] = str(os.cpu_count())
|
| 7 |
torch.set_num_threads(os.cpu_count())
|
| 8 |
|
|
|
|
| 9 |
model1 = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA")
|
| 10 |
model2 = gr.load("models/Purz/face-projection")
|
| 11 |
|
|
@@ -19,46 +22,67 @@ def generate_images(text, selected_model):
|
|
| 19 |
elif selected_model == "Model 2 (Face Projection)":
|
| 20 |
model = model2
|
| 21 |
else:
|
| 22 |
-
return ["Invalid model selection."] * 3
|
| 23 |
|
| 24 |
results = []
|
|
|
|
|
|
|
| 25 |
for i in range(3):
|
| 26 |
if stop_event.is_set():
|
| 27 |
-
return ["Image generation stopped by user."] * 3
|
| 28 |
|
| 29 |
modified_text = f"{text} variation {i+1}"
|
| 30 |
result = model(modified_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
results.append(result)
|
|
|
|
| 32 |
|
| 33 |
-
return results
|
| 34 |
|
| 35 |
def stop_generation():
|
| 36 |
"""Stops the ongoing image generation by setting the stop_event flag."""
|
| 37 |
stop_event.set()
|
| 38 |
-
return ["Generation stopped."] * 3
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
text_input = gr.Textbox(label="Type here your imagination:", placeholder="Type your prompt...")
|
| 46 |
model_selector = gr.Radio(
|
| 47 |
["Model 1 (Turbo Realism)", "Model 2 (Face Projection)"],
|
| 48 |
label="Select Model",
|
| 49 |
value="Model 1 (Turbo Realism)"
|
| 50 |
)
|
| 51 |
-
|
| 52 |
with gr.Row():
|
| 53 |
generate_button = gr.Button("Generate 3 Images 🎨")
|
| 54 |
-
stop_button = gr.Button("Stop
|
| 55 |
-
|
| 56 |
with gr.Row():
|
| 57 |
output1 = gr.Image(label="Generated Image 1")
|
| 58 |
output2 = gr.Image(label="Generated Image 2")
|
| 59 |
output3 = gr.Image(label="Generated Image 3")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
interface.launch()
|
|
|
|
| 2 |
import threading
|
| 3 |
import os
|
| 4 |
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
# Set environment variables and threading
|
| 8 |
os.environ["OMP_NUM_THREADS"] = str(os.cpu_count())
|
| 9 |
torch.set_num_threads(os.cpu_count())
|
| 10 |
|
| 11 |
+
# Load models
|
| 12 |
model1 = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA")
|
| 13 |
model2 = gr.load("models/Purz/face-projection")
|
| 14 |
|
|
|
|
| 22 |
elif selected_model == "Model 2 (Face Projection)":
|
| 23 |
model = model2
|
| 24 |
else:
|
| 25 |
+
return ["Invalid model selection."] * 3, [""] * 3
|
| 26 |
|
| 27 |
results = []
|
| 28 |
+
download_links = []
|
| 29 |
+
|
| 30 |
for i in range(3):
|
| 31 |
if stop_event.is_set():
|
| 32 |
+
return ["Image generation stopped by user."] * 3, [""] * 3
|
| 33 |
|
| 34 |
modified_text = f"{text} variation {i+1}"
|
| 35 |
result = model(modified_text)
|
| 36 |
+
|
| 37 |
+
# Save image locally
|
| 38 |
+
filename = f"generated_image_{i+1}.png"
|
| 39 |
+
result.save(filename)
|
| 40 |
+
|
| 41 |
results.append(result)
|
| 42 |
+
download_links.append(filename) # Store file path for download
|
| 43 |
|
| 44 |
+
return results, download_links
|
| 45 |
|
| 46 |
def stop_generation():
|
| 47 |
"""Stops the ongoing image generation by setting the stop_event flag."""
|
| 48 |
stop_event.set()
|
| 49 |
+
return ["Generation stopped."] * 3, [""] * 3
|
| 50 |
|
| 51 |
+
# Gradio UI
|
| 52 |
+
with gr.Blocks() as interface:
|
| 53 |
+
gr.Markdown("### Text-to-Image Generator with Download Option")
|
| 54 |
+
|
| 55 |
+
text_input = gr.Textbox(label="Enter Your Prompt:", placeholder="Describe an image...")
|
|
|
|
| 56 |
model_selector = gr.Radio(
|
| 57 |
["Model 1 (Turbo Realism)", "Model 2 (Face Projection)"],
|
| 58 |
label="Select Model",
|
| 59 |
value="Model 1 (Turbo Realism)"
|
| 60 |
)
|
| 61 |
+
|
| 62 |
with gr.Row():
|
| 63 |
generate_button = gr.Button("Generate 3 Images 🎨")
|
| 64 |
+
stop_button = gr.Button("Stop Generation")
|
| 65 |
+
|
| 66 |
with gr.Row():
|
| 67 |
output1 = gr.Image(label="Generated Image 1")
|
| 68 |
output2 = gr.Image(label="Generated Image 2")
|
| 69 |
output3 = gr.Image(label="Generated Image 3")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
download1 = gr.File(label="Download Image 1")
|
| 73 |
+
download2 = gr.File(label="Download Image 2")
|
| 74 |
+
download3 = gr.File(label="Download Image 3")
|
| 75 |
+
|
| 76 |
+
generate_button.click(
|
| 77 |
+
generate_images,
|
| 78 |
+
inputs=[text_input, model_selector],
|
| 79 |
+
outputs=[output1, output2, output3, download1, download2, download3]
|
| 80 |
+
)
|
| 81 |
|
| 82 |
+
stop_button.click(
|
| 83 |
+
stop_generation,
|
| 84 |
+
inputs=[],
|
| 85 |
+
outputs=[output1, output2, output3, download1, download2, download3]
|
| 86 |
+
)
|
| 87 |
|
| 88 |
+
interface.launch()
|