Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,9 @@ from datetime import datetime
|
|
7 |
|
8 |
# Load the models
|
9 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
|
|
|
|
|
|
10 |
|
11 |
def load_models(models):
|
12 |
loaded_models = {}
|
@@ -28,14 +31,27 @@ async def infer(model_str, prompt, seed=-1):
|
|
28 |
result = await asyncio.wait_for(task, timeout=300)
|
29 |
return result
|
30 |
|
|
|
31 |
def generate_image(model_name, prompt, seed):
|
32 |
loop = asyncio.new_event_loop()
|
33 |
asyncio.set_event_loop(loop)
|
34 |
try:
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
finally:
|
37 |
loop.close()
|
38 |
|
|
|
39 |
# Interface
|
40 |
with gr.Blocks() as demo:
|
41 |
with gr.Column():
|
|
|
7 |
|
8 |
# Load the models
|
9 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
10 |
+
from PIL import Image
|
11 |
+
import io
|
12 |
+
|
13 |
|
14 |
def load_models(models):
|
15 |
loaded_models = {}
|
|
|
31 |
result = await asyncio.wait_for(task, timeout=300)
|
32 |
return result
|
33 |
|
34 |
+
|
35 |
def generate_image(model_name, prompt, seed):
|
36 |
loop = asyncio.new_event_loop()
|
37 |
asyncio.set_event_loop(loop)
|
38 |
try:
|
39 |
+
# Get the result from inference
|
40 |
+
result = loop.run_until_complete(infer(model_name, prompt, seed))
|
41 |
+
if isinstance(result, tuple):
|
42 |
+
# Assuming the first element is the image data
|
43 |
+
result = result[0]
|
44 |
+
if isinstance(result, bytes):
|
45 |
+
# Convert bytes to PIL Image if necessary
|
46 |
+
return Image.open(io.BytesIO(result))
|
47 |
+
elif isinstance(result, Image.Image):
|
48 |
+
return result
|
49 |
+
else:
|
50 |
+
raise ValueError(f"Unexpected output type: {type(result)}")
|
51 |
finally:
|
52 |
loop.close()
|
53 |
|
54 |
+
|
55 |
# Interface
|
56 |
with gr.Blocks() as demo:
|
57 |
with gr.Column():
|