KP02 commited on
Commit
9565d24
1 Parent(s): 56b4949

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -0
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from dotenv import load_dotenv
3
+ import os
4
+ from torch import autocast
5
+ #import openai
6
+ from diffusers import StableDiffusionPipeline
7
+ import torch
8
+ from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
9
+ import base64
10
+ import streamlit as st
11
+ from PIL import Image
12
+ import io
13
+ import torch
14
+ from diffusers import StableDiffusionPipeline
15
+
16
+
17
+ def get_base64(bin_file):
18
+ with open(bin_file, 'rb') as f:
19
+ data = f.read()
20
+ return base64.b64encode(data).decode()
21
+
22
+
23
+ # def set_background(png_file):
24
+ # bin_str = get_base64(png_file)
25
+ # page_bg_img = '''
26
+ # <style>
27
+ # .stApp {
28
+ # background-image: url("data:image/png;base64,%s");
29
+ # background-size: cover;
30
+ # }
31
+ # </style>
32
+ # ''' % bin_str
33
+ # st.markdown(page_bg_img, unsafe_allow_html=True)
34
+
35
+ # set_background('/home/ubuntu/AI-Image-Generation-Streamlit-App/8L0A9954.png')
36
+
37
+ #load_dotenv()
38
+ #openai.api_key = os.getenv("OPENAI_API_KEY")
39
+
40
+ #function to generate AI based images using OpenAI Dall-E
41
+ # def generate_images_using_openai(text):
42
+ # response = openai.Image.create(prompt= text, n=1, size="512x512")
43
+ # image_url = response['data'][0]['url']
44
+ # return image_url
45
+
46
+
47
+ # #function to generate AI based images using Huggingface Diffusers
48
+ # def generate_images_using_huggingface_diffusers(text):
49
+ # pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
50
+ # pipe = pipe.to("cuda")
51
+ # prompt = text
52
+ # image = pipe(prompt).images[0]
53
+ # return image
54
+
55
+ def Animegen(text):
56
+ model = "Linaqruf/animagine-xl"
57
+ pipe = StableDiffusionXLPipeline.from_pretrained(
58
+ model,
59
+ torch_dtype=torch.float16,
60
+ use_safetensors=True,
61
+ variant="fp16"
62
+ )
63
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
64
+ pipe.to('cuda')
65
+ prompt = text
66
+ image = pipe(
67
+ prompt,
68
+ width=1024,
69
+ height=1024,
70
+ guidance_scale=12,
71
+ target_size=(1024,1024),
72
+ original_size=(4096,4096),
73
+ num_inference_steps=50
74
+ ).images[0]
75
+ return image
76
+
77
+ # def Avatars(text):
78
+ # model_id = "riccardogiorato/avatar-diffusion"
79
+ # pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
80
+ # pipe = pipe.to("cuda")
81
+ # prompt = text
82
+ # image = pipe(prompt).images[0]
83
+ # return image
84
+
85
+ #Streamlit Code
86
+ choice = st.sidebar.selectbox("Select your choice", ["Home","ImageGenerator"])
87
+
88
+
89
+ if choice == "Home":
90
+ st.title(" WoxPix ")
91
+ with st.expander("About the App"):
92
+ st.write("AI-generated images are images produced using artificial intelligence methods, like Generative Adversarial Networks (GANs) or similar algorithms. These images are not taken from the internet or captured using cameras. Instead, they are created by AI models that analyze patterns in existing data and generate novel images resembling the patterns they've learned from.")
93
+ st.write("Keep in mind that AI-generated visuals may not always correspond to your exact vision, and you may need to experiment with different prompts to achieve the ideal result. Furthermore, depending on the platform and the intricacy of your request, the quality and realism of AI-generated graphics can differ.")
94
+
95
+
96
+ if choice == "ImageGenerator":
97
+ st.subheader("Image Generator")
98
+ input_prompt = st.text_input("Enter your text prompt")
99
+ # if input_prompt is not None:
100
+ # if st.button("Generate Image"):
101
+ # image_url = Animegen(input_prompt)
102
+ # st.image(image_url, caption="Generated Animegen")
103
+ if input_prompt is not None:
104
+ if st.button("Generate Image"):
105
+ image_output = Animegen(input_prompt)
106
+ st.info("Generating image.....")
107
+ st.success("Image Generated Successfully")
108
+ st.image(image_output, caption="Generated by AI Research Centre - Woxsen University")
109
+ buffered = io.BytesIO()
110
+ image_output.save(buffered, format="PNG")
111
+ st.download_button(
112
+ label="Download Image",
113
+ data=buffered.getvalue(),
114
+ file_name="generated_image.png",
115
+ mime="image/png"
116
+ )
117
+
118
+ # st.button("Download Generated Image")
119
+ # image = Image.open(image_output)
120
+ # img_byte_array = image.getvalue()
121
+
122
+ # st.download_button(
123
+ # label="Click here to download the generated image as JPEG",
124
+ # data=img_byte_array,
125
+ # file_name="generated_image.jpeg",
126
+ # mime="image/jpeg",
127
+ # )
128
+ # elif choice == "Avatars":
129
+ # st.subheader("Avatars")
130
+ # input_prompt = st.text_input("Enter your text prompt")
131
+ # if input_prompt is not None:
132
+ # if st.button("Generate Image"):
133
+ # image_url = Avatars(input_prompt)
134
+ # st.image(image_url, caption="Generated Avatar")
135
+
136
+ # elif choice == "Huggingface Diffusers":
137
+ # st.subheader("Innovative Image Generation")
138
+ # input_prompt = st.text_input("Enter your text prompt")
139
+ # if input_prompt is not None:
140
+ # if st.button("Generate Image"):
141
+ # image_output = generate_images_using_huggingface_diffusers(input_prompt)
142
+ # st.info("Generating image.....")
143
+ # st.success("Image Generated Successfully")
144
+ # st.image(image_output, caption="Generated by Huggingface Diffusers")
145
+
146
+
147
+ # model_id = "riccardogiorato/avatar-diffusion"
148
+ # pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
149
+ # pipe = pipe.to("cuda")
150
+ # prompt = "a magical witch with blue hair with avatartwow style"
151
+ # image = pipe(prompt).images[0]
152
+ # image.save("./magical_witch.png")
153
+
154
+ small_image_width = 200