Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,61 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
from
|
5 |
-
import
|
|
|
6 |
|
7 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
|
9 |
-
|
10 |
-
torch.cuda.max_memory_allocated(device=device)
|
11 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
12 |
-
pipe.enable_xformers_memory_efficient_attention()
|
13 |
-
pipe = pipe.to(device)
|
14 |
-
else:
|
15 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
16 |
-
pipe = pipe.to(device)
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
image = pipe(
|
29 |
-
prompt = prompt,
|
30 |
-
negative_prompt = negative_prompt,
|
31 |
-
guidance_scale = guidance_scale,
|
32 |
-
num_inference_steps = num_inference_steps,
|
33 |
-
width = width,
|
34 |
-
height = height,
|
35 |
-
generator = generator
|
36 |
-
).images[0]
|
37 |
|
38 |
-
|
39 |
|
40 |
examples = [
|
41 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
42 |
-
"An astronaut riding a green horse",
|
43 |
-
"A delicious ceviche cheesecake slice",
|
44 |
]
|
45 |
|
46 |
css="""
|
@@ -50,10 +65,7 @@ css="""
|
|
50 |
}
|
51 |
"""
|
52 |
|
53 |
-
|
54 |
-
power_device = "GPU"
|
55 |
-
else:
|
56 |
-
power_device = "CPU"
|
57 |
|
58 |
with gr.Blocks(css=css) as demo:
|
59 |
|
@@ -65,8 +77,15 @@ with gr.Blocks(css=css) as demo:
|
|
65 |
|
66 |
with gr.Row():
|
67 |
|
68 |
-
|
69 |
-
label="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
show_label=False,
|
71 |
max_lines=1,
|
72 |
placeholder="Enter your prompt",
|
@@ -77,60 +96,60 @@ with gr.Blocks(css=css) as demo:
|
|
77 |
|
78 |
result = gr.Image(label="Result", show_label=False)
|
79 |
|
80 |
-
with gr.Accordion("Advanced Settings", open=False):
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
|
97 |
-
|
98 |
|
99 |
-
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
|
117 |
-
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
|
135 |
gr.Examples(
|
136 |
examples = examples,
|
@@ -139,7 +158,7 @@ with gr.Blocks(css=css) as demo:
|
|
139 |
|
140 |
run_button.click(
|
141 |
fn = infer,
|
142 |
-
inputs = [
|
143 |
outputs = [result]
|
144 |
)
|
145 |
|
|
|
1 |
import gradio as gr
|
2 |
+
import replicate
|
3 |
+
from openai import OpenAI
|
4 |
+
from PIL import Image
|
5 |
+
import requests
|
6 |
+
from io import BytesIO
|
7 |
|
|
|
8 |
|
9 |
+
def generate_image_openai(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
client = OpenAI()
|
12 |
+
response = client.images.generate(
|
13 |
+
model="dall-e-3",
|
14 |
+
prompt=prompt,
|
15 |
+
size="1024x1024",
|
16 |
+
n=1,
|
17 |
+
)
|
18 |
|
19 |
+
return response.data[0].url, response.data[0].revised_prompt
|
20 |
|
21 |
+
def style_transfer(input_image_path, style_image_path, revised_prompt):
|
22 |
+
input = {
|
23 |
+
# "image": open(input_image_path, "rb"),
|
24 |
+
"image": input_image_path,
|
25 |
+
"image_style": open(style_image_path, "rb"),
|
26 |
+
"style_strength": 0.4,
|
27 |
+
"structure_strength":1.2,
|
28 |
+
"prompt": " natural light, natural bright colors, low quality, candid, grainy, instagram photo, phone camera, candid, blurry home video, high iso noisy" ,
|
29 |
+
"seed": 42,
|
30 |
+
}
|
31 |
+
output = replicate.run(
|
32 |
+
"prakharsaxena24/2d-to-real-style:c0e1e612a11a13d1d57a6d647e7665ad850bc73715337c1f499bb7b52404c35a",
|
33 |
+
input=input
|
34 |
+
)
|
35 |
+
return output[0]
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
def infer(text,title):
|
40 |
+
|
41 |
+
prompt = f"""Please create a simple suitable image to accompany the following text as part of an article with the title "{title}". The objects in the image must have realistic proportions. Text: "{text}"
|
42 |
+
Please make sure not to include text in the image."""
|
43 |
+
image_url_openai, revised_prompt = generate_image_openai(prompt)
|
44 |
+
style_image_url = style_transfer(image_url_openai, f'./style.png',revised_prompt)
|
45 |
+
response = requests.get(style_image_url)
|
46 |
+
img = Image.open(BytesIO(response.content))
|
47 |
+
|
48 |
+
return img
|
49 |
+
|
50 |
+
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
|
54 |
|
55 |
examples = [
|
56 |
+
# "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
57 |
+
# "An astronaut riding a green horse",
|
58 |
+
# "A delicious ceviche cheesecake slice",
|
59 |
]
|
60 |
|
61 |
css="""
|
|
|
65 |
}
|
66 |
"""
|
67 |
|
68 |
+
|
|
|
|
|
|
|
69 |
|
70 |
with gr.Blocks(css=css) as demo:
|
71 |
|
|
|
77 |
|
78 |
with gr.Row():
|
79 |
|
80 |
+
text = gr.Text(
|
81 |
+
label="Text",
|
82 |
+
show_label=False,
|
83 |
+
max_lines=1,
|
84 |
+
placeholder="Enter your prompt",
|
85 |
+
container=False,
|
86 |
+
)
|
87 |
+
title = gr.Text(
|
88 |
+
label="Title",
|
89 |
show_label=False,
|
90 |
max_lines=1,
|
91 |
placeholder="Enter your prompt",
|
|
|
96 |
|
97 |
result = gr.Image(label="Result", show_label=False)
|
98 |
|
99 |
+
# with gr.Accordion("Advanced Settings", open=False):
|
100 |
|
101 |
+
# negative_prompt = gr.Text(
|
102 |
+
# label="Negative prompt",
|
103 |
+
# max_lines=1,
|
104 |
+
# placeholder="Enter a negative prompt",
|
105 |
+
# visible=False,
|
106 |
+
# )
|
107 |
|
108 |
+
# seed = gr.Slider(
|
109 |
+
# label="Seed",
|
110 |
+
# minimum=0,
|
111 |
+
# maximum=100000,
|
112 |
+
# step=1,
|
113 |
+
# value=0,
|
114 |
+
# )
|
115 |
|
116 |
+
# randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
117 |
|
118 |
+
# with gr.Row():
|
119 |
|
120 |
+
# width = gr.Slider(
|
121 |
+
# label="Width",
|
122 |
+
# minimum=256,
|
123 |
+
# maximum=MAX_IMAGE_SIZE,
|
124 |
+
# step=32,
|
125 |
+
# value=512,
|
126 |
+
# )
|
127 |
|
128 |
+
# height = gr.Slider(
|
129 |
+
# label="Height",
|
130 |
+
# minimum=256,
|
131 |
+
# maximum=MAX_IMAGE_SIZE,
|
132 |
+
# step=32,
|
133 |
+
# value=512,
|
134 |
+
# )
|
135 |
|
136 |
+
# with gr.Row():
|
137 |
|
138 |
+
# guidance_scale = gr.Slider(
|
139 |
+
# label="Guidance scale",
|
140 |
+
# minimum=0.0,
|
141 |
+
# maximum=10.0,
|
142 |
+
# step=0.1,
|
143 |
+
# value=0.0,
|
144 |
+
# )
|
145 |
|
146 |
+
# num_inference_steps = gr.Slider(
|
147 |
+
# label="Number of inference steps",
|
148 |
+
# minimum=1,
|
149 |
+
# maximum=12,
|
150 |
+
# step=1,
|
151 |
+
# value=2,
|
152 |
+
# )
|
153 |
|
154 |
gr.Examples(
|
155 |
examples = examples,
|
|
|
158 |
|
159 |
run_button.click(
|
160 |
fn = infer,
|
161 |
+
inputs = [text, title],
|
162 |
outputs = [result]
|
163 |
)
|
164 |
|