Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,31 +2,63 @@ 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 |
# Load models via gradio's built-in client
|
| 10 |
model1 = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA")
|
| 11 |
model2 = gr.load("models/Purz/face-projection")
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def ensure_image_type(possible_img):
|
| 16 |
"""
|
| 17 |
-
Unwraps
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
"""
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def generate_images(text, selected_model):
|
| 32 |
stop_event.clear()
|
|
@@ -44,11 +76,13 @@ def generate_images(text, selected_model):
|
|
| 44 |
return ["Image generation stopped by user."] * 3
|
| 45 |
|
| 46 |
modified_text = f"{text} variation {i+1}"
|
|
|
|
|
|
|
| 47 |
raw_output = model(modified_text)
|
| 48 |
|
| 49 |
-
# Unwrap
|
| 50 |
-
|
| 51 |
-
results.append(
|
| 52 |
|
| 53 |
return results
|
| 54 |
|
|
@@ -59,9 +93,9 @@ def stop_generation():
|
|
| 59 |
|
| 60 |
with gr.Blocks() as interface:
|
| 61 |
gr.Markdown(
|
| 62 |
-
"### ⚠ Sorry for the inconvenience. The Space is currently running on
|
| 63 |
)
|
| 64 |
-
|
| 65 |
text_input = gr.Textbox(
|
| 66 |
label="Welcome to EpicFrame. Set free your imagination!",
|
| 67 |
placeholder="Type your prompt. Example: A mob boss smoking a cigar outside a tiny cafe."
|
|
@@ -71,16 +105,16 @@ with gr.Blocks() as interface:
|
|
| 71 |
label="Select Model",
|
| 72 |
value="Model 1 (Turbo Realism)"
|
| 73 |
)
|
| 74 |
-
|
| 75 |
with gr.Row():
|
| 76 |
generate_button = gr.Button("Generate 3 Images 🎨")
|
| 77 |
stop_button = gr.Button("Stop Image Generation")
|
| 78 |
-
|
| 79 |
with gr.Row():
|
| 80 |
output1 = gr.Image(label="Generated Image 1")
|
| 81 |
output2 = gr.Image(label="Generated Image 2")
|
| 82 |
output3 = gr.Image(label="Generated Image 3")
|
| 83 |
-
|
| 84 |
generate_button.click(
|
| 85 |
fn=generate_images,
|
| 86 |
inputs=[text_input, model_selector],
|
|
@@ -92,4 +126,4 @@ with gr.Blocks() as interface:
|
|
| 92 |
outputs=[output1, output2, output3]
|
| 93 |
)
|
| 94 |
|
| 95 |
-
interface.launch()
|
|
|
|
| 2 |
import threading
|
| 3 |
import os
|
| 4 |
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import numpy as np
|
| 7 |
|
| 8 |
os.environ["OMP_NUM_THREADS"] = str(os.cpu_count())
|
| 9 |
torch.set_num_threads(os.cpu_count())
|
| 10 |
|
| 11 |
+
stop_event = threading.Event()
|
| 12 |
+
|
| 13 |
# Load models via gradio's built-in client
|
| 14 |
model1 = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA")
|
| 15 |
model2 = gr.load("models/Purz/face-projection")
|
| 16 |
|
| 17 |
+
def unwrap_image_output(raw_output):
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
+
Unwraps raw_output until we (hopefully) get a PIL Image or NumPy array.
|
| 20 |
+
Gradio's Image component can display:
|
| 21 |
+
- A PIL image,
|
| 22 |
+
- A NumPy array (H x W x C),
|
| 23 |
+
- A file path (string) pointing to an image on disk, or
|
| 24 |
+
- A base64-encoded image string.
|
| 25 |
+
This function attempts to handle the first two cases.
|
| 26 |
+
|
| 27 |
+
If the model returns a tuple/list/dict that does not
|
| 28 |
+
contain an actual image, we raise a ValueError so that
|
| 29 |
+
it's clear how to proceed.
|
| 30 |
"""
|
| 31 |
+
# 1) Keep unwrapping if it's a tuple/list
|
| 32 |
+
# (e.g. sometimes pipelines return (image, text))
|
| 33 |
+
while isinstance(raw_output, (list, tuple)):
|
| 34 |
+
if not raw_output:
|
| 35 |
+
raise ValueError("Received an empty list/tuple from the model.")
|
| 36 |
+
raw_output = raw_output[0] # Take the first element
|
| 37 |
+
|
| 38 |
+
# 2) Check if it's a dict containing an "image" key
|
| 39 |
+
if isinstance(raw_output, dict):
|
| 40 |
+
# Common pattern: raw_output.get("image") might be the real image
|
| 41 |
+
if "image" in raw_output:
|
| 42 |
+
raw_output = raw_output["image"]
|
| 43 |
+
else:
|
| 44 |
+
raise ValueError(
|
| 45 |
+
f"Received a dictionary but could not find an 'image' key. "
|
| 46 |
+
f"Keys present: {list(raw_output.keys())}"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# 3) At this point, raw_output should be either a PIL image, NumPy array, or a path
|
| 50 |
+
if isinstance(raw_output, Image.Image):
|
| 51 |
+
return raw_output
|
| 52 |
+
elif isinstance(raw_output, np.ndarray):
|
| 53 |
+
return raw_output
|
| 54 |
+
elif isinstance(raw_output, str):
|
| 55 |
+
# If it's a file path or base64 string, we can return it as-is
|
| 56 |
+
return raw_output
|
| 57 |
+
|
| 58 |
+
# If we get here, it means we still have some unexpected type
|
| 59 |
+
raise ValueError(
|
| 60 |
+
f"Could not convert output to an image. It is of type: {type(raw_output)}"
|
| 61 |
+
)
|
| 62 |
|
| 63 |
def generate_images(text, selected_model):
|
| 64 |
stop_event.clear()
|
|
|
|
| 76 |
return ["Image generation stopped by user."] * 3
|
| 77 |
|
| 78 |
modified_text = f"{text} variation {i+1}"
|
| 79 |
+
|
| 80 |
+
# Call the model
|
| 81 |
raw_output = model(modified_text)
|
| 82 |
|
| 83 |
+
# Unwrap it to get a proper single image
|
| 84 |
+
img = unwrap_image_output(raw_output)
|
| 85 |
+
results.append(img)
|
| 86 |
|
| 87 |
return results
|
| 88 |
|
|
|
|
| 93 |
|
| 94 |
with gr.Blocks() as interface:
|
| 95 |
gr.Markdown(
|
| 96 |
+
"### ⚠ Sorry for the inconvenience. The Space is currently running on CPU, which might affect performance."
|
| 97 |
)
|
| 98 |
+
|
| 99 |
text_input = gr.Textbox(
|
| 100 |
label="Welcome to EpicFrame. Set free your imagination!",
|
| 101 |
placeholder="Type your prompt. Example: A mob boss smoking a cigar outside a tiny cafe."
|
|
|
|
| 105 |
label="Select Model",
|
| 106 |
value="Model 1 (Turbo Realism)"
|
| 107 |
)
|
| 108 |
+
|
| 109 |
with gr.Row():
|
| 110 |
generate_button = gr.Button("Generate 3 Images 🎨")
|
| 111 |
stop_button = gr.Button("Stop Image Generation")
|
| 112 |
+
|
| 113 |
with gr.Row():
|
| 114 |
output1 = gr.Image(label="Generated Image 1")
|
| 115 |
output2 = gr.Image(label="Generated Image 2")
|
| 116 |
output3 = gr.Image(label="Generated Image 3")
|
| 117 |
+
|
| 118 |
generate_button.click(
|
| 119 |
fn=generate_images,
|
| 120 |
inputs=[text_input, model_selector],
|
|
|
|
| 126 |
outputs=[output1, output2, output3]
|
| 127 |
)
|
| 128 |
|
| 129 |
+
interface.launch()
|