raynardj commited on
Commit
cf33f35
1 Parent(s): e65a26c

🎸 more fluent experience

Browse files
Files changed (2) hide show
  1. app.py +50 -40
  2. meta.csv +0 -0
app.py CHANGED
@@ -7,7 +7,7 @@ from requests.auth import HTTPBasicAuth
7
  import torch
8
 
9
  st.set_page_config(layout="wide")
10
- st.title("【随无涯】")
11
 
12
  @st.cache(allow_output_mutation=True)
13
  def load_model():
@@ -42,7 +42,7 @@ def inference(text):
42
  bos_token_id=101,
43
  eos_token_id=tokenizer.sep_token_id,
44
  pad_token_id=tokenizer.pad_token_id,
45
- ), skip_special_tokens=True)[0].replace(" ","")
46
 
47
 
48
  @st.cache
@@ -53,13 +53,15 @@ def get_file_df():
53
 
54
  file_df = get_file_df()
55
 
56
- col1, col2 = st.columns([.3, 1])
57
-
58
- col1.markdown("""
59
  * 朕亲自下厨的[🤗 翻译模型](https://github.com/raynardj/wenyanwen-ancient-translate-to-modern), [⭐️ 训练笔记](https://github.com/raynardj/yuan)
60
- * 📚 书籍来自 [殆知阁](http://www.daizhige.org/),只为了便于展示翻译,喜欢请访问网站,书籍[github文件链接](https://github.com/garychowcmu/daizhigev20)
61
  """)
62
 
 
 
 
63
  USER_ID = st.secrets["USER_ID"]
64
  SECRET = st.secrets["SECRET"]
65
 
@@ -117,56 +119,64 @@ if 'pathway' in st.session_state:
117
  else:
118
  st.session_state.pathway = []
119
 
120
- path_text = col1.text("/".join(st.session_state.pathway))
 
121
 
122
  def reset_path():
123
- print("before rooting")
124
- print("/".join(st.session_state.pathway))
125
  st.session_state.pathway = []
126
  path_text.text(st.session_state.pathway)
127
 
128
- if col1.button("回到根目录"):
 
129
  reset_path()
130
 
131
- def display_tree(sub_list):
132
- dropdown = col1.selectbox("【选书】", options=sub_list)
133
- if col1.button(f'【确定{len(st.session_state.pathway)+1}】'):
 
 
 
134
  st.session_state.pathway.append(dropdown)
135
  if dropdown.endswith('.txt'):
136
  filepath = "/".join(st.session_state.pathway)
137
  file_size = file_size_map[filepath]
138
- col2.write(
139
- f"loading file:{filepath},({show_file_size(file_size)})")
140
-
141
- # if file size is too large, we will not load it
142
- if file_size > 3*1024*1024:
143
- urlpath = filepath.replace(".txt",".html")
144
- dzg = f"http://www.daizhige.org/{urlpath}"
145
- st.markdown(f"文件太大,[前往殆知阁页面]({dzg}), 或挑挑其他的书吧")
 
 
 
 
 
 
146
  reset_path()
147
- return None
148
- path_text.text(filepath)
149
- text = fetch_file(filepath)
150
- # set y scroll markdown
151
- col2.markdown(f"""```{text}```""", )
152
- reset_path()
153
 
154
  else:
155
  sub_list = fetch_from_df(
156
  st.session_state.pathway)
157
  path_text.text("/".join(st.session_state.pathway))
158
- display_tree(sub_list)
159
-
160
-
161
- display_tree(fetch_from_df(st.session_state.pathway))
 
 
 
 
 
 
 
 
 
 
 
 
162
 
163
- cc = col2.text_area("【输入文本】", height=150)
 
164
 
165
- if col2.button("【翻译】"):
166
- if cc:
167
- if len(cc)>168:
168
- col2.write(f"句子太长,最多168个字符")
169
- else:
170
- col2.markdown(f"""```{inference(cc)}```""")
171
- else:
172
- col2.write("请输入文本")
7
  import torch
8
 
9
  st.set_page_config(layout="wide")
10
+
11
 
12
  @st.cache(allow_output_mutation=True)
13
  def load_model():
42
  bos_token_id=101,
43
  eos_token_id=tokenizer.sep_token_id,
44
  pad_token_id=tokenizer.pad_token_id,
45
+ ), skip_special_tokens=True)[0].replace(" ", "")
46
 
47
 
48
  @st.cache
53
 
54
  file_df = get_file_df()
55
 
56
+ st.sidebar.title("【随无涯】")
57
+ st.sidebar.markdown("""
 
58
  * 朕亲自下厨的[🤗 翻译模型](https://github.com/raynardj/wenyanwen-ancient-translate-to-modern), [⭐️ 训练笔记](https://github.com/raynardj/yuan)
59
+ * 📚 书籍来自 [殆知阁](http://www.daizhige.org/),文本的[github api](https://github.com/garychowcmu/daizhigev20)
60
  """)
61
 
62
+ c2 = st.container()
63
+ c = st.container()
64
+
65
  USER_ID = st.secrets["USER_ID"]
66
  SECRET = st.secrets["SECRET"]
67
 
119
  else:
120
  st.session_state.pathway = []
121
 
122
+ path_text = st.sidebar.text("/".join(st.session_state.pathway))
123
+
124
 
125
  def reset_path():
 
 
126
  st.session_state.pathway = []
127
  path_text.text(st.session_state.pathway)
128
 
129
+
130
+ if st.sidebar.button("回到根目录"):
131
  reset_path()
132
 
133
+ st.session_state.translating = False
134
+
135
+ def display_tree():
136
+ sublist = fetch_from_df(st.session_state.pathway)
137
+ dropdown = st.sidebar.selectbox("【选书】", options=sublist)
138
+ with st.spinner("加载中..."):
139
  st.session_state.pathway.append(dropdown)
140
  if dropdown.endswith('.txt'):
141
  filepath = "/".join(st.session_state.pathway)
142
  file_size = file_size_map[filepath]
143
+ with st.spinner(f"loading file:{filepath},({show_file_size(file_size)})"):
144
+ # if file size is too large, we will not load it
145
+ if file_size > 3*1024*1024:
146
+ urlpath = filepath.replace(".txt", ".html")
147
+ dzg = f"http://www.daizhige.org/{urlpath}"
148
+ st.markdown(f"文件太大,[前往殆知阁页面]({dzg}), 或挑挑其他的书吧")
149
+ reset_path()
150
+ return None
151
+ path_text.text(filepath)
152
+ text = fetch_file(filepath)
153
+ # create markdown with max heights
154
+ c.markdown(
155
+ f"""<pre style='max-height:300px;overflow-y:auto'>{text}</pre>""", unsafe_allow_html=True
156
+ )
157
  reset_path()
 
 
 
 
 
 
158
 
159
  else:
160
  sub_list = fetch_from_df(
161
  st.session_state.pathway)
162
  path_text.text("/".join(st.session_state.pathway))
163
+ display_tree()
164
+
165
+ if st.session_state.translating == False:
166
+ display_tree()
167
+
168
+ def translate_text():
169
+ st.session_state.translating = True
170
+ if c2.button("【翻译】"):
171
+ if cc:
172
+ if len(cc) > 168:
173
+ c2.write(f"句子太长,最多168个字符")
174
+ else:
175
+ c2.markdown(f"""```{inference(cc)}```""")
176
+ else:
177
+ c2.write("请输入文本")
178
+ st.session_state.translating = False
179
 
180
+ cc = c2.text_area("【输入文本】", height=150)
181
+ translate_text()
182
 
 
 
 
 
 
 
 
 
meta.csv CHANGED
The diff for this file is too large to render. See raw diff