mikhailbot zenafey commited on
Commit
7bef88a
·
verified ·
1 Parent(s): 53dbc39

app.py + requirements.txt upload (#5)

Browse files

- app.py + requirements.txt upload (ef727dc2a535ffa5eaa6fe4dcafc0783255de174)


Co-authored-by: Zenafey Baenro <zenafey@users.noreply.huggingface.co>

Files changed (2) hide show
  1. app.py +151 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import random
4
+ import os
5
+ from io import BytesIO
6
+ from PIL import Image
7
+ import numpy as np
8
+
9
+ MAX_SEED = np.iinfo(np.int32).max
10
+ MAX_IMAGE_SIZE = 1344
11
+
12
+
13
+ class APIClient:
14
+ def __init__(self, api_key=os.getenv("API_KEY"), base_url="inference.prodia.com"):
15
+ self.headers = {
16
+ "Content-Type": "application/json",
17
+ "Accept": "image/jpeg",
18
+ "Authorization": f"Bearer {api_key}"
19
+ }
20
+ self.base_url = f"https://{base_url}"
21
+
22
+ def _post(self, url, json=None):
23
+ r = requests.post(url, headers=self.headers, json=json)
24
+ r.raise_for_status()
25
+
26
+ return Image.open(BytesIO(r.content)).convert("RGBA")
27
+
28
+ def job(self, config):
29
+ body = {"type": "v2.job.sd3.txt2img", "config": config}
30
+ return self._post(f"{self.base_url}/v2/job", json=body)
31
+
32
+
33
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
34
+
35
+ if randomize_seed:
36
+ seed = random.randint(0, MAX_SEED)
37
+
38
+ image = generative_api.job(
39
+ {
40
+ "prompt": prompt,
41
+ "negative_prompt": negative_prompt,
42
+ "width": width,
43
+ "height": height,
44
+ "guidance_scale": guidance_scale,
45
+ "steps": num_inference_steps,
46
+ # "refiner": True
47
+ "seed": seed
48
+ }
49
+ )
50
+
51
+ return image, seed
52
+
53
+
54
+ generative_api = APIClient()
55
+
56
+ examples = [
57
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
58
+ "An astronaut riding a green horse",
59
+ "A delicious ceviche cheesecake slice",
60
+ ]
61
+
62
+ css = """
63
+ #col-container {
64
+ margin: 0 auto;
65
+ max-width: 580px;
66
+ }
67
+ """
68
+
69
+ with gr.Blocks(css=css) as demo:
70
+ with gr.Column(elem_id="col-container"):
71
+ gr.Markdown(f"""
72
+ # Demo [Stable Diffusion 3 - Medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium)
73
+ Learn more about [Stable Diffusion 3](https://stability.ai/news/stable-diffusion-3). Powered by [Prodia API](https://prodia.com).
74
+ """)
75
+
76
+ with gr.Row():
77
+ prompt = gr.Text(
78
+ label="Prompt",
79
+ show_label=False,
80
+ max_lines=1,
81
+ placeholder="Enter your prompt",
82
+ container=False,
83
+ )
84
+
85
+ run_button = gr.Button("Run", scale=0)
86
+
87
+ result = gr.Image(label="Result", show_label=False)
88
+
89
+ with gr.Accordion("Advanced Settings", open=False):
90
+ negative_prompt = gr.Text(
91
+ label="Negative prompt",
92
+ max_lines=1,
93
+ placeholder="Enter a negative prompt",
94
+ )
95
+
96
+ seed = gr.Slider(
97
+ label="Seed",
98
+ minimum=0,
99
+ maximum=MAX_SEED,
100
+ step=1,
101
+ value=0,
102
+ )
103
+
104
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
105
+
106
+ with gr.Row():
107
+ width = gr.Slider(
108
+ label="Width",
109
+ minimum=256,
110
+ maximum=MAX_IMAGE_SIZE,
111
+ step=64,
112
+ value=1024,
113
+ )
114
+
115
+ height = gr.Slider(
116
+ label="Height",
117
+ minimum=256,
118
+ maximum=MAX_IMAGE_SIZE,
119
+ step=64,
120
+ value=1024,
121
+ )
122
+
123
+ with gr.Row():
124
+ guidance_scale = gr.Slider(
125
+ label="Guidance scale",
126
+ minimum=0.0,
127
+ maximum=10.0,
128
+ step=0.1,
129
+ value=5.0,
130
+ )
131
+
132
+ num_inference_steps = gr.Slider(
133
+ label="Number of inference steps",
134
+ minimum=1,
135
+ maximum=50,
136
+ step=1,
137
+ value=28,
138
+ )
139
+
140
+ gr.Examples(
141
+ examples=examples,
142
+ inputs=[prompt]
143
+ )
144
+ gr.on(
145
+ triggers=[run_button.click, prompt.submit, negative_prompt.submit],
146
+ fn=infer,
147
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
148
+ outputs=[result, seed]
149
+ )
150
+
151
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio~=4.37.2
2
+ requests~=2.32.3
3
+ numpy~=1.26.4