raynardj commited on
Commit
2fc06de
1 Parent(s): e0a6c5a

🎸 logging

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -24,6 +24,7 @@ tokenizer, model = load_model()
24
 
25
 
26
  def inference(text):
 
27
  tk_kwargs = dict(
28
  truncation=True,
29
  max_length=168,
@@ -32,7 +33,7 @@ def inference(text):
32
 
33
  inputs = tokenizer([text, ], **tk_kwargs)
34
  with torch.no_grad():
35
- return tokenizer.batch_decode(
36
  model.generate(
37
  inputs.input_ids,
38
  attention_mask=inputs.attention_mask,
@@ -42,6 +43,8 @@ def inference(text):
42
  eos_token_id=tokenizer.sep_token_id,
43
  pad_token_id=tokenizer.pad_token_id,
44
  ), skip_special_tokens=True)[0].replace(" ", "")
 
 
45
 
46
 
47
  @st.cache
@@ -96,6 +99,7 @@ def fetch_file(path):
96
  obj_hash = file_obj_hash_map[path]
97
  auth = HTTPBasicAuth(USER_ID, SECRET)
98
  url = f"https://api.github.com/repos/garychowcmu/daizhigev20/git/blobs/{obj_hash}"
 
99
  r = requests.get(url, auth=auth)
100
  if r.status_code == 200:
101
  data = r.json()
@@ -121,8 +125,6 @@ def show_filepath(filepath: str):
121
 
122
  if st.sidebar.selectbox(label="何以尋跡 How to search",options=["以類尋書 category","書名求書 search"])=="以類尋書 category":
123
 
124
- st.session_state.translating = False
125
-
126
  # root_data = fetch_from_github()
127
  if 'pathway' in st.session_state:
128
  pass
@@ -151,12 +153,14 @@ if st.sidebar.selectbox(label="何以尋跡 How to search",options=["以類尋
151
  with st.spinner(f"Load 載文:{filepath},({show_file_size(file_size)})"):
152
  # if file size is too large, we will not load it
153
  if file_size > 3*1024*1024:
 
154
  urlpath = filepath.replace(".txt", ".html")
155
  dzg = f"http://www.daizhige.org/{urlpath}"
156
  st.markdown(f"File too big 其文碩而難載,不能為之,[往 殆知閣]({dzg}), 或擇他書")
157
  reset_path()
158
  return None
159
  path_text.text(filepath)
 
160
  text = fetch_file(filepath)
161
  # create markdown with max heights
162
  c.markdown(
@@ -170,8 +174,7 @@ if st.sidebar.selectbox(label="何以尋跡 How to search",options=["以類尋
170
  path_text.text("/".join(st.session_state.pathway))
171
  display_tree()
172
 
173
- if st.session_state.translating == False:
174
- display_tree()
175
  else:
176
  def search_kw():
177
  result = file_df[file_df.filepath.str.contains(st.session_state.kw)].reset_index(drop=True)
@@ -186,12 +189,10 @@ else:
186
  st.session_state.kw = kw
187
  search_kw()
188
 
189
- if st.session_state.translating == False:
190
- loading_with_search()
191
 
192
 
193
  def translate_text():
194
- st.session_state.translating = True
195
  if c2.button("【曉文達義 Translate】"):
196
  if cc:
197
  if len(cc) > 168:
@@ -200,7 +201,6 @@ def translate_text():
200
  c2.markdown(f"""```{inference(cc)}```""")
201
  else:
202
  c2.write("【入難曉之文字 Please input sentence for translating】")
203
- st.session_state.translating = False
204
 
205
  cc = c2.text_area("【入難曉之文字 Input sentence】", height=150)
206
  translate_text()
 
24
 
25
 
26
  def inference(text):
27
+ print(f"from: {text}")
28
  tk_kwargs = dict(
29
  truncation=True,
30
  max_length=168,
 
33
 
34
  inputs = tokenizer([text, ], **tk_kwargs)
35
  with torch.no_grad():
36
+ new = tokenizer.batch_decode(
37
  model.generate(
38
  inputs.input_ids,
39
  attention_mask=inputs.attention_mask,
 
43
  eos_token_id=tokenizer.sep_token_id,
44
  pad_token_id=tokenizer.pad_token_id,
45
  ), skip_special_tokens=True)[0].replace(" ", "")
46
+ print(f"to: {new}")
47
+ return new
48
 
49
 
50
  @st.cache
 
99
  obj_hash = file_obj_hash_map[path]
100
  auth = HTTPBasicAuth(USER_ID, SECRET)
101
  url = f"https://api.github.com/repos/garychowcmu/daizhigev20/git/blobs/{obj_hash}"
102
+ print(f"requesting {url}")
103
  r = requests.get(url, auth=auth)
104
  if r.status_code == 200:
105
  data = r.json()
 
125
 
126
  if st.sidebar.selectbox(label="何以尋跡 How to search",options=["以類尋書 category","書名求書 search"])=="以類尋書 category":
127
 
 
 
128
  # root_data = fetch_from_github()
129
  if 'pathway' in st.session_state:
130
  pass
 
153
  with st.spinner(f"Load 載文:{filepath},({show_file_size(file_size)})"):
154
  # if file size is too large, we will not load it
155
  if file_size > 3*1024*1024:
156
+ print(f"skip {filepath}")
157
  urlpath = filepath.replace(".txt", ".html")
158
  dzg = f"http://www.daizhige.org/{urlpath}"
159
  st.markdown(f"File too big 其文碩而難載,不能為之,[往 殆知閣]({dzg}), 或擇他書")
160
  reset_path()
161
  return None
162
  path_text.text(filepath)
163
+ print(f"read {filepath}")
164
  text = fetch_file(filepath)
165
  # create markdown with max heights
166
  c.markdown(
 
174
  path_text.text("/".join(st.session_state.pathway))
175
  display_tree()
176
 
177
+ display_tree()
 
178
  else:
179
  def search_kw():
180
  result = file_df[file_df.filepath.str.contains(st.session_state.kw)].reset_index(drop=True)
 
189
  st.session_state.kw = kw
190
  search_kw()
191
 
192
+ loading_with_search()
 
193
 
194
 
195
  def translate_text():
 
196
  if c2.button("【曉文達義 Translate】"):
197
  if cc:
198
  if len(cc) > 168:
 
201
  c2.markdown(f"""```{inference(cc)}```""")
202
  else:
203
  c2.write("【入難曉之文字 Please input sentence for translating】")
 
204
 
205
  cc = c2.text_area("【入難曉之文字 Input sentence】", height=150)
206
  translate_text()