Spaces:
Sleeping
Sleeping
Updated app.py
Browse files
app.py
CHANGED
|
@@ -16,43 +16,43 @@ def register_vocabulary_in_main():
|
|
| 16 |
logger.info("Successfully registered classes in __main__")
|
| 17 |
except Exception as e:
|
| 18 |
logger.warning(f"Could not register classes in __main__: {e}")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
for directory in nltk_dirs:
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
-
logger.warning(f"
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
logger.info("NLTK punkt tokenizer found!")
|
| 41 |
-
return
|
| 42 |
-
except LookupError:
|
| 43 |
-
# Not found, try to download to different locations
|
| 44 |
-
for directory in nltk_dirs:
|
| 45 |
-
try:
|
| 46 |
-
logger.info(f"Attempting to download punkt tokenizer to {directory}")
|
| 47 |
-
nltk.download('punkt', download_dir=directory)
|
| 48 |
-
logger.info(f"Successfully downloaded punkt tokenizer to {directory}")
|
| 49 |
-
return
|
| 50 |
-
except Exception as e:
|
| 51 |
-
logger.warning(f"Failed to download punkt to {directory}: {e}")
|
| 52 |
-
|
| 53 |
-
# If we get here, we couldn't download punkt anywhere
|
| 54 |
-
logger.error("Could not download NLTK punkt tokenizer to any location")
|
| 55 |
-
logger.error("The application may not function correctly")
|
| 56 |
"""
|
| 57 |
Main application entry point for Image Captioning API
|
| 58 |
"""
|
|
|
|
| 16 |
logger.info("Successfully registered classes in __main__")
|
| 17 |
except Exception as e:
|
| 18 |
logger.warning(f"Could not register classes in __main__: {e}")
|
| 19 |
+
def setup_nltk():
|
| 20 |
+
"""Set up NLTK data directory and ensure punkt tokenizer is available"""
|
| 21 |
+
logger.info("Setting up NLTK...")
|
| 22 |
+
|
| 23 |
+
# Create potential NLTK data directories with proper permissions
|
| 24 |
+
nltk_dirs = [
|
| 25 |
+
os.path.expanduser('~/.nltk_data'),
|
| 26 |
+
'./nltk_data',
|
| 27 |
+
'/usr/local/share/nltk_data'
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
for directory in nltk_dirs:
|
| 31 |
+
try:
|
| 32 |
+
os.makedirs(directory, exist_ok=True)
|
| 33 |
+
logger.info(f"Created NLTK data directory: {directory}")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
logger.warning(f"Could not create NLTK directory {directory}: {e}")
|
| 36 |
+
|
| 37 |
+
# Try to find punkt tokenizer
|
| 38 |
+
try:
|
| 39 |
+
nltk.data.find('tokenizers/punkt')
|
| 40 |
+
logger.info("NLTK punkt tokenizer found!")
|
| 41 |
+
return
|
| 42 |
+
except LookupError:
|
| 43 |
+
# Not found, try to download to different locations
|
| 44 |
for directory in nltk_dirs:
|
| 45 |
try:
|
| 46 |
+
logger.info(f"Attempting to download punkt tokenizer to {directory}")
|
| 47 |
+
nltk.download('punkt', download_dir=directory)
|
| 48 |
+
logger.info(f"Successfully downloaded punkt tokenizer to {directory}")
|
| 49 |
+
return
|
| 50 |
except Exception as e:
|
| 51 |
+
logger.warning(f"Failed to download punkt to {directory}: {e}")
|
| 52 |
|
| 53 |
+
# If we get here, we couldn't download punkt anywhere
|
| 54 |
+
logger.error("Could not download NLTK punkt tokenizer to any location")
|
| 55 |
+
logger.error("The application may not function correctly")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
"""
|
| 57 |
Main application entry point for Image Captioning API
|
| 58 |
"""
|