Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import os
|
3 |
+
import warnings
|
4 |
+
|
5 |
+
from IPython.display import display
|
6 |
+
from PIL import Image
|
7 |
+
from stability_sdk import client
|
8 |
+
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
|
9 |
+
|
10 |
+
import gradio as gr
|
11 |
+
stability_api = client.StabilityInference(
|
12 |
+
key=os.environ["Secret"],
|
13 |
+
verbose=True,
|
14 |
+
)
|
15 |
+
|
16 |
+
|
17 |
+
def infer(prompt):
|
18 |
+
# the object returned is a python generator
|
19 |
+
answers = stability_api.generate(
|
20 |
+
prompt=f"Beautiful Portait of a {prompt} made out of flowers 💐 🌺 🌸 , artstation winner by Victo Ngai, Kilian Eng, vibrant colors, winning-award masterpiece, aesthetic octane render, 8K HD",
|
21 |
+
height =640
|
22 |
+
)
|
23 |
+
|
24 |
+
# iterating over the generator produces the api response
|
25 |
+
for resp in answers:
|
26 |
+
for artifact in resp.artifacts:
|
27 |
+
if artifact.finish_reason == generation.FILTER:
|
28 |
+
warnings.warn(
|
29 |
+
"Your request activated the API's safety filters and could not be processed."
|
30 |
+
"Please modify the prompt and try again.")
|
31 |
+
if artifact.type == generation.ARTIFACT_IMAGE:
|
32 |
+
img = Image.open(io.BytesIO(artifact.binary))
|
33 |
+
return img
|
34 |
+
|
35 |
+
|
36 |
+
block = gr.Blocks(css=".container { max-width: 600px; margin: auto; }")
|
37 |
+
|
38 |
+
num_samples = 1
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
with block as demo:
|
43 |
+
gr.Markdown("<h1><center>Flower Diffusion</center></h1>")
|
44 |
+
gr.Markdown(
|
45 |
+
"Get a pretty flowery image from any prompt - keep it simple!"
|
46 |
+
)
|
47 |
+
with gr.Group():
|
48 |
+
with gr.Box():
|
49 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
50 |
+
|
51 |
+
text = gr.Textbox(
|
52 |
+
value = "Kitty cat",
|
53 |
+
label="Enter your prompt", show_label=False, max_lines=1
|
54 |
+
).style(
|
55 |
+
border=(True, False, True, True),
|
56 |
+
rounded=(True, False, False, True),
|
57 |
+
container=False,
|
58 |
+
)
|
59 |
+
btn = gr.Button("Run").style(
|
60 |
+
margin=False,
|
61 |
+
rounded=(False, True, True, False),
|
62 |
+
)
|
63 |
+
|
64 |
+
|
65 |
+
gallery = gr.Image()
|
66 |
+
text.submit(infer, inputs=[text], outputs=gallery)
|
67 |
+
btn.click(infer, inputs=[text], outputs=gallery)
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
demo.launch(debug=True, enable_queue = True)
|