import streamlit as st
from unsloth import FastVisionModel
from transformers import AutoModel
from PIL import Image
import base64
import os
import tempfile
os.environ["UNSLOTH_WARN_UNINITIALIZED"] = '0'
# Page configuration
st.set_page_config(
page_title="DeepSeek-OCR",
page_icon="🔎",
layout="wide",
initial_sidebar_state="expanded"
)
# Load model with caching (only loads once)
@st.cache_resource
def load_model():
"""Load the DeepSeek OCR model once and cache it."""
model, tokenizer = FastVisionModel.from_pretrained(
model_name="Baldezo313/deepseek_ocr_merged",
load_in_4bit=True, # recommandé sur HF Spaces
auto_model=AutoModel,
trust_remote_code=True,
)
FastVisionModel.for_inference(model) # Enable for inference!
return model, tokenizer
# Title and description in main area
logo_path = "./assets/deepseek.png"
if os.path.exists(logo_path):
st.markdown(
"""
# DeepSeek-OCR
""".format(
base64.b64encode(open(logo_path, "rb").read()).decode()
),
unsafe_allow_html=True,
)
else:
st.title("DeepSeek-OCR")
# Add clear button to top right
col1, col2 = st.columns([6,1])
with col2:
if st.button("Clear 🗑️"):
if 'ocr_result' in st.session_state:
del st.session_state['ocr_result']
st.rerun()
st.markdown('
Extract structured text from images using DeepSeek-OCR!
', unsafe_allow_html=True) st.markdown("---") # Move upload controls to sidebar with st.sidebar: st.header("Upload Image") uploaded_file = st.file_uploader("Choose an image...", type=['png', 'jpg', 'jpeg']) if uploaded_file is not None: # Display the uploaded image image = Image.open(uploaded_file) st.image(image, caption="Uploaded Image") if st.button("Extract Text 🔍", type="primary"): with st.spinner("Processing image..."): try: # Load model (cached, so only loads once) # Show loading message if model hasn't been loaded yet if 'model_loaded' not in st.session_state: st.info("Loading DeepSeek OCR model (this only happens once)...") st.session_state['model_loaded'] = True model, tokenizer = load_model() # Save uploaded file temporarily (preserve original extension) file_extension = os.path.splitext(uploaded_file.name)[1] or '.jpg' with tempfile.NamedTemporaryFile(delete=False, suffix=file_extension) as tmp_file: tmp_file.write(uploaded_file.getvalue()) tmp_image_path = tmp_file.name # Create temporary output directory with tempfile.TemporaryDirectory() as tmp_output_dir: # Run inference prompt = "