Spaces:
Runtime error
Runtime error
SuperPunk2077
commited on
Commit
•
59907f0
1
Parent(s):
3eabd63
Update app.py
Browse files
app.py
CHANGED
@@ -4,4 +4,82 @@ def greet(name):
|
|
4 |
return "Hello " + name + "!!"
|
5 |
|
6 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
return "Hello " + name + "!!"
|
5 |
|
6 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
+
iface.launch()
|
8 |
+
|
9 |
+
import os, requests, huggingface.dataset
|
10 |
+
import tensorflow as tf
|
11 |
+
|
12 |
+
PAGES = ['xing', 'pacific', 'gsc', 'rrc']
|
13 |
+
|
14 |
+
PAGE_SIZE = 100
|
15 |
+
WEIGHTS_PATH = os.path.expanduser('~/.huggingface')
|
16 |
+
WEIGHTS_PATH += '/model.h5'
|
17 |
+
FLATTENED_PATH = WEIGHTS_PATH + '/flattened'
|
18 |
+
BELIEF_PATH = WEIGHTS_PATH + '/belief'
|
19 |
+
TRAIN_DIR = os.path.expanduser('~/.huggingface')
|
20 |
+
+ '/models/nlp-train'
|
21 |
+
DEMO_DIR = os.path.expanduser('~/.huggingface')
|
22 |
+
+ '/models/nlp-demo'
|
23 |
+
|
24 |
+
PAGE_DIRS = []
|
25 |
+
for page in PAGES:
|
26 |
+
filename = page.replace('-', '_')
|
27 |
+
PAGE_DIRS.append(os.path.expanduser('~/.huggingface') + '/text/{0}/pages'.format(filename))
|
28 |
+
|
29 |
+
Y_TO_X = {}
|
30 |
+
def add_page(page, dirs):
|
31 |
+
page_dir = dirs[page]
|
32 |
+
assert page_dir is not None
|
33 |
+
train_page, test_page, train_test_ratio, _, _ = huggingface.dataset.read_page(page, page_dir, page_size=PAGE_SIZE)
|
34 |
+
assert train_page is not None
|
35 |
+
assert test_page is not None
|
36 |
+
if train_test_ratio == 0.5:
|
37 |
+
assert train_page.shape == (PAGE_SIZE,)
|
38 |
+
assert test_page.shape == (PAGE_SIZE,)
|
39 |
+
else:
|
40 |
+
assert train_page.shape == (int(train_page.shape[0] * train_test_ratio),)
|
41 |
+
assert test_page.shape == (PAGE_SIZE - int(train_page.shape[0] * train_test_ratio),)
|
42 |
+
X = np.hstack([train_page, test_page])
|
43 |
+
if page in Y_TO_X:
|
44 |
+
Y = Y_TO_X[page]
|
45 |
+
else:
|
46 |
+
Y = list(huggingface.dataset.read_text(page).encode('utf8'))
|
47 |
+
Y_TO_X[page] = Y
|
48 |
+
return X, Y
|
49 |
+
|
50 |
+
add_page(xing, PAGE_DIRS)
|
51 |
+
add_page(pacific, PAGE_DIRS)
|
52 |
+
add_page(gsc, PAGE_DIRS)
|
53 |
+
add_page(rrc, PAGE_DIRS)
|
54 |
+
|
55 |
+
# load model
|
56 |
+
with tf.Session() as sess:
|
57 |
+
model = huggingface.model.load(sess, FLATTENED_PATH, PAGE_DIRS)
|
58 |
+
model.to(sess)
|
59 |
+
|
60 |
+
X, Y = np.array(list(map(add_page, PAGES))), []
|
61 |
+
|
62 |
+
for page in PAGES:
|
63 |
+
X, Y = np.array(list(map(add_page, PAGES))), list(Y_TO_X[page])
|
64 |
+
|
65 |
+
X = np.array(X)
|
66 |
+
/ 255.0
|
67 |
+
Y = np.array(Y) / 255.0
|
68 |
+
X = np.reshape(X, (-1, 100, 200, 1))
|
69 |
+
Y = np.reshape(Y, (-1, 10))
|
70 |
+
|
71 |
+
model = tf.keras.models.Model(inputs=model.input, outputs=model.output)
|
72 |
+
model.compile(optimizer=tf.keras.optimizers.Adam(),
|
73 |
+
loss='categorical_crossentropy',
|
74 |
+
metrics=['accuracy'])
|
75 |
+
model.fit(X, Y, batch_size=100, nb_epoch=1000, verbose=2, validation_data=(X, Y))
|
76 |
+
model.save_weights(WEIGHTS_PATH)
|
77 |
+
|
78 |
+
BELIEF_PATH = WEIGHTS_PATH + '/belief'
|
79 |
+
model.compile(optimizer=tf.keras.optimizers.Adam(),
|
80 |
+
loss=tf.keras.losses.HuberLoss(delta=0.01))
|
81 |
+
model.load_weights(WEIGHTS_PATH)
|
82 |
+
model.fit(X, Y, batch_size=10, epochs=1)
|
83 |
+
|
84 |
+
NOTE : To know more about why to use Huber loss instead of Mean Square Error follow this link
|
85 |
+
Good Luck!
|