Ds0uz4 commited on
Commit
8d9fa15
·
verified ·
1 Parent(s): c69e248

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -21
app.py CHANGED
@@ -178,13 +178,13 @@ optimizer=torch.optim.Adam(model.parameters())
178
 
179
 
180
 
181
- checkpoint = torch.load(r'model/final.pth', map_location=device)
182
- # Restore states
183
- #print("Checkpoint keys:", checkpoint.keys())
184
- model.load_state_dict(checkpoint['model_state_dict'])
185
 
186
- optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
187
- model.eval()
188
  characters = string.ascii_letters + string.digits
189
  idx_to_char = {idx: char for idx, char in enumerate(characters)}
190
 
@@ -194,26 +194,14 @@ def to_text(arr):
194
  for c in arr:
195
  ans=ans+idx_to_char[c.item()]
196
  return ans
197
-
198
-
199
  def predict_captcha(image):
200
  try:
201
  if image is None:
202
  return "No image provided"
203
 
204
- # Handle Gradio's FileData input: dict with 'data' and 'meta'
205
- if isinstance(image, dict) and 'data' in image:
206
- image = image['data']
207
-
208
- # Convert to PIL.Image
209
- if isinstance(image, str) and image.startswith('data:image'):
210
- import base64
211
- from io import BytesIO
212
- image_data = base64.b64decode(image.split(',')[1])
213
- image = Image.open(BytesIO(image_data))
214
- elif not isinstance(image, Image.Image):
215
- from io import BytesIO
216
- image = Image.open(BytesIO(image))
217
 
218
  # Process image
219
  transform = transforms.Compose([
@@ -236,6 +224,11 @@ def predict_captcha(image):
236
  print(f"Error details: {str(e)}")
237
  return f"Error processing image: {str(e)}"
238
 
 
 
 
 
 
239
 
240
  # Update Gradio interface
241
  iface = gr.Interface(
 
178
 
179
 
180
 
181
+ # checkpoint = torch.load(r'model/final.pth', map_location=device)
182
+ # # Restore states
183
+ # #print("Checkpoint keys:", checkpoint.keys())
184
+ # model.load_state_dict(checkpoint['model_state_dict'])
185
 
186
+ # optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
187
+ # model.eval()
188
  characters = string.ascii_letters + string.digits
189
  idx_to_char = {idx: char for idx, char in enumerate(characters)}
190
 
 
194
  for c in arr:
195
  ans=ans+idx_to_char[c.item()]
196
  return ans
 
 
197
  def predict_captcha(image):
198
  try:
199
  if image is None:
200
  return "No image provided"
201
 
202
+ # Directly use the image as a PIL.Image
203
+ if not isinstance(image, Image.Image):
204
+ return "Invalid image format"
 
 
 
 
 
 
 
 
 
 
205
 
206
  # Process image
207
  transform = transforms.Compose([
 
224
  print(f"Error details: {str(e)}")
225
  return f"Error processing image: {str(e)}"
226
 
227
+ # Ensure model is loaded and set to eval mode
228
+ checkpoint = torch.load(r'model/final.pth', map_location=device)
229
+ model.load_state_dict(checkpoint['model_state_dict'])
230
+ model.eval() # Set the model to evaluation mode after loading the state
231
+
232
 
233
  # Update Gradio interface
234
  iface = gr.Interface(