Alexander Seifert
commited on
Commit
·
3849c89
1
Parent(s):
1d53dd0
Add application file
Browse files- app.py +113 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import hashlib
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
# BOOKSHINE_HOST = 'https://api.bookshine.at'
|
8 |
+
BOOKSHINE_HOST = 'http://localhost'
|
9 |
+
BOOKSHINE_PORT = '4690'
|
10 |
+
PASSWORDS = os.environ.get("PASSWORDS", "").split(",")
|
11 |
+
|
12 |
+
def password_is_correct(password):
|
13 |
+
return password in PASSWORDS
|
14 |
+
|
15 |
+
def process_docx(uploaded_file):
|
16 |
+
# Read and encode file contents
|
17 |
+
encoded_file = base64.b64encode(uploaded_file.read()).decode('utf-8')
|
18 |
+
|
19 |
+
payload = {
|
20 |
+
"filename": uploaded_file.name,
|
21 |
+
"attachment_b64": encoded_file,
|
22 |
+
"include_diff": True,
|
23 |
+
"include_docx": True,
|
24 |
+
"include_commas": True,
|
25 |
+
"is_demo": False,
|
26 |
+
}
|
27 |
+
|
28 |
+
response = requests.post(f'{BOOKSHINE_HOST}:{BOOKSHINE_PORT}/process_md', json=payload)
|
29 |
+
|
30 |
+
# Decode response and create download link
|
31 |
+
if response.ok:
|
32 |
+
response = response.json()
|
33 |
+
|
34 |
+
# make corrected docx available for download
|
35 |
+
decoded_file = base64.b64decode(response['docx'])
|
36 |
+
out_name = uploaded_file.name.replace('.docx', '-bookshine.docx')
|
37 |
+
st.download_button('✅ Korrigierten Umbruch herunterladen', data=decoded_file, file_name=out_name)
|
38 |
+
|
39 |
+
# show diff
|
40 |
+
diff = response["diff"].replace("¶", " ")
|
41 |
+
diff = diff.replace("<ins>", "<ins style='background-color: #c8e6c9; text-decoration: underline;'>")
|
42 |
+
st.write(diff, unsafe_allow_html=True)
|
43 |
+
else:
|
44 |
+
st.error('Es ist ein Fehler aufgetreten')
|
45 |
+
return
|
46 |
+
|
47 |
+
|
48 |
+
def process_pdf(uploaded_file):
|
49 |
+
# Read and encode file contents
|
50 |
+
encoded_file = base64.b64encode(uploaded_file.read()).decode('utf-8')
|
51 |
+
|
52 |
+
# md5 encode file contents
|
53 |
+
md5 = hashlib.md5(encoded_file.encode()).hexdigest()
|
54 |
+
|
55 |
+
payload = {
|
56 |
+
"attachment_id": md5,
|
57 |
+
"attachment_b64": encoded_file,
|
58 |
+
}
|
59 |
+
|
60 |
+
response = requests.post(f'{BOOKSHINE_HOST}:{BOOKSHINE_PORT}/process_umbruch', json=payload)
|
61 |
+
|
62 |
+
# Decode response and create download link
|
63 |
+
if response.ok:
|
64 |
+
response = response.json()
|
65 |
+
# trennungen = [a for a in response["annotations"] if a["type"] == "TRENNUNG"]
|
66 |
+
b64 = response['annotated']
|
67 |
+
decoded_file = base64.b64decode(b64)
|
68 |
+
out_name = uploaded_file.name.replace('.pdf', '-bookshine.pdf')
|
69 |
+
st.download_button('✅ Report herunterladen', data=decoded_file, file_name=out_name)
|
70 |
+
|
71 |
+
# show pdf
|
72 |
+
pdf_display = f'<iframe src="data:application/pdf;base64,{b64}" width="800" height="800" type="application/pdf"></iframe>'
|
73 |
+
st.write(pdf_display, unsafe_allow_html=True)
|
74 |
+
else:
|
75 |
+
st.error('Es ist ein Fehler aufgetreten')
|
76 |
+
|
77 |
+
|
78 |
+
def main():
|
79 |
+
st.title('Bookshine')
|
80 |
+
|
81 |
+
if "in_progress" not in st.session_state:
|
82 |
+
st.session_state.in_progress = False
|
83 |
+
|
84 |
+
# st.write("Bitte geben Sie die E-Mail-Adresse an, an die Sie die Resultate geschickt haben wollen.")
|
85 |
+
st.write("Wenn Sie Bookshine testen wollen, aber keinen Zugriffscode haben, schreiben Sie bitte eine E-Mail an [email protected].")
|
86 |
+
|
87 |
+
password = st.text_input("Zugriffscode")
|
88 |
+
|
89 |
+
uploaded_file = st.file_uploader('Manuskript (.docx) oder Umbruch (.pdf) hochladen', type=['docx', 'pdf'], accept_multiple_files=False)
|
90 |
+
|
91 |
+
if not uploaded_file:
|
92 |
+
return
|
93 |
+
|
94 |
+
process_button_name = "✨ Manuskript korrigieren"
|
95 |
+
if uploaded_file.name.endswith('.pdf'):
|
96 |
+
process_button_name = "✨ Umbruch prüfen"
|
97 |
+
|
98 |
+
def set_in_progress():
|
99 |
+
st.session_state.in_progress = True
|
100 |
+
|
101 |
+
# API request button
|
102 |
+
if st.button(process_button_name, on_click=set_in_progress, disabled=not password_is_correct(password) or st.session_state.in_progress):
|
103 |
+
with st.spinner('Datei wird verarbeitet …'):
|
104 |
+
if uploaded_file.name.endswith('.docx'):
|
105 |
+
process_docx(uploaded_file)
|
106 |
+
elif uploaded_file.name.endswith('.pdf'):
|
107 |
+
process_pdf(uploaded_file)
|
108 |
+
else:
|
109 |
+
st.warning('Bitte laden Sie eine Datei des Typs .docx oder .pdf hoch.')
|
110 |
+
st.session_state.in_progress = False
|
111 |
+
|
112 |
+
if __name__ == '__main__':
|
113 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
streamlit
|