-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: webapp using streamlit --------- Signed-off-by: Frost Ming <[email protected]> Co-authored-by: yihong <[email protected]>
- Loading branch information
1 parent
d177558
commit 8235a03
Showing
8 changed files
with
1,229 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .epubhv import EPUBHV as EPUBHV |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# This file is @generated by PDM. | ||
# Please do not edit it manually. | ||
|
||
altair==5.2.0 | ||
attrs==23.2.0 | ||
backports-zoneinfo==0.2.1; python_version < "3.9" | ||
beautifulsoup4==4.12.2 | ||
blinker==1.7.0 | ||
cachetools==5.3.2 | ||
certifi==2023.11.17 | ||
charset-normalizer==3.3.2 | ||
click==8.1.7 | ||
colorama==0.4.6; sys_platform == "win32" or platform_system == "Windows" | ||
cssutils==2.7.1 | ||
docstring-parser==0.15 | ||
fugashi==1.3.0 | ||
gitdb==4.0.11 | ||
gitpython==3.1.40 | ||
idna==3.6 | ||
importlib-metadata==6.11.0 | ||
importlib-resources==6.1.1; python_version < "3.9" | ||
jieba==0.42.1 | ||
jinja2==3.1.2 | ||
jsonschema==4.20.0 | ||
jsonschema-specifications==2023.12.1 | ||
langdetect==1.0.9 | ||
lxml==4.9.3 | ||
markdown-it-py==3.0.0 | ||
markupsafe==2.1.3 | ||
mdurl==0.1.2 | ||
mypy-extensions==1.0.0 | ||
numpy==1.24.4 | ||
opencc-python-reimplemented==0.1.7 | ||
packaging==23.2 | ||
pandas==2.0.3 | ||
pillow==10.2.0 | ||
pkgutil-resolve-name==1.3.10; python_version < "3.9" | ||
protobuf==4.25.1 | ||
pyarrow==14.0.2 | ||
pydeck==0.8.1b0 | ||
pygments==2.17.2 | ||
pygtrie==2.5.0 | ||
pypinyin==0.50.0 | ||
python-dateutil==2.8.2 | ||
pytz==2023.3.post1 | ||
referencing==0.32.0 | ||
requests==2.31.0 | ||
rich==13.7.0 | ||
rpds-py==0.16.2 | ||
six==1.16.0 | ||
smmap==5.0.1 | ||
soupsieve==2.5 | ||
streamlit==1.29.0 | ||
tenacity==8.2.3 | ||
tojyutping==0.2.3 | ||
toml==0.10.2 | ||
toolz==0.12.0 | ||
tornado==6.4 | ||
typed-argument-parser==1.8.1 | ||
typing-extensions==4.9.0 | ||
typing-inspect==0.9.0 | ||
tzdata==2023.4 | ||
tzlocal==5.2 | ||
unidic-lite==1.0.8 | ||
urllib3==2.1.0 | ||
validators==0.22.0 | ||
watchdog==3.0.0; platform_system != "Darwin" | ||
zipp==3.17.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import base64 | ||
import tempfile | ||
from pathlib import Path | ||
|
||
import streamlit as st | ||
import streamlit.components.v1 as components | ||
|
||
from epubhv import EPUBHV | ||
|
||
LABELS = { | ||
"none": "None", | ||
"auto": "Auto", | ||
"s2t": "Simplified Chinese to Traditional Chinese", | ||
"t2s": "Traditional Chinese to Simplified Chinese", | ||
"s2tw": "Simplified Chinese to Traditional Chinese (Taiwan Standard)", | ||
"tw2s": "Traditional Chinese (Taiwan Standard) to Simplified Chinese", | ||
"s2hk": "Simplified Chinese to Traditional Chinese (Hong Kong variant)", | ||
"hk2s": "Traditional Chinese (Hong Kong variant) to Simplified Chinese", | ||
"s2twp": "Simplified Chinese to Traditional Chinese (Taiwan variant)", | ||
"tw2sp": "Traditional Chinese (Taiwan variant) to Simplified Chinese", | ||
"t2tw": "Traditional Chinese (OpenCC Standard) to Taiwan Standard", | ||
"hk2t": "Traditional Chinese (Hong Kong variant) to Traditional Chinese", | ||
"t2hk": "Traditional Chinese (OpenCC Standard) to Hong Kong variant", | ||
"t2jp": "Traditional Chinese Characters (Kyūjitai) to New Japanese Kanji", | ||
"jp2t": "New Japanese Kanji to Traditional Chinese Characters (Kyūjitai)", | ||
"tw2t": "Traditional Chinese (OpenCC Standard) to Traditional Chinese (Taiwan standard)", | ||
} | ||
|
||
|
||
def download_button(data: bytes, download_filename: str) -> None: | ||
b64 = base64.b64encode(data).decode() | ||
|
||
dl_link = f""" | ||
<html> | ||
<head> | ||
<title>Start Auto Download file</title> | ||
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script> | ||
<script> | ||
$('<a href="data:text/csv;base64,{b64}" download="{download_filename}">')[0].click() | ||
</script> | ||
</head> | ||
</html> | ||
""" | ||
components.html(dl_link, height=0) | ||
|
||
|
||
st.set_page_config( | ||
page_title="EPUBHV, a toolset to convert your EPUB", | ||
page_icon="📖", | ||
layout="centered", | ||
initial_sidebar_state="auto", | ||
menu_items=None, | ||
) | ||
st.header("📖 EPUBHV, a toolset to convert your EPUB", divider="rainbow") | ||
st.caption( | ||
"Author: [@yihong0618](https://github.com/yihong0618) | [GitHub](https://github.com/yihong0618/epubhv) | [PyPI](https://pypi.org/project/epubhv/)", | ||
) | ||
|
||
|
||
def run(): | ||
if st.session_state["epubfile"] is None: | ||
st.error("Please upload an epub file") | ||
return | ||
epubfile = st.session_state["epubfile"] | ||
with tempfile.TemporaryDirectory() as tmpdir, st.spinner("Processing..."): | ||
with open(Path(tmpdir) / epubfile.name, "wb") as f: | ||
f.write(epubfile.read()) | ||
convert = st.session_state["convert"] | ||
epubhv = EPUBHV( | ||
file_path=Path(tmpdir) / epubfile.name, | ||
need_ruby=st.session_state["need_ruby"], | ||
need_cantonese=st.session_state["need_cantonese"], | ||
convert_to=None if convert == "none" else convert, | ||
convert_punctuation=st.session_state["punctuation"], | ||
) | ||
result = epubhv.run(method=st.session_state["method"], dest=Path(tmpdir)) | ||
download_button(result.read_bytes(), result.name) | ||
|
||
|
||
with st.form(key="my_form"): | ||
epubfile = st.file_uploader("Upload an epub file", type="epub", key="epubfile") | ||
method = st.radio( | ||
"Choose a method", | ||
("to_vertical", "to_horizontal"), | ||
format_func=lambda x: x.replace("_", " ").title(), | ||
horizontal=True, | ||
key="method", | ||
) | ||
need_ruby = st.checkbox("Need ruby", key="need_ruby") | ||
need_cantonese = st.checkbox("Need cantonese", key="need_cantonese") | ||
|
||
convert = st.selectbox( | ||
"Transform text", | ||
options=[ | ||
"none", | ||
"s2t", | ||
"t2s", | ||
"s2tw", | ||
"tw2s", | ||
"s2hk", | ||
"hk2s", | ||
"s2twp", | ||
"tw2sp", | ||
"t2tw", | ||
"hk2t", | ||
"t2hk", | ||
"t2jp", | ||
"jp2t", | ||
"tw2t", | ||
], | ||
format_func=LABELS.__getitem__, | ||
key="convert", | ||
) | ||
punctuation = st.selectbox( | ||
"Transform punctuation", | ||
options=["auto", "t2s", "s2t", "none"], | ||
format_func=LABELS.__getitem__, | ||
key="punctuation", | ||
) | ||
st.form_submit_button(label="Transform", on_click=run) |