MedhaCodes commited on
Commit
99cb69f
Β·
verified Β·
1 Parent(s): c1c15d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -15
app.py CHANGED
@@ -1,3 +1,13 @@
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import tensorflow as tf
3
  from tensorflow import keras
@@ -8,15 +18,25 @@ from PIL import Image
8
  # ----------------------------
9
  # PAGE CONFIG
10
  # ----------------------------
11
- st.set_page_config(page_title="Captcha OCR", layout="centered")
12
- st.title("πŸ” Captcha OCR using CRNN + CTC")
13
- st.write("Upload a captcha image and get prediction")
 
 
 
 
 
14
 
15
  # ----------------------------
16
  # LOAD CHARACTERS
17
  # ----------------------------
18
- with open("characters.txt", "r") as f:
19
- characters = list(f.read().strip())
 
 
 
 
 
20
 
21
  charToNum = layers.StringLookup(vocabulary=characters, mask_token=None)
22
  numToChar = layers.StringLookup(
@@ -26,25 +46,31 @@ numToChar = layers.StringLookup(
26
  )
27
 
28
  # ----------------------------
29
- # LOAD MODEL
30
  # ----------------------------
31
  @st.cache_resource
32
  def load_model():
33
- model = keras.models.load_model("ocr_model.keras")
 
 
 
34
  return model
35
 
36
- model = load_model()
 
 
 
 
37
 
38
  # ----------------------------
39
  # PREPROCESS FUNCTION
40
  # ----------------------------
41
  def preprocess_image(image):
42
  image = image.convert("L") # grayscale
43
- image = image.resize((200, 50)) # SAME as training
44
- image = np.array(image)
45
- image = image.astype("float32") / 255.0
46
  image = np.expand_dims(image, axis=-1)
47
- image = np.transpose(image, (1, 0, 2)) # IMPORTANT
48
  image = np.expand_dims(image, axis=0)
49
  return image
50
 
@@ -72,15 +98,20 @@ def decode_prediction(pred):
72
  # ----------------------------
73
  # FILE UPLOADER
74
  # ----------------------------
75
- uploaded_file = st.file_uploader("Upload Captcha Image", type=["png", "jpg", "jpeg"])
 
 
 
76
 
77
  if uploaded_file is not None:
78
  image = Image.open(uploaded_file)
 
79
  st.image(image, caption="Uploaded Image", use_column_width=True)
80
 
81
  processed = preprocess_image(image)
82
 
83
- prediction = model.predict(processed)
84
- text = decode_prediction(prediction)
 
85
 
86
  st.success(f"🎯 Prediction: {text}")
 
1
+ # ----------------------------
2
+ # FIX FOR HUGGING FACE TIMEOUT
3
+ # ----------------------------
4
+ import os
5
+ os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false"
6
+ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
7
+
8
+ # ----------------------------
9
+ # IMPORTS
10
+ # ----------------------------
11
  import streamlit as st
12
  import tensorflow as tf
13
  from tensorflow import keras
 
18
  # ----------------------------
19
  # PAGE CONFIG
20
  # ----------------------------
21
+ st.set_page_config(
22
+ page_title="Captcha OCR",
23
+ page_icon="πŸ”",
24
+ layout="centered"
25
+ )
26
+
27
+ st.title("πŸ” Captcha OCR")
28
+ st.markdown("CRNN + CTC Model Deployment")
29
 
30
  # ----------------------------
31
  # LOAD CHARACTERS
32
  # ----------------------------
33
+ @st.cache_resource
34
+ def load_characters():
35
+ with open("characters.txt", "r") as f:
36
+ characters = list(f.read().strip())
37
+ return characters
38
+
39
+ characters = load_characters()
40
 
41
  charToNum = layers.StringLookup(vocabulary=characters, mask_token=None)
42
  numToChar = layers.StringLookup(
 
46
  )
47
 
48
  # ----------------------------
49
+ # LOAD MODEL (LAZY + SAFE)
50
  # ----------------------------
51
  @st.cache_resource
52
  def load_model():
53
+ model = keras.models.load_model(
54
+ "ocr_model.keras",
55
+ compile=False # IMPORTANT for memory reduction
56
+ )
57
  return model
58
 
59
+ # Lazy loading (prevents reload crash)
60
+ if "model" not in st.session_state:
61
+ st.session_state.model = load_model()
62
+
63
+ model = st.session_state.model
64
 
65
  # ----------------------------
66
  # PREPROCESS FUNCTION
67
  # ----------------------------
68
  def preprocess_image(image):
69
  image = image.convert("L") # grayscale
70
+ image = image.resize((200, 50)) # same as training
71
+ image = np.array(image).astype("float32") / 255.0
 
72
  image = np.expand_dims(image, axis=-1)
73
+ image = np.transpose(image, (1, 0, 2)) # IMPORTANT (match training)
74
  image = np.expand_dims(image, axis=0)
75
  return image
76
 
 
98
  # ----------------------------
99
  # FILE UPLOADER
100
  # ----------------------------
101
+ uploaded_file = st.file_uploader(
102
+ "Upload Captcha Image",
103
+ type=["png", "jpg", "jpeg"]
104
+ )
105
 
106
  if uploaded_file is not None:
107
  image = Image.open(uploaded_file)
108
+
109
  st.image(image, caption="Uploaded Image", use_column_width=True)
110
 
111
  processed = preprocess_image(image)
112
 
113
+ with st.spinner("Predicting..."):
114
+ prediction = model.predict(processed)
115
+ text = decode_prediction(prediction)
116
 
117
  st.success(f"🎯 Prediction: {text}")