import streamlit as st
def render_ocr():
st.markdown("## 👁 Legacy Font OCR (Coptic)")
st.markdown("""
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.
Powered by the OCR tools from CoptAussie.
""", 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()