Spaces:
Runtime error
Runtime error
msIntui commited on
Commit Β·
e2c1993
1
Parent(s): 910e0d4
feat: update model downloading from Azure Blob Storage and environment configuration
Browse files- .env +33 -0
- download_models.py +50 -41
- gradioChatApp.py +12 -4
.env
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# OpenAI and API Keys
|
| 2 |
+
OPENAI_API_KEY=your_openai_api_key
|
| 3 |
+
TAVILY_API_KEY=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
|
| 4 |
+
LANGCHAIN_API_KEY=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
|
| 5 |
+
LANGCHAIN_TRACING_V2=true
|
| 6 |
+
LANGCHAIN_PROJECT=P&ID agent
|
| 7 |
+
LANGSMITH_API_KEY=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
|
| 8 |
+
|
| 9 |
+
# Storage Configuration
|
| 10 |
+
STORAGE_TYPE=local
|
| 11 |
+
AZURE_STORAGE_CONTAINER_NAME='pnid-models'
|
| 12 |
+
|
| 13 |
+
# Azure Storage Connection Strings
|
| 14 |
+
AZURE_STORAGE_PRIMARY_CONNECTION='DefaultEndpointsProtocol=https;AccountName=pidmodelstorage019659c7a;AccountKey=PYE6QujpTMkciPKLMDsNQzDqCIn331on1cv4MvpRsBPC8ud84jeIVJQoargLuigoN6kdjAICi0u1+AStkTuRSg==;EndpointSuffix=core.windows.net'
|
| 15 |
+
AZURE_STORAGE_SECONDARY_CONNECTION='DefaultEndpointsProtocol=https;AccountName=pidmodelstorage019659c7a;AccountKey=6DS5ApT8Z7pw7tSjavt+S83mX/DJquxf5NuEB2Q2svy9OyubBYh/Hog9jz1wWdKZID8cyul2ecrL+AStVNF9lQ==;EndpointSuffix=core.windows.net'
|
| 16 |
+
|
| 17 |
+
# Runtime Settings
|
| 18 |
+
USE_TORCH=1
|
| 19 |
+
GRADIO_SERVER_PORT=7861
|
| 20 |
+
|
| 21 |
+
# Model Paths
|
| 22 |
+
MODEL_SDM_41_PATH=models/Intui_SDM_41.pt
|
| 23 |
+
MODEL_SDM_30_PATH=models/Intui_SDM_30.pt
|
| 24 |
+
MODEL_SDM_20_PATH=models/Intui_SDM_20.pt
|
| 25 |
+
MODEL_DEEPLSD_PATH=models/deeplsd_md.tar
|
| 26 |
+
MODEL_CRAFT_PATH=models/craft_mlt_25k.pth
|
| 27 |
+
MODEL_ENGLISH_PATH=models/english_g2.pth
|
| 28 |
+
MODEL_LDM_PATH=models/intui_LDM_01.pt
|
| 29 |
+
|
| 30 |
+
# Legacy Model Paths (can be removed if not used)
|
| 31 |
+
# YOLO_MODEL_PATH=models/yolo/yolov8n.pt
|
| 32 |
+
# DEEPLSD_MODEL_PATH=models/deeplsd/deeplsd_md.tar
|
| 33 |
+
# DOCTR_MODEL_PATH=models/doctr/ocr_predictor.pt
|
download_models.py
CHANGED
|
@@ -1,49 +1,58 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import torch
|
| 4 |
-
from doctr.models import ocr_predictor
|
| 5 |
-
from ultralytics import YOLO
|
| 6 |
-
from deeplsd.models.deeplsd_inference import DeepLSD
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
"""
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
+
from azure.storage.blob import BlobServiceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
|
| 5 |
# Load environment variables
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
+
def download_from_azure():
|
| 9 |
+
"""Download models from Azure Blob Storage"""
|
| 10 |
+
try:
|
| 11 |
+
# Try primary connection first, fallback to secondary
|
| 12 |
+
connect_str = os.getenv('AZURE_STORAGE_PRIMARY_CONNECTION')
|
| 13 |
+
if not connect_str:
|
| 14 |
+
connect_str = os.getenv('AZURE_STORAGE_SECONDARY_CONNECTION')
|
| 15 |
+
|
| 16 |
+
container_name = os.getenv('AZURE_STORAGE_CONTAINER_NAME', 'pnid-models')
|
| 17 |
+
|
| 18 |
+
# Create the BlobServiceClient
|
| 19 |
+
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
|
| 20 |
+
container_client = blob_service_client.get_container_client(container_name)
|
| 21 |
+
|
| 22 |
+
# Create models directory
|
| 23 |
+
os.makedirs('models', exist_ok=True)
|
| 24 |
+
|
| 25 |
+
# Define model files to download
|
| 26 |
+
model_files = {
|
| 27 |
+
'Intui_SDM_41.pt': os.getenv('MODEL_SDM_41_PATH'),
|
| 28 |
+
'Intui_SDM_30.pt': os.getenv('MODEL_SDM_30_PATH'),
|
| 29 |
+
'Intui_SDM_20.pt': os.getenv('MODEL_SDM_20_PATH'),
|
| 30 |
+
'deeplsd_md.tar': os.getenv('MODEL_DEEPLSD_PATH'),
|
| 31 |
+
'craft_mlt_25k.pth': os.getenv('MODEL_CRAFT_PATH'),
|
| 32 |
+
'english_g2.pth': os.getenv('MODEL_ENGLISH_PATH'),
|
| 33 |
+
'intui_LDM_01.pt': os.getenv('MODEL_LDM_PATH')
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Download each model
|
| 37 |
+
for blob_name, local_path in model_files.items():
|
| 38 |
+
if not local_path:
|
| 39 |
+
continue
|
| 40 |
+
|
| 41 |
+
print(f"Downloading {blob_name}...")
|
| 42 |
+
os.makedirs(os.path.dirname(local_path), exist_ok=True)
|
| 43 |
+
|
| 44 |
+
if not os.path.exists(local_path) or os.path.getsize(local_path) == 0:
|
| 45 |
+
blob_client = container_client.get_blob_client(blob_name)
|
| 46 |
+
with open(local_path, "wb") as file:
|
| 47 |
+
data = blob_client.download_blob()
|
| 48 |
+
file.write(data.readall())
|
| 49 |
+
print(f"Downloaded {blob_name} to {local_path}")
|
| 50 |
+
else:
|
| 51 |
+
print(f"Skipping {blob_name}, already exists")
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Error downloading models from Azure: {str(e)}")
|
| 55 |
+
raise
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
+
download_from_azure()
|
gradioChatApp.py
CHANGED
|
@@ -25,7 +25,7 @@ import cv2
|
|
| 25 |
import numpy as np
|
| 26 |
import time
|
| 27 |
from huggingface_hub import HfApi, login
|
| 28 |
-
from download_models import
|
| 29 |
|
| 30 |
# Load environment variables from .env file
|
| 31 |
load_dotenv()
|
|
@@ -789,9 +789,17 @@ def create_ui():
|
|
| 789 |
|
| 790 |
|
| 791 |
def main():
|
| 792 |
-
#
|
| 793 |
-
|
| 794 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 795 |
|
| 796 |
demo = create_ui()
|
| 797 |
# Remove HF Spaces conditional, just use local development settings
|
|
|
|
| 25 |
import numpy as np
|
| 26 |
import time
|
| 27 |
from huggingface_hub import HfApi, login
|
| 28 |
+
from download_models import download_from_azure
|
| 29 |
|
| 30 |
# Load environment variables from .env file
|
| 31 |
load_dotenv()
|
|
|
|
| 789 |
|
| 790 |
|
| 791 |
def main():
|
| 792 |
+
# Check for all required models
|
| 793 |
+
required_models = [
|
| 794 |
+
'models/yolo/yolov8n.pt',
|
| 795 |
+
'models/deeplsd/deeplsd_md.tar',
|
| 796 |
+
'models/doctr/craft_mlt_25k.pth',
|
| 797 |
+
'models/doctr/english_g2.pth',
|
| 798 |
+
'models/yolo/intui_LDM_01.pt'
|
| 799 |
+
]
|
| 800 |
+
|
| 801 |
+
if any(not os.path.exists(model) for model in required_models):
|
| 802 |
+
download_from_azure()
|
| 803 |
|
| 804 |
demo = create_ui()
|
| 805 |
# Remove HF Spaces conditional, just use local development settings
|