import base64
import hashlib
import requests
import os
import streamlit as st
BOOKSHINE_HOST = 'https://api.bookshine.at'
BOOKSHINE_PORT = '4690'
PASSWORDS = [p.lower() for p in os.environ.get("PASSWORDS", "").split(",")]
PITCH_EMBED = """"""
def password_is_correct(password):
return password.lower() in PASSWORDS
def process_docx(uploaded_file):
# Read and encode file contents
encoded_file = base64.b64encode(uploaded_file.read()).decode('utf-8')
payload = {
"filename": uploaded_file.name,
"attachment_b64": encoded_file,
"include_diff": True,
"include_docx": True,
"include_commas": True,
"is_demo": False,
}
response = requests.post(f'{BOOKSHINE_HOST}:{BOOKSHINE_PORT}/process_md', json=payload)
# Decode response and create download link
if response.ok:
response = response.json()
# make corrected docx available for download
decoded_file = base64.b64decode(response['docx'])
out_name = uploaded_file.name.replace('.docx', '-bookshine.docx')
st.download_button('✅ Korrigierten Umbruch herunterladen', data=decoded_file, file_name=out_name)
# show diff
diff = response["diff"].replace("¶", " ")
diff = diff.replace("", "")
st.write(diff, unsafe_allow_html=True)
else:
st.error('Es ist ein Fehler aufgetreten')
return
def process_pdf(uploaded_file):
# Read and encode file contents
encoded_file = base64.b64encode(uploaded_file.read()).decode('utf-8')
# md5 encode file contents
md5 = hashlib.md5(encoded_file.encode()).hexdigest()
payload = {
"attachment_id": md5,
"attachment_b64": encoded_file,
}
response = requests.post(f'{BOOKSHINE_HOST}:{BOOKSHINE_PORT}/process_umbruch', json=payload)
# Decode response and create download link
if response.ok:
response = response.json()
# trennungen = [a for a in response["annotations"] if a["type"] == "TRENNUNG"]
b64 = response['annotated']
decoded_file = base64.b64decode(b64)
out_name = uploaded_file.name.replace('.pdf', '-bookshine.pdf')
st.download_button('✅ Report herunterladen', data=decoded_file, file_name=out_name)
# show pdf
pdf_display = f''
st.write(pdf_display, unsafe_allow_html=True)
else:
st.error('Es ist ein Fehler aufgetreten')
def main():
st.write("❧
", unsafe_allow_html=True)
st.write(PITCH_EMBED, unsafe_allow_html=True)
st.markdown("
", unsafe_allow_html=True)
st.subheader('Überzeugen Sie sich selbst')
if "in_progress" not in st.session_state:
st.session_state.in_progress = False
# st.write("Bitte geben Sie die E-Mail-Adresse an, an die Sie die Resultate geschickt haben wollen.")
uploaded_file = st.file_uploader('Manuskript (.docx) oder Umbruch (.pdf) hochladen', type=['docx', 'pdf'], accept_multiple_files=False)
if not uploaded_file:
return
password = st.text_input("Zugriffscode").lower()
process_button_name = "✨ Manuskript korrigieren"
if uploaded_file.name.endswith('.pdf'):
process_button_name = "✨ Umbruch prüfen"
def set_in_progress():
st.session_state.in_progress = True
# API request button
if st.button(process_button_name, on_click=set_in_progress, disabled=not password_is_correct(password) or st.session_state.in_progress):
with st.spinner('Datei wird verarbeitet …'):
if uploaded_file.name.endswith('.docx'):
process_docx(uploaded_file)
elif uploaded_file.name.endswith('.pdf'):
process_pdf(uploaded_file)
else:
st.warning('Bitte laden Sie eine Datei des Typs .docx oder .pdf hoch.')
st.session_state.in_progress = False
if not password_is_correct(password):
st.error('**Zugriffscode ist falsch.** Wenn Sie Bookshine testen wollen, aber keinen Zugriffscode haben, schreiben Sie einfach eine formlose E-Mail an alexander.seifert@gmail.com mit dem Betreff `Bookshine Zugriffscode`.')
if __name__ == '__main__':
main()