Spaces:
Sleeping
Sleeping
File size: 7,916 Bytes
5b46afd 3038f2b 5b46afd 3038f2b 5b46afd 8fc7ee2 5b46afd 8fc7ee2 5b46afd 8fc7ee2 5b46afd 3038f2b 5b46afd 3038f2b 5b46afd 3038f2b 5b46afd 3038f2b 5b46afd 3038f2b 5b46afd 3038f2b 8fc7ee2 5b46afd 8fc7ee2 5b46afd 8fc7ee2 5b46afd 3038f2b 5b46afd 3038f2b 8fc7ee2 5b46afd 3038f2b 5b46afd 3038f2b 5b46afd 3038f2b 5b46afd 8fc7ee2 3038f2b 8fc7ee2 5b46afd 8fc7ee2 5b46afd 8fc7ee2 5b46afd 8fc7ee2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | import os
import sys
import time
from pathlib import Path
import numpy as np
import streamlit as st
from PIL import Image
sys.path.insert(0, str(Path(__file__).resolve().parent))
try:
from .ocr_engine import DEFAULT_MODEL_ID, OCREngine
from .storage import is_model_cached, storage_info
from .utils import clean_ocr_markdown
except ImportError:
from ocr_engine import DEFAULT_MODEL_ID, OCREngine
from storage import is_model_cached, storage_info
from utils import clean_ocr_markdown
st.set_page_config(
page_title="LightOnOCR Handwriting POC",
page_icon="📝",
layout="wide",
initial_sidebar_state="expanded",
)
st.markdown(
"""
<style>
.stApp {
background: #f7f8fb;
color: #111827;
}
[data-testid="stSidebar"] {
background: #ffffff;
border-right: 1px solid #e5e7eb;
}
.hero {
padding: 1.35rem 0 1rem;
border-bottom: 1px solid #e5e7eb;
margin-bottom: 1rem;
}
.hero h1 {
font-size: 2rem;
line-height: 1.12;
margin: 0 0 .35rem;
color: #0f172a;
}
.hero p {
margin: 0;
color: #475569;
max-width: 780px;
}
.metric-strip {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: .75rem;
margin: .75rem 0 1rem;
max-width: 100%;
overflow: hidden;
}
.metric-box {
background: #ffffff;
border: 1px solid #e5e7eb;
border-radius: 8px;
padding: .8rem .9rem;
min-width: 0;
overflow: hidden;
}
.metric-box span {
display: block;
color: #64748b;
font-size: .78rem;
}
.metric-box strong {
color: #0f172a;
font-size: .95rem;
overflow-wrap: anywhere;
word-break: break-word;
}
[data-testid="column"] {
min-width: 0 !important;
}
[data-testid="stImage"] img {
max-height: 72vh;
object-fit: contain;
}
[data-testid="stCodeBlock"] {
max-width: 100%;
overflow-x: auto;
}
.result-empty {
align-items: center;
color: #64748b;
display: flex;
min-height: 260px;
}
.stButton > button {
border-radius: 8px;
font-weight: 700;
min-height: 2.75rem;
}
div[data-testid="stFileUploader"] {
background: #ffffff;
border: 1px solid #e5e7eb;
border-radius: 8px;
padding: .75rem;
}
@media (max-width: 800px) {
.metric-strip { grid-template-columns: 1fr; }
.hero h1 { font-size: 1.55rem; }
}
</style>
""",
unsafe_allow_html=True,
)
st.markdown(
"""
<div class="hero">
<h1>LightOnOCR Handwriting POC</h1>
<p>Ekstraksi teks dokumen, nota, dan handwriting dengan model PetaniHandal berbasis LightOnOCR. Output dikembalikan sebagai Markdown agar mudah diaudit, disalin, atau diproses lanjut.</p>
</div>
""",
unsafe_allow_html=True,
)
PRESET_LABELS = {
"handwriting": "Handwriting",
"document": "Dokumen umum",
"receipt": "Nota / invoice",
}
with st.sidebar:
st.header("Pengaturan POC")
preset = st.segmented_control(
"Mode ekstraksi",
options=list(PRESET_LABELS.keys()),
format_func=lambda key: PRESET_LABELS[key],
default="handwriting",
)
max_size = st.slider(
"Resolusi sisi terpanjang",
960,
2200,
1540,
step=100,
help="Rekomendasi LightOnOCR sekitar 1540px untuk menjaga geometri teks.",
)
max_new_tokens = st.slider("Batas token output", 512, 8192, 4096, step=512)
temperature = st.slider("Temperature", 0.0, 0.7, 0.1, step=0.05)
st.divider()
st.caption("Model aktif")
st.code(DEFAULT_MODEL_ID, language=None)
info = storage_info()
cached = is_model_cached(DEFAULT_MODEL_ID)
with st.expander("Storage & cache", expanded=False):
st.write(f"Environment: {info['environment']}")
st.write(f"Base path: `{info['base_path']}`")
st.write("Model cache:", "tersedia" if cached else "belum tersedia")
if "disk" in info:
disk = info["disk"]
used_pct = disk["used_gb"] / disk["total_gb"] if disk["total_gb"] else 0
st.progress(used_pct, text=f"{disk['used_gb']} GB / {disk['total_gb']} GB digunakan")
@st.cache_resource(show_spinner="Menyiapkan LightOnOCR. Download hanya terjadi jika model belum ada di cache.")
def get_ocr_engine(model_preset: str, output_tokens: int, temp: float):
return OCREngine(
preset=model_preset,
max_new_tokens=output_tokens,
temperature=temp,
)
runtime_label = "vLLM endpoint" if os.getenv("LIGHTONOCR_ENDPOINT_URL") else "Transformers local"
model_id = os.getenv("LIGHTONOCR_MODEL_ID", DEFAULT_MODEL_ID)
cache_label = "Warm cache" if is_model_cached(model_id) else "Cache pending"
st.markdown(
f"""
<div class="metric-strip">
<div class="metric-box"><span>Runtime</span><strong>{runtime_label}</strong></div>
<div class="metric-box"><span>Model</span><strong>{model_id}</strong></div>
<div class="metric-box"><span>Cache</span><strong>{cache_label}</strong></div>
</div>
""",
unsafe_allow_html=True,
)
uploaded_file = st.file_uploader(
"Unggah gambar dokumen",
type=["png", "jpg", "jpeg"],
accept_multiple_files=False,
)
if "ocr_result" not in st.session_state:
st.session_state.ocr_result = None
if "ocr_elapsed" not in st.session_state:
st.session_state.ocr_elapsed = None
if "uploaded_name" not in st.session_state:
st.session_state.uploaded_name = None
if uploaded_file is None:
st.session_state.uploaded_name = None
st.info("Unggah satu gambar untuk memulai ekstraksi.")
else:
if st.session_state.uploaded_name != uploaded_file.name:
st.session_state.uploaded_name = uploaded_file.name
st.session_state.ocr_result = None
st.session_state.ocr_elapsed = None
image = Image.open(uploaded_file).convert("RGB")
img_array_bgr = np.array(image)[:, :, ::-1].copy()
h, w = img_array_bgr.shape[:2]
left, right = st.columns([1, 1], gap="medium")
with left:
st.subheader("Preview Dokumen")
st.image(image, use_container_width=True)
st.caption(f"Ukuran asli: {w} x {h}px")
with right:
st.subheader("Hasil OCR")
run = st.button("Jalankan Ekstraksi", type="primary", use_container_width=True)
if run:
with st.spinner("Membaca dokumen dan menyusun Markdown..."):
engine = get_ocr_engine(preset, max_new_tokens, temperature)
t0 = time.time()
result = engine.process_image(img_array_bgr, max_size=max_size)
elapsed = time.time() - t0
md_text = clean_ocr_markdown(result.get("markdown_text", ""))
st.session_state.ocr_result = md_text
st.session_state.ocr_elapsed = elapsed
if st.session_state.ocr_elapsed is not None:
st.success(f"Selesai dalam {st.session_state.ocr_elapsed:.1f} detik")
with st.container(border=True):
if st.session_state.ocr_result:
tab_rendered, tab_raw = st.tabs(["Rendered", "Markdown"])
with tab_rendered:
st.markdown(st.session_state.ocr_result, unsafe_allow_html=True)
with tab_raw:
st.code(st.session_state.ocr_result, language="markdown")
elif st.session_state.ocr_elapsed is not None:
st.warning("Model tidak mengembalikan teks untuk gambar ini.")
else:
st.markdown(
'<div class="result-empty">Hasil OCR akan muncul di sini setelah ekstraksi dijalankan.</div>',
unsafe_allow_html=True,
)
|