File size: 3,658 Bytes
2000421
f6a2955
 
 
 
2000421
f6a2955
 
 
2000421
f6a2955
 
 
 
 
2000421
 
 
 
 
f6a2955
2000421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6a2955
2000421
 
f6a2955
 
2000421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6a2955
 
5648a79
f6a2955
 
2000421
 
 
f6a2955
 
2000421
 
 
 
 
f6a2955
 
 
 
2000421
f6a2955
 
2000421
 
 
f6a2955
 
 
 
 
 
 
2000421
f6a2955
 
2000421
1969b0a
2000421
 
 
 
 
 
 
 
 
 
 
 
 
 
f6a2955
 
2000421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6a2955
2000421
f6a2955
2000421
 
 
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
```python
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

from huggingface_hub import hf_hub_download

import numpy as np
import gradio as gr
import os

max_length = 5
img_width = 200
img_height = 50

# -----------------------------
# Load model from Hugging Face
# -----------------------------
def load_model():
    possible_files = ["model.h5", "model.keras"]

    model_path = None
    for fname in possible_files:
        try:
            model_path = hf_hub_download(
                repo_id="keras-io/ocr-for-captcha",
                filename=fname
            )
            print(f"Loaded model file: {fname}")
            break
        except Exception:
            continue

    if model_path is None:
        raise ValueError("No compatible model file found in Hugging Face repo.")

    return keras.models.load_model(model_path, compile=False)

model = load_model()

# Create prediction model (same as your original)
prediction_model = keras.models.Model(
    model.get_layer(name="image").input,
    model.get_layer(name="dense2").output
)

# -----------------------------
# Load vocabulary
# -----------------------------
def load_vocab():
    if os.path.exists("vocab.txt"):
        with open("vocab.txt", "r") as f:
            return f.read().splitlines()

    # fallback: download from HF
    vocab_path = hf_hub_download(
        repo_id="keras-io/ocr-for-captcha",
        filename="vocab.txt"
    )
    with open(vocab_path, "r") as f:
        return f.read().splitlines()

vocab = load_vocab()

num_to_char = layers.StringLookup(
    vocabulary=vocab, mask_token=None, invert=True
)

# -----------------------------
# Decode predictions
# -----------------------------
def decode_batch_predictions(pred):
    input_len = np.ones(pred.shape[0]) * pred.shape[1]

    results = keras.backend.ctc_decode(
        pred, input_length=input_len, greedy=True
    )[0][0][:, :max_length]

    output_text = []
    for res in results:
        res = tf.strings.reduce_join(num_to_char(res)).numpy().decode("utf-8")
        output_text.append(res)

    return output_text

# -----------------------------
# Prediction function
# -----------------------------
def classify_image(img_path):
    img = tf.io.read_file(img_path)
    img = tf.io.decode_png(img, channels=1)
    img = tf.image.convert_image_dtype(img, tf.float32)
    img = tf.image.resize(img, [img_height, img_width])
    img = tf.transpose(img, perm=[1, 0, 2])
    img = tf.expand_dims(img, axis=0)

    preds = prediction_model.predict(img)
    pred_text = decode_batch_predictions(preds)

    return pred_text[0]

# -----------------------------
# Gradio UI (modern API)
# -----------------------------
image = gr.Image(type="filepath")
text = gr.Textbox()

iface = gr.Interface(
    fn=classify_image,
    inputs=image,
    outputs=text,
    title="OCR for CAPTCHA",
    description="Keras implementation of OCR model for reading CAPTCHA 🤖",
    examples=["dd764.png", "3p4nn.png"]
)

if __name__ == "__main__":
    iface.launch()
```

---

# ⚠️ If this still fails

Most likely reason:
👉 The Hugging Face repo does **not include a full saved model**

If that happens, tell me and I’ll:

* rebuild the model architecture from the Keras example
* load weights properly
* give you a guaranteed working version

---

# ✔️ What changed

* ❌ Removed `from_pretrained_keras`
* ✅ Added `hf_hub_download`
* ✅ Added fallback for model filename
* ✅ Updated Gradio API
* ✅ Made vocab loading safer

---

If you want, I can also make this:

* run on GPU
* deploy on Hugging Face Spaces
* or convert it to a fast API backend