Spaces:
Sleeping
Sleeping
Download ui/ocr_page.py from georgtawadrous/thoth_app: direct link, hf CLI and curl.
- Browser
- Download file 2.21 kB
-
https://huggingface.co/spaces/georgtawadrous/thoth_app/resolve/main/ui/ocr_page.py
- Command line
-
hf download hf://spaces/georgtawadrous/thoth_app/ui/ocr_page.py
-
curl -L -o ocr_page.py https://huggingface.co/spaces/georgtawadrous/thoth_app/resolve/main/ui/ocr_page.py
2.21 kB
| import streamlit as st | |
| def render_ocr(): | |
| st.markdown("## 👁 Legacy Font OCR (Coptic)") | |
| st.markdown(""" | |
| <div style="font-family:'Crimson Pro',serif; font-size:1rem; | |
| color:#9c7330; margin-bottom:1rem; line-height:1.6;"> | |
| Upload an image from a pre-Unicode Coptic grammar book (e.g. Mallon, Steindorff) | |
| to convert legacy font encodings directly to proper Unicode Coptic characters.<br><br> | |
| <i>Powered by the OCR tools from <a href="https://coptaussie.netlify.app/" target="_blank" style="color:#4fa8bf;">CoptAussie</a>.</i> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| uploaded = st.file_uploader( | |
| "Upload printed Coptic / transliterated text image for OCR", | |
| type=["png", "jpg", "jpeg", "tif", "tiff"], | |
| ) | |
| if uploaded: | |
| _handle_ocr_upload(uploaded) | |
| def _handle_ocr_upload(uploaded_file): | |
| import requests | |
| from PIL import Image | |
| import io | |
| st.markdown("**Running legacy font OCR on uploaded image via API…**") | |
| file_bytes = uploaded_file.read() | |
| img = Image.open(io.BytesIO(file_bytes)) | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.image(img, caption="Uploaded image", use_container_width=True) | |
| text = "" | |
| try: | |
| url = "https://ocr-service-410603182993.australia-southeast2.run.app/upload" | |
| files = {"image": (uploaded_file.name, file_bytes, uploaded_file.type)} | |
| response = requests.post(f"{url}?lang=cop", files=files) | |
| if response.status_code == 200: | |
| text = response.json().get("ocrText", "") | |
| else: | |
| text = f"API Error: {response.status_code} - {response.text}" | |
| except Exception as e: | |
| text = f"OCR Failed: {str(e)}" | |
| with col2: | |
| st.markdown("**OCR output (Unicode):**") | |
| st.code(text) | |
| if text.strip() and not text.startswith("API Error") and not text.startswith("OCR Failed"): | |
| if st.button("Query this text in Ask Shenute"): | |
| # Set the pending prompt in session state and navigate to chat | |
| st.session_state.pending_prompt = f"Transcribe and translate:\n\n{text.strip()}" | |
| st.session_state.page = "chat" | |
| st.rerun() | |