Spaces:
Runtime error
Runtime error
Upload Home.py with huggingface_hub
Browse files
Home.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import neural_style
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
import os
|
5 |
+
import random
|
6 |
+
import numpy as np
|
7 |
+
from PIL import Image, ImageEnhance
|
8 |
+
from io import BytesIO
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
+
#import streamlit_ext as ste #for download button not to rerun
|
11 |
+
from huggingface_hub import upload_file
|
12 |
+
|
13 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
14 |
+
|
15 |
+
st.set_page_config(layout="wide")
|
16 |
+
|
17 |
+
st.markdown('<p class="font">Afrodreams.AI</p>', unsafe_allow_html=True)
|
18 |
+
st.subheader("This app takes in your image and styles it with a unique african art.")
|
19 |
+
|
20 |
+
#Create two columns with different width
|
21 |
+
col1, col2 = st.columns( [0.8, 0.2])
|
22 |
+
import time
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
with col1: # To display the header text using css style
|
27 |
+
st.markdown(""" <style> .font {
|
28 |
+
font-size:35px ; font-family: 'Cooper Black'; color: #FF9633;}
|
29 |
+
</style> """, unsafe_allow_html=True)
|
30 |
+
st.markdown('<p class="font">Upload your photo here...</p>', unsafe_allow_html=True)
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
#with st.sidebar.expander("About the App"):
|
35 |
+
# st.write("""
|
36 |
+
# This app takes in your image and styles it with a unique african art.""")
|
37 |
+
|
38 |
+
|
39 |
+
#Add file uploader to allow users to upload photos
|
40 |
+
uploaded_file = st.file_uploader("", type=['jpg','png','jpeg'])
|
41 |
+
|
42 |
+
# add slider to side bar
|
43 |
+
style_weight = st.slider("Select Style Weight", min_value=10, max_value=100, value=12)
|
44 |
+
img_size_slider= st.select_slider(label= 'Seleet Output Quality Level',
|
45 |
+
options = ['Very Low', 'Low', 'Normal', 'High', 'Very High'],
|
46 |
+
value='Normal')
|
47 |
+
img_size_mapping = {'Very Low':128, 'Low':300, 'Normal':400, 'High':500, 'Very High':600}
|
48 |
+
|
49 |
+
|
50 |
+
def get_random_subset(list_, num_imgs):
|
51 |
+
return random.sample(list_, num_imgs)
|
52 |
+
|
53 |
+
|
54 |
+
def display_random_images(five_rand_imgs, display_type, size= (15, 6)):
|
55 |
+
fig = plt.figure(figsize=size)
|
56 |
+
fig.subplots_adjust(wspace=0.2)
|
57 |
+
for i in range(1, len(five_rand_imgs)+1):
|
58 |
+
ith_image = Image.open(five_rand_imgs[i-1])
|
59 |
+
|
60 |
+
ax = fig.add_subplot(1, 5, i)
|
61 |
+
ax.imshow(ith_image)
|
62 |
+
ax.set_title(f'{display_type} {i}')
|
63 |
+
plt.axis('off')
|
64 |
+
|
65 |
+
st.pyplot(fig)
|
66 |
+
|
67 |
+
def select_title_from_check_box(index, names_dict):
|
68 |
+
return names_dict[index]
|
69 |
+
def format_func(five_rand_imgs):
|
70 |
+
st.write(' the lenght is', len(five_rand_imgs))
|
71 |
+
return list(range(len(five_rand_imgs)))
|
72 |
+
path = 'stylesv2'
|
73 |
+
|
74 |
+
|
75 |
+
#expander for style selection
|
76 |
+
with st.expander("Expand to select style type"):
|
77 |
+
img_names = [os.path.join(path, img) for img in os.listdir(path)]
|
78 |
+
five_rand_imgs0 = get_random_subset(img_names, 5)
|
79 |
+
if 'selected_image' not in st.session_state:
|
80 |
+
st.session_state.selected_image = five_rand_imgs0
|
81 |
+
five_rand_imgs = st.session_state.selected_image
|
82 |
+
display_random_images(five_rand_imgs, 'Style')
|
83 |
+
chosen_style = st.selectbox(
|
84 |
+
'Select the style you want to use',
|
85 |
+
options = five_rand_imgs, format_func = lambda x: "Style " + str(five_rand_imgs.index(x) + 1),
|
86 |
+
key= 'expander1'
|
87 |
+
)
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
#put notificaation
|
92 |
+
with st.empty():
|
93 |
+
for seconds in range(5):
|
94 |
+
st.info('Please note that by using this app, you agree that your image be will be showcased on this app.')
|
95 |
+
time.sleep(1)
|
96 |
+
st.empty()
|
97 |
+
|
98 |
+
#Add 'before' and 'after' columns
|
99 |
+
if uploaded_file is not None:
|
100 |
+
image = Image.open(uploaded_file)
|
101 |
+
|
102 |
+
col1, col2 = st.columns( [0.5, 0.5])
|
103 |
+
with col1:
|
104 |
+
st.markdown('<p style="text-align: center;">Before</p>',unsafe_allow_html=True)
|
105 |
+
st.image(image,width=300)
|
106 |
+
|
107 |
+
with col2:
|
108 |
+
st.markdown('<p style="text-align: center;">After</p>',unsafe_allow_html=True)
|
109 |
+
|
110 |
+
# add a button
|
111 |
+
run = st.button('Generate Art')
|
112 |
+
my_bar = st.progress(0)
|
113 |
+
params = neural_style.TransferParams()
|
114 |
+
params.gpu = "c"
|
115 |
+
params.backend = "mkl"
|
116 |
+
|
117 |
+
|
118 |
+
params.image_size = img_size_mapping[img_size_slider]
|
119 |
+
|
120 |
+
params.content_image = uploaded_file
|
121 |
+
params.style_weight = style_weight
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
keep_style = False
|
126 |
+
if run==True:
|
127 |
+
# run image selection if keep style is false
|
128 |
+
if keep_style==False:
|
129 |
+
|
130 |
+
styles = os.listdir(path)
|
131 |
+
#params.style_image = path + '/' + random.choice(styles)
|
132 |
+
params.style_image = chosen_style
|
133 |
+
|
134 |
+
st.session_state.submitted = True
|
135 |
+
with st.spinner('Wait for it...'):
|
136 |
+
neural_style.transfer(params)
|
137 |
+
|
138 |
+
#display image when done.
|
139 |
+
with col2:
|
140 |
+
if 'submitted' in st.session_state:
|
141 |
+
result = Image.open('out.png')
|
142 |
+
st.image(result, width=300)
|
143 |
+
buf = BytesIO()
|
144 |
+
result.save(buf, format="png")
|
145 |
+
if len(os.listdir('generated_samples')) <= 10:
|
146 |
+
img_file_name = f"generated_samples/{str(len(os.listdir('generated_samples')))}.png"
|
147 |
+
|
148 |
+
_ = upload_file(path_or_fileobj = 'out.png',
|
149 |
+
path_in_repo = img_file_name,
|
150 |
+
repo_id='AfrodreamsAI/afrodreams',
|
151 |
+
repo_type='space',
|
152 |
+
token=HF_TOKEN
|
153 |
+
)
|
154 |
+
|
155 |
+
byte_im = buf.getvalue()
|
156 |
+
#run =ste.download_button(button_text="Download Image", data=byte_im, download_filename='afrodreams.jpg', mime="image/png")
|
157 |
+
|
158 |
+
|
159 |
+
|
160 |
+
|
161 |
+
# selectiuing random iamges to be displayed
|
162 |
+
img_names = [os.path.join('generated_samples', img) for img in os.listdir('generated_samples')]
|
163 |
+
five_rand_imgs1 = get_random_subset(img_names, 5)
|
164 |
+
st.subheader('\n\n\n\n\n\n\n\n Examples of some Generate Images')
|
165 |
+
display_random_images(five_rand_imgs1, 'Generate image', size=(20, 15))
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
|
170 |
+
|
171 |
+
|