Update app.py
Browse files
app.py
CHANGED
|
@@ -22,27 +22,48 @@ import seaborn as sns
|
|
| 22 |
from collections import Counter
|
| 23 |
import pickle
|
| 24 |
from transformers import ResNetForImageClassification
|
|
|
|
| 25 |
|
| 26 |
# Set up logging
|
| 27 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 28 |
|
| 29 |
# Set the working directory to a writable location
|
| 30 |
WORKING_DIR = "/app" # Use /app for temporary files
|
| 31 |
-
PERSISTENT_DIR = "/data" # Use /data for persistent storage
|
| 32 |
os.makedirs(WORKING_DIR, exist_ok=True)
|
| 33 |
-
os.makedirs(PERSISTENT_DIR, exist_ok=True)
|
| 34 |
os.chdir(WORKING_DIR)
|
| 35 |
|
| 36 |
# Verify the current directory
|
| 37 |
logging.info(f"Current working directory: {os.getcwd()}")
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
else:
|
| 47 |
cache = {}
|
| 48 |
|
|
@@ -180,20 +201,13 @@ new_images = process_new_images(new_images)
|
|
| 180 |
# Update the combined DataFrame with extracted text
|
| 181 |
combined_df.update(new_images)
|
| 182 |
|
| 183 |
-
# Save the updated dataset to
|
| 184 |
-
|
| 185 |
-
combined_df.to_csv(output_file, index=False)
|
| 186 |
-
logging.info(f"Updated dataset saved to {output_file}")
|
| 187 |
-
|
| 188 |
-
# Save the cache to persistent storage
|
| 189 |
-
with open(CACHE_FILE, "wb") as f:
|
| 190 |
-
pickle.dump(cache, f)
|
| 191 |
-
logging.info(f"Cache saved to {CACHE_FILE}")
|
| 192 |
|
| 193 |
logging.info("Incremental processing complete!")
|
| 194 |
|
| 195 |
# Load or download the ResNet model from Hugging Face
|
| 196 |
-
MODEL_DIR = os.path.join(
|
| 197 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 198 |
MODEL_PATH = os.path.join(MODEL_DIR, "resnet50-0676ba61.pth")
|
| 199 |
|
|
|
|
| 22 |
from collections import Counter
|
| 23 |
import pickle
|
| 24 |
from transformers import ResNetForImageClassification
|
| 25 |
+
from datasets import load_dataset, Dataset # Hugging Face datasets library
|
| 26 |
|
| 27 |
# Set up logging
|
| 28 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 29 |
|
| 30 |
# Set the working directory to a writable location
|
| 31 |
WORKING_DIR = "/app" # Use /app for temporary files
|
|
|
|
| 32 |
os.makedirs(WORKING_DIR, exist_ok=True)
|
|
|
|
| 33 |
os.chdir(WORKING_DIR)
|
| 34 |
|
| 35 |
# Verify the current directory
|
| 36 |
logging.info(f"Current working directory: {os.getcwd()}")
|
| 37 |
|
| 38 |
+
# Hugging Face dataset name for persistent storage
|
| 39 |
+
DATASET_NAME = "Fred808/data" # Replace with your Hugging Face dataset name
|
| 40 |
|
| 41 |
+
# Function to save data to Hugging Face Hub
|
| 42 |
+
def save_to_hub(data, dataset_name):
|
| 43 |
+
"""Save data to Hugging Face Hub as a dataset."""
|
| 44 |
+
try:
|
| 45 |
+
dataset = Dataset.from_dict(data)
|
| 46 |
+
dataset.push_to_hub(dataset_name)
|
| 47 |
+
logging.info(f"Data saved to Hugging Face Hub: {dataset_name}")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
logging.error(f"Error saving data to Hugging Face Hub: {e}")
|
| 50 |
+
|
| 51 |
+
# Function to load data from Hugging Face Hub
|
| 52 |
+
def load_from_hub(dataset_name):
|
| 53 |
+
"""Load data from Hugging Face Hub."""
|
| 54 |
+
try:
|
| 55 |
+
dataset = load_dataset(dataset_name)
|
| 56 |
+
logging.info(f"Data loaded from Hugging Face Hub: {dataset_name}")
|
| 57 |
+
return dataset["train"].to_dict() # Convert to dictionary
|
| 58 |
+
except Exception as e:
|
| 59 |
+
logging.error(f"Error loading data from Hugging Face Hub: {e}")
|
| 60 |
+
return None
|
| 61 |
+
|
| 62 |
+
# Load cache from Hugging Face Hub
|
| 63 |
+
CACHE_KEY = "image_text_cache"
|
| 64 |
+
cache = load_from_hub(DATASET_NAME)
|
| 65 |
+
if cache and CACHE_KEY in cache:
|
| 66 |
+
cache = cache[CACHE_KEY]
|
| 67 |
else:
|
| 68 |
cache = {}
|
| 69 |
|
|
|
|
| 201 |
# Update the combined DataFrame with extracted text
|
| 202 |
combined_df.update(new_images)
|
| 203 |
|
| 204 |
+
# Save the updated dataset to Hugging Face Hub
|
| 205 |
+
save_to_hub({"data_with_extracted_text": combined_df.to_dict(), CACHE_KEY: cache}, DATASET_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
logging.info("Incremental processing complete!")
|
| 208 |
|
| 209 |
# Load or download the ResNet model from Hugging Face
|
| 210 |
+
MODEL_DIR = os.path.join(WORKING_DIR, "models")
|
| 211 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 212 |
MODEL_PATH = os.path.join(MODEL_DIR, "resnet50-0676ba61.pth")
|
| 213 |
|