Update app.py
Browse files
app.py
CHANGED
|
@@ -13,29 +13,40 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
|
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
# =========================
|
| 16 |
-
#
|
| 17 |
# =========================
|
| 18 |
-
parseq_path = os.path.join(os.path.dirname(__file__), 'parseq')
|
| 19 |
-
if os.path.exists(parseq_path):
|
| 20 |
-
sys.path.insert(0, parseq_path)
|
| 21 |
-
else:
|
| 22 |
-
logger.warning(f"PARSeq folder not found at {parseq_path}, trying direct import")
|
| 23 |
-
|
| 24 |
try:
|
|
|
|
|
|
|
| 25 |
from strhub.data.utils import Tokenizer
|
|
|
|
| 26 |
except ImportError:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
warnings.filterwarnings('ignore')
|
| 41 |
|
|
@@ -121,17 +132,21 @@ def load_model(model_path, lang_name):
|
|
| 121 |
logger.error(f"No charset found for {lang_name}")
|
| 122 |
return None, None, None
|
| 123 |
|
| 124 |
-
#
|
|
|
|
|
|
|
|
|
|
| 125 |
try:
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
except Exception as e:
|
| 129 |
-
logger.error(f"Failed to
|
| 130 |
return None, None, None
|
| 131 |
-
|
| 132 |
-
# Create tokenizer and attach to model
|
| 133 |
-
tokenizer = Tokenizer(charset_str)
|
| 134 |
-
model.tokenizer = tokenizer
|
| 135 |
|
| 136 |
# Handle different checkpoint formats
|
| 137 |
if 'model_state_dict' in checkpoint:
|
|
@@ -159,6 +174,7 @@ def load_model(model_path, lang_name):
|
|
| 159 |
if unexpected_keys:
|
| 160 |
logger.warning(f"Unexpected keys for {lang_name}: {unexpected_keys[:5]}...")
|
| 161 |
|
|
|
|
| 162 |
model = model.to(device)
|
| 163 |
model.eval()
|
| 164 |
|
|
|
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
# =========================
|
| 16 |
+
# Import PARSeq modules directly
|
| 17 |
# =========================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
+
# Try to import from installed parseq package
|
| 20 |
+
from strhub.models.parseq.model import PARSeq
|
| 21 |
from strhub.data.utils import Tokenizer
|
| 22 |
+
logger.info("Successfully imported PARSeq from package")
|
| 23 |
except ImportError:
|
| 24 |
+
try:
|
| 25 |
+
# Try local path
|
| 26 |
+
parseq_path = os.path.join(os.path.dirname(__file__), 'parseq')
|
| 27 |
+
if os.path.exists(parseq_path):
|
| 28 |
+
sys.path.insert(0, parseq_path)
|
| 29 |
+
from strhub.models.parseq.model import PARSeq
|
| 30 |
+
from strhub.data.utils import Tokenizer
|
| 31 |
+
logger.info("Successfully imported PARSeq from local path")
|
| 32 |
+
else:
|
| 33 |
+
logger.error("PARSeq not found")
|
| 34 |
+
raise
|
| 35 |
+
except ImportError as e:
|
| 36 |
+
logger.error(f"Failed to import PARSeq: {e}")
|
| 37 |
+
# Create placeholder classes
|
| 38 |
+
class Tokenizer:
|
| 39 |
+
def __init__(self, charset):
|
| 40 |
+
self.charset = charset
|
| 41 |
+
self._itos = {i: ch for i, ch in enumerate(charset)}
|
| 42 |
+
self._stoi = {ch: i for i, ch in enumerate(charset)}
|
| 43 |
+
self.pad_id = 0
|
| 44 |
+
self.bos_id = 1
|
| 45 |
+
self.eos_id = 2
|
| 46 |
+
|
| 47 |
+
class PARSeq:
|
| 48 |
+
def __init__(self, *args, **kwargs):
|
| 49 |
+
pass
|
| 50 |
|
| 51 |
warnings.filterwarnings('ignore')
|
| 52 |
|
|
|
|
| 132 |
logger.error(f"No charset found for {lang_name}")
|
| 133 |
return None, None, None
|
| 134 |
|
| 135 |
+
# Create tokenizer
|
| 136 |
+
tokenizer = Tokenizer(charset_str)
|
| 137 |
+
|
| 138 |
+
# Create model instance - use PARSeq class directly
|
| 139 |
try:
|
| 140 |
+
# Try to create model with proper arguments
|
| 141 |
+
model = PARSeq(
|
| 142 |
+
charset_size=len(charset_str),
|
| 143 |
+
img_size=(32, 128),
|
| 144 |
+
max_label_length=100
|
| 145 |
+
)
|
| 146 |
+
logger.info(f"Model instance created for {lang_name}")
|
| 147 |
except Exception as e:
|
| 148 |
+
logger.error(f"Failed to create model instance: {e}")
|
| 149 |
return None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
# Handle different checkpoint formats
|
| 152 |
if 'model_state_dict' in checkpoint:
|
|
|
|
| 174 |
if unexpected_keys:
|
| 175 |
logger.warning(f"Unexpected keys for {lang_name}: {unexpected_keys[:5]}...")
|
| 176 |
|
| 177 |
+
model.tokenizer = tokenizer
|
| 178 |
model = model.to(device)
|
| 179 |
model.eval()
|
| 180 |
|