Tran Xuan Huy commited on
Commit
d3b3fb2
·
1 Parent(s): 5909b88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +282 -3
app.py CHANGED
@@ -1,10 +1,290 @@
1
-
2
  import gradio as gr
3
  import json
4
  import torch
5
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
6
  from collections import namedtuple
7
- from component2date import time2date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  fields = ['device', 'model_name', 'max_source_length', 'max_target_length', 'beam_size']
10
  params = namedtuple('params', field_names=fields)
@@ -17,7 +297,6 @@ args = params(
17
  beam_size=1
18
  )
19
 
20
-
21
  model = AutoModelForSeq2SeqLM.from_pretrained(
22
  "Huy1432884/db_retrieval",
23
  use_auth_token="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"
 
 
1
  import gradio as gr
2
  import json
3
  import torch
4
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
5
  from collections import namedtuple
6
+ import datetime
7
+ import calendar
8
+ from dateutil.relativedelta import relativedelta
9
+
10
+ def time2date(input):
11
+ chu_ky_thoi_gian = input['CHU KỲ THỜI GIAN']
12
+ thu = input['THỨ']
13
+ ngay = input['NGÀY']
14
+ tuan = input['TUẦN']
15
+ thang = input['THÁNG']
16
+ quy = input['QUÝ']
17
+ nam = input['NĂM']
18
+ current_date = datetime.date.today()
19
+ output = 'error'
20
+ if chu_ky_thoi_gian=='ngày':
21
+ # hôm kia
22
+ if ngay=='hôm kia':
23
+ output = current_date - datetime.timedelta(days=2)
24
+ # hôm qua
25
+ elif ngay=='hôm qua':
26
+ output = current_date - datetime.timedelta(days=1)
27
+ # hôm nay
28
+ elif ngay=='hôm nay':
29
+ output = current_date
30
+ # ngày mai
31
+ elif ngay=='mai':
32
+ output = current_date + datetime.timedelta(days=1)
33
+ # ngày kia
34
+ elif ngay=='kia':
35
+ output = current_date + datetime.timedelta(days=2)
36
+ # đầu
37
+ elif ngay=='đầu':
38
+ if thang=='trước':
39
+ needed_thang = 12 if current_date.month==1 else current_date.month-1
40
+ needed_nam = current_date.year-1 if current_date.month==1 else current_date.year
41
+ output = datetime.date(
42
+ needed_nam,
43
+ needed_thang,
44
+ 1
45
+ )
46
+ elif thang=='này':
47
+ needed_thang = current_date.month
48
+ needed_nam = current_date.year
49
+ output = datetime.date(
50
+ needed_nam,
51
+ needed_thang,
52
+ 1
53
+ )
54
+ elif thang=='sau':
55
+ needed_thang = 1 if current_date.month==12 else current_date.month+1
56
+ needed_nam = current_date.year+1 if current_date.month==12 else current_date.year
57
+ output = datetime.date(
58
+ needed_nam,
59
+ needed_thang,
60
+ 1
61
+ )
62
+ else:
63
+ print('ngày đầu')
64
+ output = current_date
65
+ # cuối
66
+ elif ngay=='cuối':
67
+ if thang=='trước':
68
+ needed_thang = 12 if current_date.month==1 else current_date.month-1
69
+ needed_nam = current_date.year-1 if current_date.month==1 else current_date.year
70
+ output = datetime.date(
71
+ needed_nam,
72
+ needed_thang,
73
+ calendar.monthrange(needed_nam, needed_thang)[1]
74
+ )
75
+ elif thang=='này':
76
+ needed_thang = current_date.month
77
+ needed_nam = current_date.year
78
+ output = datetime.date(
79
+ needed_nam,
80
+ needed_thang,
81
+ calendar.monthrange(needed_nam, needed_thang)[1]
82
+ )
83
+ elif thang=='sau':
84
+ needed_thang = 1 if current_date.month==12 else current_date.month+1
85
+ needed_nam = current_date.year+1 if current_date.month==12 else current_date.year
86
+ output = datetime.date(
87
+ needed_nam,
88
+ needed_thang,
89
+ calendar.monthrange(needed_nam, needed_thang)[1]
90
+ )
91
+ else:
92
+ print('ngày cuối')
93
+ output = current_date
94
+ # xxx
95
+ elif ngay.isdigit():
96
+ ngay = int(ngay)
97
+ if thang.isdigit():
98
+ thang = int(thang)
99
+ if nam.isdigit():
100
+ nam = int(nam)
101
+ try:
102
+ output = datetime.date(nam, thang, ngay)
103
+ except ValueError:
104
+ print("ngày xxx tháng yyy nam xxx")
105
+ output = current_date
106
+ else:
107
+ try:
108
+ output = datetime.date(current_date.year, thang, ngay)
109
+ except ValueError:
110
+ print("ngày xxx tháng yyy nam xxx")
111
+ output = current_date
112
+ else:
113
+ try:
114
+ output = datetime.date(current_date.year, current_date.month, ngay)
115
+ except ValueError:
116
+ print('ngày xxx')
117
+ output = current_date
118
+ elif ngay=='khác':
119
+ # thứ hai tuần sau
120
+ # thứ hai tuần này
121
+ # thứ hai tuần trước
122
+ # thứ hai tuần gần nhất
123
+ # thứ hai
124
+ if thu != "khác":
125
+ if tuan=='khác' or tuan=='gần nhất':
126
+ current_thu = current_date.weekday()+2
127
+ input_thu = 8 if thu=='chủ nhật' else int(thu)
128
+ if input_thu < current_thu:
129
+ daydelta = current_thu - input_thu
130
+ output = current_date - datetime.timedelta(days=daydelta)
131
+ elif input_thu == current_thu:
132
+ output = current_date - datetime.timedelta(days=7)
133
+ else:
134
+ daydelta = input_thu - current_thu
135
+ output = current_date - datetime.timedelta(days=7-daydelta)
136
+ elif tuan=='này':
137
+ current_thu = current_date.weekday()+2
138
+ input_thu = 8 if thu=='chủ nhật' else int(thu)
139
+ if input_thu < current_thu:
140
+ daydelta = current_thu - input_thu
141
+ output = current_date - datetime.timedelta(days=daydelta)
142
+ elif input_thu == current_thu:
143
+ output = current_date
144
+ else:
145
+ daydelta = input_thu - current_thu
146
+ output = current_date + datetime.timedelta(days=daydelta)
147
+ elif tuan=='trước':
148
+ current_thu = current_date.weekday()+2
149
+ input_thu = 8 if thu=='chủ nhật' else int(thu)
150
+ if input_thu < current_thu:
151
+ daydelta = current_thu - input_thu + 7
152
+ output = current_date - datetime.timedelta(days=daydelta)
153
+ elif input_thu == current_thu:
154
+ output = current_date - datetime.timedelta(days=7)
155
+ else:
156
+ daydelta = input_thu - current_thu
157
+ output = current_date - datetime.timedelta(days=7-daydelta)
158
+ elif tuan=='sau':
159
+ current_thu = current_date.weekday()+2
160
+ input_thu = 8 if thu=='chủ nhật' else int(thu)
161
+ if input_thu < current_thu:
162
+ daydelta = 7 - (current_thu - input_thu)
163
+ output = current_date + datetime.timedelta(days=daydelta)
164
+ elif input_thu == current_thu:
165
+ output = current_date + datetime.timedelta(days=7)
166
+ else:
167
+ daydelta = input_thu - current_thu
168
+ output = current_date + datetime.timedelta(days=7+daydelta)
169
+ elif thu=='khác':
170
+ current_thu = current_date.weekday()
171
+ if tuan=='trước' or tuan=='gần nhất': # ngày cuối tuần
172
+ daydelta = current_thu+1
173
+ output = current_date - datetime.timedelta(days=daydelta)
174
+ elif tuan=='này':
175
+ daydelta = 6 - current_thu
176
+ if daydelta > 0:
177
+ output = current_date + datetime.timedelta(days=daydelta)
178
+ else:
179
+ output = current_date
180
+ elif tuan=='sau':
181
+ daydelta = 13 - current_thu
182
+ output = current_date + datetime.timedelta(days=daydelta)
183
+ else:
184
+ print('ngày khác thứ khác tuần khác')
185
+ output = current_date
186
+ else: # return default for ngay
187
+ output = current_date
188
+ print('Error chu ky thoi gian: ngay')
189
+
190
+ elif chu_ky_thoi_gian=='tháng': # ngày cuối tháng
191
+ if thu != 'khác' or ngay != 'khác' or tuan != 'khác':
192
+ print('Invalid')
193
+ needed_thang = 12 if current_date.month==1 else current_date.month-1
194
+ needed_nam = current_date.year-1 if current_date.month==1 else current_date.year
195
+ output = datetime.date(
196
+ needed_nam,
197
+ needed_thang,
198
+ calendar.monthrange(needed_nam, needed_thang)[1]
199
+ )
200
+
201
+ elif thang=='trước':
202
+ needed_thang = 12 if current_date.month==1 else current_date.month-1
203
+ needed_nam = current_date.year-1 if current_date.month==1 else current_date.year
204
+ output = datetime.date(
205
+ needed_nam,
206
+ needed_thang,
207
+ calendar.monthrange(needed_nam, needed_thang)[1]
208
+ )
209
+ elif thang=='này':
210
+ needed_thang = current_date.month
211
+ needed_nam = current_date.year
212
+ output = datetime.date(
213
+ needed_nam,
214
+ needed_thang,
215
+ calendar.monthrange(needed_nam, needed_thang)[1]
216
+ )
217
+ elif thang=='sau':
218
+ needed_thang = 1 if current_date.month==12 else current_date.month+1
219
+ needed_nam = current_date.year+1 if current_date.month==12 else current_date.year
220
+ output = datetime.date(
221
+ needed_nam,
222
+ needed_thang,
223
+ calendar.monthrange(needed_nam, needed_thang)[1]
224
+ )
225
+ elif thang.isdigit():
226
+ thang = int(thang)
227
+ if nam.isdigit():
228
+ nam = int(nam)
229
+ output = datetime.date(
230
+ int(nam),
231
+ int(thang),
232
+ calendar.monthrange(int(nam), int(thang))[1]
233
+ )
234
+ else:
235
+ if thang > current_date.month:
236
+ output = datetime.date(
237
+ current_date.year-1,
238
+ thang,
239
+ calendar.monthrange(current_date.year-1, thang)[1]
240
+ )
241
+ else:
242
+ output = datetime.date(
243
+ current_date.year,
244
+ thang,
245
+ calendar.monthrange(current_date.year, thang)[1]
246
+ )
247
+ elif thang=='khác':
248
+ needed_thang = 12 if current_date.month==1 else current_date.month-1
249
+ needed_nam = current_date.year-1 if current_date.month==1 else current_date.year
250
+ output = datetime.date(
251
+ needed_nam,
252
+ needed_thang,
253
+ calendar.monthrange(needed_nam, needed_thang)[1]
254
+ )
255
+ else: # ngay cuoi thang truoc
256
+ needed_thang = 12 if current_date.month==1 else current_date.month-1
257
+ needed_nam = current_date.year-1 if current_date.month==1 else current_date.year
258
+ output = datetime.date(
259
+ needed_nam,
260
+ needed_thang,
261
+ calendar.monthrange(needed_nam, needed_thang)[1]
262
+ )
263
+ print('Error chu ky thoi gian: thang')
264
+ elif chu_ky_thoi_gian=='quý':
265
+ if quy in ['1', '2', '3', '4', 'I', 'II', 'III', 'IV']:
266
+ output = "Valid"
267
+ output = current_date
268
+ # print('Chu ky thoi gian: quy ' + quy)
269
+ else:
270
+ output = "Invalid"
271
+ output = current_date
272
+ # print('Error chu ky thoi gian: quy')
273
+ elif chu_ky_thoi_gian=='năm':
274
+ if nam.isdigit():
275
+ output = "Valid"
276
+ output = current_date
277
+ # print("Chu ky thoi gian: nam " + nam)
278
+ else:
279
+ output = "Invalid"
280
+ output = current_date
281
+ # print('Error chu ky thoi gian: năm')
282
+ elif chu_ky_thoi_gian=='khác':
283
+ output = "Valid"
284
+ output = current_date
285
+ else:
286
+ output = current_date
287
+ return output
288
 
289
  fields = ['device', 'model_name', 'max_source_length', 'max_target_length', 'beam_size']
290
  params = namedtuple('params', field_names=fields)
 
297
  beam_size=1
298
  )
299
 
 
300
  model = AutoModelForSeq2SeqLM.from_pretrained(
301
  "Huy1432884/db_retrieval",
302
  use_auth_token="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"