Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,13 +21,15 @@ MAX_IMAGE_SIZE = 1024
|
|
| 21 |
text_encoder_client = Client("Gemini899/mistral-text-encoder")
|
| 22 |
|
| 23 |
# ============================================================================
|
| 24 |
-
# HARDCODED PROMPTS -
|
| 25 |
# ============================================================================
|
| 26 |
|
| 27 |
HARDCODED_PROMPTS = {
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
# Pre-load embeddings at startup
|
|
@@ -43,11 +45,11 @@ def load_cached_embeddings():
|
|
| 43 |
}
|
| 44 |
|
| 45 |
for key, filename in embedding_files.items():
|
| 46 |
-
# Try multiple possible paths
|
| 47 |
possible_paths = [
|
| 48 |
filename, # Current directory
|
| 49 |
f"/home/user/app/{filename}", # HF Spaces app directory
|
| 50 |
-
os.path.join(os.path.dirname(__file__), filename), # Same dir as script
|
| 51 |
]
|
| 52 |
|
| 53 |
for path in possible_paths:
|
|
@@ -55,11 +57,12 @@ def load_cached_embeddings():
|
|
| 55 |
try:
|
| 56 |
_cached_embeddings[key] = torch.load(path, map_location='cpu')
|
| 57 |
print(f"✓ Loaded cached embedding: {key} from {path}")
|
|
|
|
| 58 |
break
|
| 59 |
except Exception as e:
|
| 60 |
print(f"✗ Error loading {path}: {e}")
|
| 61 |
else:
|
| 62 |
-
print(f"⚠ Warning: {filename} not found - will use API for '{key}' prompt")
|
| 63 |
|
| 64 |
def normalize_prompt(prompt: str) -> str:
|
| 65 |
"""Normalize prompt by stripping whitespace for comparison."""
|
|
@@ -75,12 +78,13 @@ def get_cached_embedding(prompt: str) -> torch.Tensor | None:
|
|
| 75 |
for key, hardcoded_prompt in HARDCODED_PROMPTS.items():
|
| 76 |
if normalized_input == normalize_prompt(hardcoded_prompt):
|
| 77 |
if key in _cached_embeddings:
|
| 78 |
-
print(f"⚡ Exact match found: using cached '{key}' embedding (
|
| 79 |
return _cached_embeddings[key]
|
| 80 |
else:
|
| 81 |
-
print(f"⚠ Exact match for '{key}' but no cached embedding - using API")
|
| 82 |
return None
|
| 83 |
|
|
|
|
| 84 |
return None
|
| 85 |
|
| 86 |
def remote_text_encoder(prompts):
|
|
@@ -94,7 +98,7 @@ def remote_text_encoder(prompts):
|
|
| 94 |
if cached is not None:
|
| 95 |
return cached
|
| 96 |
|
| 97 |
-
# Not an exact match - use API
|
| 98 |
print(f"🌐 Calling Mistral API for prompt encoding...")
|
| 99 |
result = text_encoder_client.predict(
|
| 100 |
prompt=prompts,
|
|
@@ -104,7 +108,10 @@ def remote_text_encoder(prompts):
|
|
| 104 |
return prompt_embeds
|
| 105 |
|
| 106 |
# Load cached embeddings at startup
|
|
|
|
|
|
|
| 107 |
load_cached_embeddings()
|
|
|
|
| 108 |
|
| 109 |
# ============================================================================
|
| 110 |
# Model Loading
|
|
|
|
| 21 |
text_encoder_client = Client("Gemini899/mistral-text-encoder")
|
| 22 |
|
| 23 |
# ============================================================================
|
| 24 |
+
# HARDCODED PROMPTS - EXACT match from depth_logic.py (bypasses Mistral API)
|
| 25 |
# ============================================================================
|
| 26 |
|
| 27 |
HARDCODED_PROMPTS = {
|
| 28 |
+
# From call_flux2_dev_relief_generation() in depth_logic.py
|
| 29 |
+
"relief": "Ignore all shadows, Clay bas-relief sculpture. PRESERVE exact facial features and proportions. Uniform matte gray material, NO black areas, NO dark shadows, NO outlines. Soft smooth depth only. Light gray to white tones. Like carved marble or clay relief. Keep nostrils and area under nose BRIGHT and light-toned, NOT dark pits.",
|
| 30 |
|
| 31 |
+
# From call_flux2_dev_detail_generation() in depth_logic.py
|
| 32 |
+
"details": "Preserve exact pixel alignment. Enhance this depth map by adding surface micro-details (skin pores, fabric texture, hair strands, stone grain, wrinkles, fingernails, knuckles) using ONLY tonal variations within ±10% of local gray values. Keep EXACT same outline, silhouette, and overall tonal range. NO shadows, NO reflections, NO new light sources, NO dark areas under nose/eyes/lips. Output must overlay perfectly on original as bump map detail layer."
|
| 33 |
}
|
| 34 |
|
| 35 |
# Pre-load embeddings at startup
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
for key, filename in embedding_files.items():
|
| 48 |
+
# Try multiple possible paths for HuggingFace Spaces
|
| 49 |
possible_paths = [
|
| 50 |
filename, # Current directory
|
| 51 |
f"/home/user/app/{filename}", # HF Spaces app directory
|
| 52 |
+
os.path.join(os.path.dirname(os.path.abspath(__file__)), filename), # Same dir as script
|
| 53 |
]
|
| 54 |
|
| 55 |
for path in possible_paths:
|
|
|
|
| 57 |
try:
|
| 58 |
_cached_embeddings[key] = torch.load(path, map_location='cpu')
|
| 59 |
print(f"✓ Loaded cached embedding: {key} from {path}")
|
| 60 |
+
print(f" Shape: {_cached_embeddings[key].shape}, Dtype: {_cached_embeddings[key].dtype}")
|
| 61 |
break
|
| 62 |
except Exception as e:
|
| 63 |
print(f"✗ Error loading {path}: {e}")
|
| 64 |
else:
|
| 65 |
+
print(f"⚠ Warning: {filename} not found - will use Mistral API for '{key}' prompt")
|
| 66 |
|
| 67 |
def normalize_prompt(prompt: str) -> str:
|
| 68 |
"""Normalize prompt by stripping whitespace for comparison."""
|
|
|
|
| 78 |
for key, hardcoded_prompt in HARDCODED_PROMPTS.items():
|
| 79 |
if normalized_input == normalize_prompt(hardcoded_prompt):
|
| 80 |
if key in _cached_embeddings:
|
| 81 |
+
print(f"⚡ Exact match found: using cached '{key}' embedding (NO Mistral API call)")
|
| 82 |
return _cached_embeddings[key]
|
| 83 |
else:
|
| 84 |
+
print(f"⚠ Exact match for '{key}' but no cached embedding file - using Mistral API")
|
| 85 |
return None
|
| 86 |
|
| 87 |
+
# No match found
|
| 88 |
return None
|
| 89 |
|
| 90 |
def remote_text_encoder(prompts):
|
|
|
|
| 98 |
if cached is not None:
|
| 99 |
return cached
|
| 100 |
|
| 101 |
+
# Not an exact match - use Mistral API
|
| 102 |
print(f"🌐 Calling Mistral API for prompt encoding...")
|
| 103 |
result = text_encoder_client.predict(
|
| 104 |
prompt=prompts,
|
|
|
|
| 108 |
return prompt_embeds
|
| 109 |
|
| 110 |
# Load cached embeddings at startup
|
| 111 |
+
print("="*60)
|
| 112 |
+
print("Loading cached prompt embeddings...")
|
| 113 |
load_cached_embeddings()
|
| 114 |
+
print("="*60)
|
| 115 |
|
| 116 |
# ============================================================================
|
| 117 |
# Model Loading
|