Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,60 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import string
|
3 |
+
import random
|
4 |
|
5 |
+
|
6 |
+
def replace_with_stars(text, replacement_prob):
|
7 |
+
result = ""
|
8 |
+
for char in text:
|
9 |
+
if char in string.punctuation + ',。!?;:‘’“”【】《》、': # 包含中文标点
|
10 |
+
result += char
|
11 |
+
elif char == '\n': # 保留换行符
|
12 |
+
result += char
|
13 |
+
else:
|
14 |
+
if random.random() < replacement_prob:
|
15 |
+
result += '囗'
|
16 |
+
else:
|
17 |
+
result += char
|
18 |
+
return result
|
19 |
+
|
20 |
+
st.title('初中古诗词背诵APP')
|
21 |
+
|
22 |
+
with open('poems/poems.txt') as f:
|
23 |
+
data = f.read()
|
24 |
+
|
25 |
+
data = data.split('\n\n')
|
26 |
+
|
27 |
+
poems = {}
|
28 |
+
|
29 |
+
for poem in data:
|
30 |
+
poem = poem.split('\n')
|
31 |
+
id = int(poem[0])
|
32 |
+
name = poem[1]
|
33 |
+
poet = poem[2]
|
34 |
+
content = '\n\n'.join(poem[3:])
|
35 |
+
|
36 |
+
poems[name] = {
|
37 |
+
'id': id,
|
38 |
+
'name': name,
|
39 |
+
'poet': poet,
|
40 |
+
'content': content
|
41 |
+
}
|
42 |
+
|
43 |
+
# 写一个下拉菜单
|
44 |
+
|
45 |
+
poem_name = st.selectbox('选择要背诵的诗词', poems.keys())
|
46 |
+
|
47 |
+
with st.sidebar:
|
48 |
+
recitation_mode = st.toggle('背诵模式', value=False)
|
49 |
+
if recitation_mode:
|
50 |
+
prob_slider_ = False
|
51 |
+
replacement_prob = st.slider('遮盖占比', 0.0, 1.0, 0.8, 0.1)
|
52 |
+
|
53 |
+
if not recitation_mode:
|
54 |
+
st.markdown(f'### {poems[poem_name]["name"]}')
|
55 |
+
st.markdown(f'#### {poems[poem_name]["poet"]}')
|
56 |
+
st.markdown(f'{poems[poem_name]["content"]}')
|
57 |
+
else:
|
58 |
+
st.markdown(f'### {poems[poem_name]["name"]}')
|
59 |
+
st.markdown(f'#### {poems[poem_name]["poet"]}')
|
60 |
+
st.markdown(f'{replace_with_stars(poems[poem_name]["content"], replacement_prob)}')
|