Spaces:
Running
Running
ankankbhunia
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -18,6 +18,8 @@ import pickle
|
|
18 |
from PIL import Image
|
19 |
import tqdm
|
20 |
import shutil
|
|
|
|
|
21 |
|
22 |
wellcomingMessage = """
|
23 |
<h1>π₯ Handwriting Synthesis - Generate text in anyone's handwriting π₯ </h1>
|
@@ -39,35 +41,46 @@ model.eval()
|
|
39 |
def generate_image(text,folder, _ch3, images):
|
40 |
# Your image generation logic goes here (replace with your actual implementation)
|
41 |
# For demonstration purposes, we'll just concatenate the uploaded images horizontally.
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
68 |
|
69 |
-
|
70 |
-
|
|
|
71 |
|
72 |
return input_styles, page_val
|
73 |
|
|
|
18 |
from PIL import Image
|
19 |
import tqdm
|
20 |
import shutil
|
21 |
+
from datetime import datetime
|
22 |
+
|
23 |
|
24 |
wellcomingMessage = """
|
25 |
<h1>π₯ Handwriting Synthesis - Generate text in anyone's handwriting π₯ </h1>
|
|
|
41 |
def generate_image(text,folder, _ch3, images):
|
42 |
# Your image generation logic goes here (replace with your actual implementation)
|
43 |
# For demonstration purposes, we'll just concatenate the uploaded images horizontally.
|
44 |
+
try:
|
45 |
+
text_copy = text.copy()
|
46 |
+
if images:
|
47 |
+
style_log = images
|
48 |
+
style_inputs, width_length = load_itw_samples(images)
|
49 |
+
elif folder:
|
50 |
+
style_log = folder
|
51 |
+
style_inputs, width_length = load_itw_samples(folder)
|
52 |
+
else:
|
53 |
+
return None
|
54 |
+
# Load images
|
55 |
+
text = text.replace("\n", "").replace("\t", "")
|
56 |
+
text_encode = [j.encode() for j in text.split(' ')]
|
57 |
+
eval_text_encode, eval_len_text = model.netconverter.encode(text_encode)
|
58 |
+
eval_text_encode = eval_text_encode.to(DEVICE).repeat(batch_size, 1, 1)
|
59 |
+
|
60 |
+
input_styles, page_val = model._generate_page(style_inputs.to(DEVICE).clone(), width_length, eval_text_encode, eval_len_text, no_concat = True)
|
61 |
+
page_val = crop_(page_val[0]*255)
|
62 |
+
input_styles = crop_(input_styles[0]*255)
|
63 |
+
|
64 |
+
max_width = max(page_val.shape[1],input_styles.shape[1])
|
65 |
+
|
66 |
+
if page_val.shape[1]!=max_width:
|
67 |
+
page_val = np.concatenate([page_val, np.ones((page_val.shape[0],max_width-page_val.shape[1]))*255], 1)
|
68 |
+
else:
|
69 |
+
input_styles = np.concatenate([input_styles, np.ones((input_styles.shape[0],max_width-input_styles.shape[1]))*255], 1)
|
70 |
+
|
71 |
+
upper_pad = np.ones((45,input_styles.shape[1]))*255
|
72 |
+
input_styles = np.concatenate([upper_pad, input_styles], 0)
|
73 |
+
page_val = np.concatenate([upper_pad, page_val], 0)
|
74 |
|
75 |
+
page_val = Image.fromarray(page_val).convert('RGB')
|
76 |
+
input_styles = Image.fromarray(input_styles).convert('RGB')
|
77 |
+
|
78 |
+
current_datetime = datetime.now()
|
79 |
+
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
|
80 |
|
81 |
+
print (f'{formatted_datetime}: input_string - {text_copy}, style_input - {style_log}')
|
82 |
+
except:
|
83 |
+
print ('ERROR! Try again.')
|
84 |
|
85 |
return input_styles, page_val
|
86 |
|