Upload app.py
Browse files12344 unmodified lines91011121314151610 unmodified lines27282930313233343536373839402 unmodified lines434445464748495045 unmodified lines96979899100101import osimport gradio as grimport torchfrom TeLVE.imagine import ImageCaptioningModel, load_model, generate_caption4 unmodified linesMODELS_DIR = "./TeLVE/models"TOKENIZER_PATH = "./TeLVE/tokenizer"HF_REPO_ID = "outsu/TeLVE"device = torch.device("cuda" if torch.cuda.is_available() else "cpu")def list_available_models():"""List all .pth models in the models directory"""if not os.path.exists(MODELS_DIR):10 unmodified linesreturn []def download_missing_models():"""Download missing models from HuggingFace"""if not os.path.exists(MODELS_DIR):os.makedirs(MODELS_DIR)local_models = set(list_available_models())hf_models = set(get_hf_model_list())for model in hf_models - local_models:try:print(f"Downloading missing model: {model}")hf_hub_download(2 unmodified lineslocal_dir=os.path.dirname(MODELS_DIR),local_dir_use_symlinks=False)except Exception as e:print(f"Error downloading {model}: {str(e)}")def generate_description(image, model_name):"""Generate image caption using selected model"""45 unmodified linesreturn interfaceif __name__ == "__main__":print("Checking for missing models...")download_missing_models()demo = create_interface()123454 unmodified lines1011121314151617181920212223242526272829303110 unmodified lines424344454647484950515253545556575859602 unmodified lines63646566676869707172737475767778798081828345 unmodified lines129130131132133134135136import osimport jsonimport gradio as grimport torchfrom TeLVE.imagine import ImageCaptioningModel, load_model, generate_caption4 unmodified linesMODELS_DIR = "./TeLVE/models"TOKENIZER_PATH = "./TeLVE/tokenizer"HF_REPO_ID = "outsu/TeLVE"MODEL_STATE_FILE = "./TeLVE/model_state.json"device = torch.device("cuda" if torch.cuda.is_available() else "cpu")def load_model_state():"""Load the model state from JSON file"""if os.path.exists(MODEL_STATE_FILE):with open(MODEL_STATE_FILE, 'r') as f:return json.load(f)return {"downloaded_models": []}def save_model_state(state):"""Save the model state to JSON file"""os.makedirs(os.path.dirname(MODEL_STATE_FILE), exist_ok=True)with open(MODEL_STATE_FILE, 'w') as f:json.dump(state, f)def list_available_models():"""List all .pth models in the models directory"""if not os.path.exists(MODELS_DIR):10 unmodified linesreturn []def download_missing_models():"""Download missing models from HuggingFace with state management"""if not os.path.exists(MODELS_DIR):os.makedirs(MODELS_DIR)state = load_model_state()downloaded_models = set(state["downloaded_models"])local_models = set(list_available_models())hf_models = set(get_hf_model_list())# Check for models that need downloadingmodels_to_download = (hf_models - local_models) - downloaded_modelsfor model in models_to_download:try:print(f"Downloading missing model: {model}")hf_hub_download(2 unmodified lineslocal_dir=os.path.dirname(MODELS_DIR),local_dir_use_symlinks=False)downloaded_models.add(model)except Exception as e:print(f"Error downloading {model}: {str(e)}")continue# Update state with newly downloaded modelsstate["downloaded_models"] = list(downloaded_models)save_model_state(state)def verify_model_integrity():"""Verify that all models in state actually exist"""state = load_model_state()local_models = set(list_available_models())state["downloaded_models"] = list(set(state["downloaded_models"]) & local_models)save_model_state(state)def generate_description(image, model_name):"""Generate image caption using selected model"""45 unmodified linesreturn interfaceif __name__ == "__main__":print("Verifying model integrity...")verify_model_integrity()print("Checking for missing models...")download_missing_models()demo = create_interface()
app.py+37-2