Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,114 +1,67 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
import
|
| 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 |
-
def
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# For deployment, this should ideally not fail.
|
| 69 |
-
# Ensure your model is correctly pushed as SavedModel.
|
| 70 |
-
return None
|
| 71 |
-
|
| 72 |
-
model = load_model()
|
| 73 |
-
|
| 74 |
-
# --- 3. Define the prediction function for Gradio ---
|
| 75 |
-
def predict_captcha(image: Image.Image) -> str:
|
| 76 |
-
if model is None:
|
| 77 |
-
return "Error: Model not loaded. Please check logs."
|
| 78 |
-
|
| 79 |
-
# Preprocess the input image to match model's expected input
|
| 80 |
-
# Ensure this matches the preprocessing done during training!
|
| 81 |
-
img = image.resize((200, 50)) # Model input width, height (from previous discussion)
|
| 82 |
-
img_array = np.array(img).astype(np.float32)
|
| 83 |
-
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
| 84 |
-
|
| 85 |
-
# Uncomment and adjust if you applied normalization during training
|
| 86 |
-
# img_array = img_array / 255.0
|
| 87 |
-
|
| 88 |
-
# Make prediction
|
| 89 |
-
prediction = model.predict(img_array, verbose=0)
|
| 90 |
-
|
| 91 |
-
# Decode the prediction
|
| 92 |
-
decoded_solution = decode_prediction(prediction, int_to_char)
|
| 93 |
-
|
| 94 |
-
return decoded_solution
|
| 95 |
-
|
| 96 |
-
# --- 4. Create the Gradio Interface ---
|
| 97 |
-
iface = gr.Interface(
|
| 98 |
-
fn=predict_captcha,
|
| 99 |
-
inputs=gr.Image(type="pil", label="Upload Captcha Image"),
|
| 100 |
-
outputs=gr.Textbox(label="Predicted Captcha"),
|
| 101 |
-
title="Captcha Recognition",
|
| 102 |
-
description="Upload a captcha image (200x50 pixels expected) to get the predicted text.",
|
| 103 |
-
examples=[
|
| 104 |
-
# You can add example image paths here for the Gradio demo.
|
| 105 |
-
# These images should be present in your Hugging Face Space repository.
|
| 106 |
-
# e.g., "./example_captcha_1.png", "./example_captcha_2.png"
|
| 107 |
-
],
|
| 108 |
-
allow_flagging="never", # Optional: Disable flagging data
|
| 109 |
-
live=False # Set to True for real-time inference as you draw/upload
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
# Launch the Gradio app
|
| 113 |
-
if __name__ == "__main__":
|
| 114 |
-
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# --- 1. Define int_to_char mapping and decode_prediction function ---
|
| 9 |
+
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
+
MODEL_PATH = os.path.join(CURRENT_DIR, "captcha_recognition_model_char.keras")
|
| 11 |
+
INT_TO_CHAR_PATH = os.path.join(CURRENT_DIR, "int_to_char.json")
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
with open(INT_TO_CHAR_PATH, "r") as f:
|
| 15 |
+
str_int_to_char_mapping = json.load(f)
|
| 16 |
+
int_to_char = {int(k): v for k, v in str_int_to_char_mapping.items()}
|
| 17 |
+
print(f"int_to_char mapping loaded successfully.")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"Error loading int_to_char.json: {e}")
|
| 20 |
+
int_to_char = {i: chr(i + ord('A')) for i in range(26)}
|
| 21 |
+
int_to_char.update({26 + i: str(i) for i in range(10)})
|
| 22 |
+
int_to_char.update({36 + i: chr(i + ord('a')) for i in range(26)})
|
| 23 |
+
int_to_char[0] = '<pad>'
|
| 24 |
+
print("Using fallback int_to_char.")
|
| 25 |
+
|
| 26 |
+
fixed_solution_length = 5
|
| 27 |
+
|
| 28 |
+
def decode_prediction(prediction_output, int_to_char_mapping):
|
| 29 |
+
predicted_indices = np.argmax(prediction_output, axis=-1)[0]
|
| 30 |
+
predicted_chars = [int_to_char_mapping.get(idx, '') for idx in predicted_indices]
|
| 31 |
+
return "".join([char for char in predicted_chars if char != '<pad>'])
|
| 32 |
+
|
| 33 |
+
def load_model():
|
| 34 |
+
try:
|
| 35 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 36 |
+
print("Model loaded.")
|
| 37 |
+
return model
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Model loading failed: {e}")
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
+
model = load_model()
|
| 43 |
+
|
| 44 |
+
# --- 2. Prediction function exposed to Gradio ---
|
| 45 |
+
def predict_captcha(image: Image.Image) -> str:
|
| 46 |
+
if model is None:
|
| 47 |
+
return "Error: Model not loaded."
|
| 48 |
+
|
| 49 |
+
img = image.resize((200, 50))
|
| 50 |
+
img_array = np.array(img).astype(np.float32)
|
| 51 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 52 |
+
prediction = model.predict(img_array, verbose=0)
|
| 53 |
+
return decode_prediction(prediction, int_to_char)
|
| 54 |
+
|
| 55 |
+
# --- 3. Create and launch Gradio interface ---
|
| 56 |
+
iface = gr.Interface(
|
| 57 |
+
fn=predict_captcha,
|
| 58 |
+
inputs=gr.Image(type="pil", label="Upload Captcha Image"),
|
| 59 |
+
outputs=gr.Textbox(label="Predicted Captcha"),
|
| 60 |
+
title="Captcha Recognition",
|
| 61 |
+
description="Upload a captcha image (200x50 pixels expected).",
|
| 62 |
+
allow_flagging="never"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Only required locally; not needed on Hugging Face Spaces.
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|