File size: 8,819 Bytes
9889d2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py β€” DeepSeek-OCR (HF Space, Claude Skill-ready)
# - /ocr : REST API (POST)  β†’ file / image_b64 / image_url 지원
# - Gradio UI : λΈŒλΌμš°μ €μ—μ„œ μ—…λ‘œλ“œ ν…ŒμŠ€νŠΈ
# 폴더 ꡬ쑰 μ „μ œ:
#   /app.py
#   /DeepSeek-OCR-master/ (repo κ·ΈλŒ€λ‘œ)
#   /requirements.txt

import io
import os
import sys
import base64
import traceback
from typing import Optional

from PIL import Image
import numpy as np

import gradio as gr
from fastapi import FastAPI, UploadFile, File, Body
from fastapi.responses import JSONResponse

# ─────────────────────────────────────────────
# 0) Repo 경둜 μΆ”κ°€
# ─────────────────────────────────────────────
ROOT = os.path.dirname(__file__)
DEEPSEEK_ROOT = os.path.join(ROOT, "DeepSeek-OCR-master")
if DEEPSEEK_ROOT not in sys.path:
    sys.path.append(DEEPSEEK_ROOT)

# ─────────────────────────────────────────────
# 1) DeepSeek-OCR μ–΄λŒ‘ν„°
#   - μ €μž₯μ†Œκ°€ μ œκ³΅ν•˜λŠ” μ‹€μ œ μ§„μž…μ  이름이 λ‹€λ₯Ό 수 μžˆμ–΄
#     μ—¬λŸ¬ νŒ¨ν„΄μ„ μ‹œλ„ν•˜λ„λ‘ κ΅¬μ„±ν–ˆμŠ΅λ‹ˆλ‹€.
#   - ν•„μš” μ‹œ μ•„λž˜ "TODO" 뢀뢄을 μ‹€μ œ ν•¨μˆ˜λͺ…μœΌλ‘œ λ°”κΎΈμ„Έμš”.
# ─────────────────────────────────────────────
class DeepSeekOCRAdapter:
    def __init__(self):
        """
        κ°€λŠ₯ν•œ μ—”νŠΈλ¦¬ μ‹œλ‚˜λ¦¬μ˜€:
        A) deeps eek_ocr.py 내뢀에 클래슀/ν•¨μˆ˜ 제곡
           - class DeepSeekOCR  β†’ .recognize(Image) λ°˜ν™˜
           - def ocr_image(Image, lang="auto") λ°˜ν™˜
        B) run_dpsk_ocr_image.py 내뢀에 ν•¨μˆ˜ 제곡
           - def infer(Image) λ˜λŠ” def run(Image, ...) λ“±
        """
        self.backend = None
        self.fn = None  # callable(image, lang='auto') -> str

        # A-1) class DeepSeekOCR μ‹œλ„
        try:
            import deeps eek_ocr as dso  # DeepSeek-OCR-master/deeps eek_ocr.py
            if hasattr(dso, "DeepSeekOCR"):
                self.backend = dso.DeepSeekOCR()
                def _call(image: Image.Image, lang="auto"):
                    # ν΄λž˜μŠ€κ°€ recognize(image, lang) λ³΄μœ ν•œλ‹€κ³  κ°€μ •
                    if hasattr(self.backend, "recognize"):
                        return self.backend.recognize(image, lang=lang)
                    # ν˜Ήμ€ run/image_to_text λ“±μ˜ 이름일 수 있음
                    for cand in ("run", "infer", "image_to_text", "predict"):
                        if hasattr(self.backend, cand):
                            return getattr(self.backend, cand)(image)
                    raise AttributeError("DeepSeekOCR class found but no callable method.")
                self.fn = _call
                print("[DeepSeekOCRAdapter] Using deeps eek_ocr.DeepSeekOCR")
                return
        except Exception as e:
            print("[DeepSeekOCRAdapter] A-1 fallback:", e)

        # A-2) ν•¨μˆ˜ν˜• ocr_image μ‹œλ„
        try:
            import deeps eek_ocr as dso
            if hasattr(dso, "ocr_image"):
                def _call(image: Image.Image, lang="auto"):
                    return dso.ocr_image(image, lang=lang)  # TODO: ν•„μš” μ‹œ 인자λͺ… λ§žμΆ”κΈ°
                self.fn = _call
                print("[DeepSeekOCRAdapter] Using deeps eek_ocr.ocr_image")
                return
        except Exception as e:
            print("[DeepSeekOCRAdapter] A-2 fallback:", e)

        # B) run_dpsk_ocr_image.py μŠ€ν¬λ¦½νŠΈν˜• μ‹œλ„
        try:
            import run_dpsk_ocr_image as runner
            for cand in ("infer", "run", "predict", "main"):
                if hasattr(runner, cand) and callable(getattr(runner, cand)):
                    def _call(image: Image.Image, lang="auto", _fn=getattr(runner, cand)):
                        # NOTE: ν•΄λ‹Ή ν•¨μˆ˜κ°€ PIL.Imageκ°€ μ•„λ‹Œ 파일경둜λ₯Ό μš”κ΅¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
                        # 그런 경우 μž„μ‹œ 파일둜 μ €μž₯ν•΄ λ„˜κΉλ‹ˆλ‹€.
                        try:
                            return _fn(image)  # PIL.Image 직접 λ°›λŠ” μΌ€μ΄μŠ€
                        except Exception:
                            import tempfile
                            with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as tmp:
                                image.save(tmp.name)
                                # κ°€μž₯ ν”ν•œ CLI μŠ€νƒ€μΌ: (path)만 or (path, config)
                                try:
                                    return _fn(tmp.name)
                                except Exception:
                                    # λ°˜ν™˜μ΄ dict/text λ“± 무엇이든 str둜 μΊμŠ€νŒ…
                                    return str(_fn(tmp.name))
                    self.fn = _call
                    print(f"[DeepSeekOCRAdapter] Using run_dpsk_ocr_image.{cand}")
                    return
        except Exception as e:
            print("[DeepSeekOCRAdapter] B fallback:", e)

        # λ§ˆμ§€λ§‰ μ•ˆμ „μž₯치: 데λͺ¨
        print("[DeepSeekOCRAdapter] No concrete entry found. Falling back to DEMO.")
        def _demo(image: Image.Image, lang="auto"):
            return "[DEMO] μ—°κ²° μ™„λ£Œ β€” μ‹€μ œ ν•¨μˆ˜λͺ…을 app.pyμ—μ„œ ν•œ μ€„λ§Œ λ°”κΏ”μ£Όμ„Έμš”."
        self.fn = _demo

    def recognize(self, image: Image.Image, lang: str = "auto") -> str:
        return self.fn(image, lang=lang)


# ─────────────────────────────────────────────
# 2) μœ ν‹Έ
# ─────────────────────────────────────────────
def _to_pil(x) -> Image.Image:
    if isinstance(x, Image.Image):
        return x.convert("RGB")
    if isinstance(x, (bytes, bytearray)):
        return Image.open(io.BytesIO(x)).convert("RGB")
    if isinstance(x, np.ndarray):
        return Image.fromarray(x).convert("RGB")
    raise TypeError("Unsupported image type")

def _b64_to_image(image_b64: str) -> Image.Image:
    raw = base64.b64decode(image_b64)
    return _to_pil(raw)

def _url_to_image(url: str) -> Image.Image:
    import requests
    r = requests.get(url, timeout=20)
    r.raise_for_status()
    return _to_pil(r.content)


# ─────────────────────────────────────────────
# 3) FastAPI (REST)
# ─────────────────────────────────────────────
api = FastAPI(title="DeepSeek-OCR API")
_engine = DeepSeekOCRAdapter()

@api.post("/ocr")
async def ocr(
    image_b64: Optional[str] = Body(default=None),
    image_url: Optional[str] = Body(default=None),
    lang: str = Body(default="auto"),
    file: Optional[UploadFile] = File(default=None),
):
    try:
        if file is not None:
            image = _to_pil(await file.read())
        elif image_b64:
            image = _b64_to_image(image_b64)
        elif image_url:
            image = _url_to_image(image_url)
        else:
            return JSONResponse(status_code=400, content={
                "ok": False, "error": "Provide one of: file | image_b64 | image_url"
            })
        text = _engine.recognize(image, lang=lang)
        return {"ok": True, "text": text}
    except Exception as e:
        return JSONResponse(status_code=500, content={
            "ok": False, "error": str(e), "trace": traceback.format_exc()
        })

# ─────────────────────────────────────────────
# 4) Gradio UI (ν…ŒμŠ€νŠΈ)
# ─────────────────────────────────────────────
def _predict(image, lang):
    if image is None:
        return "No image."
    pil = _to_pil(image)
    return _engine.recognize(pil, lang=lang)

with gr.Blocks(title="DeepSeek-OCR (Claude-ready)") as demo:
    gr.Markdown("### DeepSeek-OCR (HF Space)\n이미지λ₯Ό μ—…λ‘œλ“œν•˜λ©΄ ν…μŠ€νŠΈλ₯Ό μΆ”μΆœν•©λ‹ˆλ‹€.")
    with gr.Row():
        img = gr.Image(type="pil", label="Input image")
        out = gr.Textbox(label="OCR Result", lines=8)
    lang = gr.Radio(["auto","en","ko","ja","zh"], value="auto", label="Language")
    btn = gr.Button("Run OCR")
    btn.click(_predict, inputs=[img, lang], outputs=[out])

# HF SpacesλŠ” 보톡 Gradio 앱을 κΈ°λ³Έ μ—”νŠΈλ¦¬λ‘œ λ„μš°μ§€λ§Œ,
# FastAPI μ—”λ“œν¬μΈνŠΈλ„ ν•¨κ»˜ λ…ΈμΆœν•˜λ €λ©΄ μ•„λž˜μ²˜λŸΌ aliasλ₯Ό λ‘‘λ‹ˆλ‹€.
app = api
demo.queue(concurrency_count=1)