Prakh24s commited on
Commit
67674ef
1 Parent(s): 1e9dde0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -85
app.py CHANGED
@@ -1,46 +1,61 @@
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
- from diffusers import DiffusionPipeline
5
- import torch
 
6
 
7
- device = "cuda" if torch.cuda.is_available() else "cpu"
8
 
9
- if torch.cuda.is_available():
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
- MAX_SEED = np.iinfo(np.int32).max
19
- MAX_IMAGE_SIZE = 1024
 
 
 
 
 
20
 
21
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
22
 
23
- if randomize_seed:
24
- seed = random.randint(0, MAX_SEED)
25
-
26
- generator = torch.Generator().manual_seed(seed)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- return image
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
- if torch.cuda.is_available():
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
- prompt = gr.Text(
69
- label="Prompt",
 
 
 
 
 
 
 
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
- negative_prompt = gr.Text(
83
- label="Negative prompt",
84
- max_lines=1,
85
- placeholder="Enter a negative prompt",
86
- visible=False,
87
- )
88
 
89
- seed = gr.Slider(
90
- label="Seed",
91
- minimum=0,
92
- maximum=MAX_SEED,
93
- step=1,
94
- value=0,
95
- )
96
 
97
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
98
 
99
- with gr.Row():
100
 
101
- width = gr.Slider(
102
- label="Width",
103
- minimum=256,
104
- maximum=MAX_IMAGE_SIZE,
105
- step=32,
106
- value=512,
107
- )
108
 
109
- height = gr.Slider(
110
- label="Height",
111
- minimum=256,
112
- maximum=MAX_IMAGE_SIZE,
113
- step=32,
114
- value=512,
115
- )
116
 
117
- with gr.Row():
118
 
119
- guidance_scale = gr.Slider(
120
- label="Guidance scale",
121
- minimum=0.0,
122
- maximum=10.0,
123
- step=0.1,
124
- value=0.0,
125
- )
126
 
127
- num_inference_steps = gr.Slider(
128
- label="Number of inference steps",
129
- minimum=1,
130
- maximum=12,
131
- step=1,
132
- value=2,
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 = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
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