Say123 commited on
Commit
00430d8
·
1 Parent(s): 5d0a140

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -0
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import io
4
+ import warnings
5
+ from PIL import Image
6
+ from stability_sdk import client
7
+ import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
8
+
9
+ os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443'
10
+ os.environ['STABILITY_KEY'] = 'sk-pP36XPd4SZ81KoNApJEZrU3TBzMlO5NPaT2tMW9yfWqO5rcz'
11
+
12
+ # Set up our connection to the API.
13
+ stability_api = client.StabilityInference(
14
+ key=os.environ['STABILITY_KEY'], # API Key reference.
15
+ verbose=True, # Print debug messages.
16
+ engine="stable-diffusion-v1-5", # Set the engine to use for generation.
17
+ # Available engines: stable-diffusion-v1 stable-diffusion-v1-5 stable-diffusion-512-v2-0 stable-diffusion-768-v2-0
18
+ # stable-diffusion-512-v2-1 stable-diffusion-768-v2-1 stable-inpainting-v1-0 stable-inpainting-512-v2-0
19
+ )
20
+
21
+ def generate_image(prompt):
22
+ answers = stability_api.generate(
23
+ prompt,
24
+ steps=30,
25
+ cfg_scale=8.0,
26
+ width=512,
27
+ height=512,
28
+ samples=1,
29
+ sampler=generation.SAMPLER_K_DPMPP_2M
30
+ )
31
+
32
+ for resp in answers:
33
+ for artifact in resp.artifacts:
34
+ if artifact.finish_reason == generation.FILTER:
35
+ warnings.warn(
36
+ "Forespørselen din aktiverte API-ets sikkerhetsfiltre og kunne ikke behandles."
37
+ "Vennligst endre forespørselen og prøv igjen.")
38
+ if artifact.type == generation.ARTIFACT_IMAGE:
39
+ img = Image.open(io.BytesIO(artifact.binary))
40
+ img.save("image.png")
41
+
42
+
43
+ import random
44
+
45
+ keyWords = [
46
+ ["Ocean",
47
+ "The vast ocean stretched out endlessly, its depths hiding unknown mysteries.",
48
+ "The dynamic waves of the ocean crashed against the shore, shaping the land.",
49
+ "The oceanscape was adorned with a brilliant array of marine life and coral reefs.",
50
+ "The pristine waters of the ocean shimmered in the sunlight, inviting for a swim.",
51
+ "The unpredictable ocean currents and storms made it a powerful and formidable force."],
52
+ ["Mountain",
53
+ "The towering peak, shrouded in mist, loomed above the valley below.",
54
+ "The jagged ridges of the mountain range stretched out as far as the eye could see.",
55
+ "The snow-covered summit glistened in the sunlight, beckoning adventurers to conquer its heights.",
56
+ "The rocky outcroppings of the mountain provided a natural fortress, defying the elements for centuries."],
57
+ ["Forest",
58
+ "The dense canopy of the forest blocked out the sun, creating a mysterious and enchanting atmosphere.",
59
+ "The towering trees of the forest reached up to the sky, providing a home for diverse wildlife.",
60
+ "The lush undergrowth of the forest was a riot of color and life, teeming with plants and animals.",
61
+ "The serene stillness of the forest was only occasionally broken by the call of birds and the rustling of leaves.",
62
+ "The ancient trees of the forest stood sentinel, their roots deep in the earth, their branches brushing the sky."],
63
+ ["River",
64
+ "The sparkling water of the river flowed gracefully, carving its way through the landscape.",
65
+ "The tranquil surface of the river was ruffled by the gentle breeze and the occasional fish jumping.",
66
+ "The winding path of the river was surrounded by lush greenery and diverse wildlife.",
67
+ "The soothing sound of the river's flow provided a backdrop for the peaceful countryside.",
68
+ "The life-giving water of the river sustained the plants, animals and the community living along its banks."],
69
+ ["Fjord",
70
+ "The steep cliffs of the fjord rose dramatically from the cold, dark waters.",
71
+ "The winding inlet of the fjord was surrounded by snow-capped peaks and glaciers.",
72
+ "The tranquil surface of the fjord was dotted with small islands and surrounded by dense forests.",
73
+ "The fjord's dark, mysterious waters were home to a variety of marine life and hidden caves.",
74
+ "The natural beauty of the fjord created a breathtaking landscape that was both awe-inspiring and humbling."],
75
+ ["Desert",
76
+ "The vast expanse of the desert stretched out endlessly, its sands rippling in the relentless sun.",
77
+ "The barren landscape of the desert was punctuated by towering dunes and rocky outcroppings.",
78
+ "The scorching heat of the desert made it a harsh and unforgiving environment for those who dared to venture there.",
79
+ "The silence of the desert was only broken by the howling winds and the occasional cry of a desert animal.",
80
+ "The stark beauty of the desert was both mesmerizing and intimidating, with its endless horizon and endless sand."],
81
+ ["Volcano",
82
+ "The towering volcano loomed over the landscape, its smoldering crater a constant reminder of its power.",
83
+ "The fiery lava flowed down the volcano's slopes, carving a path of destruction in its wake.",
84
+ "The ash and smoke from the volcano filled the air, creating a dark and ominous atmosphere.",
85
+ "The explosive eruptions of the volcano sent rocks and debris flying, making it a dangerous and unpredictable force.",
86
+ "The primal energy of the volcano, both destructive and creation, was a reminder of the raw power of nature."],
87
+ ["Lake",
88
+ "The serene waters of the lake reflected the surrounding landscape, creating a mirror-like surface.",
89
+ "The tranquil atmosphere of the lake was a haven for those seeking peace and solitude.",
90
+ "The crystal clear water of the lake was teeming with fish and aquatic life.",
91
+ "The beauty of the lake was enhanced by the gentle swaying of the reeds and the colorful flowers on its shore.",
92
+ "The calmness of the lake was only broken by the occasional splash of a jumping fish or the gentle lapping of the waves."],
93
+ ["Waterfall",
94
+ "The powerful rush of water cascaded down the cliffs, creating a mesmerizing curtain of mist.",
95
+ "The thundering roar of the waterfall echoed through the surrounding wilderness.",
96
+ "The sparkling water of the waterfall glistened in the sunlight, creating a rainbow of colors.",
97
+ "The mist and spray of the waterfall created a cool and refreshing oasis in the heat of the day.",
98
+ "The raw energy and power of the waterfall was both awe-inspiring and humbling."],
99
+ ["Valley",
100
+ "The picturesque valley was nestled between rolling hills and majestic mountains.",
101
+ "The lush greenery of the valley was dotted with colorful wildflowers and verdant fields.",
102
+ "The peaceful tranquility of the valley was only broken by the gentle sound of a flowing stream.",
103
+ "The secluded valley provided a sanctuary for an array of wildlife and birds.",
104
+ "The natural beauty of the valley was a breathtaking sight, with its panoramic views and its serenity."
105
+ ]]
106
+
107
+
108
+ def findKeyword(sentence):
109
+ words = sentence.split() #Og noe her med split
110
+ for i in range(len(keyWords)):
111
+ for j in range(len(words)):
112
+ if keyWords[i][0].lower() == words[j].lower():
113
+ words[j] = keyWords[i][random.randint(1,len(keyWords[i]) -1)]
114
+ return ''.join(str(e) for e in words)
115
+
116
+ def wrapper(input):
117
+ generate_image(findKeyword(input))
118
+ return Image.open("image.png") #pillow.open
119
+
120
+ output = gr.outputs.Image(type="pil")
121
+
122
+ iface = gr.Interface(fn=wrapper, inputs=gr.inputs.Textbox(), outputs=output)
123
+ iface.launch(share=True)