Spaces:
Sleeping
Sleeping
manishw7 commited on
Commit ·
99ff973
1
Parent(s): 795670b
Robust: Bypass PEFT generate wrapper to fix inference crash
Browse files
app.py
CHANGED
|
@@ -15,6 +15,7 @@ IS_SPACE = "SPACE_ID" in os.environ
|
|
| 15 |
print(f"System: Loading model... (Env: {'Hugging Face Space' if IS_SPACE else 'Local'})")
|
| 16 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
|
|
|
|
| 18 |
try:
|
| 19 |
processor = TrOCRProcessor.from_pretrained(BASE_MODEL_ID)
|
| 20 |
except Exception:
|
|
@@ -22,7 +23,9 @@ except Exception:
|
|
| 22 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
|
| 23 |
processor = TrOCRProcessor(image_processor=image_processor, tokenizer=tokenizer)
|
| 24 |
|
|
|
|
| 25 |
base_model = VisionEncoderDecoderModel.from_pretrained(BASE_MODEL_ID)
|
|
|
|
| 26 |
model = PeftModel.from_pretrained(base_model, ADAPTER_ID)
|
| 27 |
model.to(device)
|
| 28 |
model.eval()
|
|
@@ -35,13 +38,20 @@ def predict(image):
|
|
| 35 |
image = image.convert("RGB")
|
| 36 |
pixel_values = processor(image, return_tensors="pt").pixel_values.to(device)
|
| 37 |
|
| 38 |
-
# ---
|
| 39 |
-
#
|
|
|
|
|
|
|
| 40 |
with torch.no_grad():
|
| 41 |
-
generated_ids = model.generate(
|
|
|
|
|
|
|
|
|
|
| 42 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 43 |
return generated_text
|
| 44 |
except Exception as e:
|
|
|
|
|
|
|
| 45 |
return f"Error during inference: {str(e)}"
|
| 46 |
|
| 47 |
# Interface setup
|
|
|
|
| 15 |
print(f"System: Loading model... (Env: {'Hugging Face Space' if IS_SPACE else 'Local'})")
|
| 16 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
|
| 18 |
+
# Load Processor
|
| 19 |
try:
|
| 20 |
processor = TrOCRProcessor.from_pretrained(BASE_MODEL_ID)
|
| 21 |
except Exception:
|
|
|
|
| 23 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
|
| 24 |
processor = TrOCRProcessor(image_processor=image_processor, tokenizer=tokenizer)
|
| 25 |
|
| 26 |
+
# Load Model
|
| 27 |
base_model = VisionEncoderDecoderModel.from_pretrained(BASE_MODEL_ID)
|
| 28 |
+
# The PeftModel wrapper injects weights into base_model
|
| 29 |
model = PeftModel.from_pretrained(base_model, ADAPTER_ID)
|
| 30 |
model.to(device)
|
| 31 |
model.eval()
|
|
|
|
| 38 |
image = image.convert("RGB")
|
| 39 |
pixel_values = processor(image, return_tensors="pt").pixel_values.to(device)
|
| 40 |
|
| 41 |
+
# --- THE ROBUST FIX ---
|
| 42 |
+
# We call .base_model.generate() directly.
|
| 43 |
+
# This bypasses the buggy PEFT wrapper while still using the LoRA weights.
|
| 44 |
+
# We also add max_new_tokens for a better result.
|
| 45 |
with torch.no_grad():
|
| 46 |
+
generated_ids = model.base_model.generate(
|
| 47 |
+
pixel_values=pixel_values,
|
| 48 |
+
max_new_tokens=64
|
| 49 |
+
)
|
| 50 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 51 |
return generated_text
|
| 52 |
except Exception as e:
|
| 53 |
+
import traceback
|
| 54 |
+
print(traceback.format_exc()) # Log the full error to the Space logs
|
| 55 |
return f"Error during inference: {str(e)}"
|
| 56 |
|
| 57 |
# Interface setup
|