Transcendental-Programmer
commited on
Commit
·
c9ea389
1
Parent(s):
d2e666f
fix: module import error
Browse files- Dockerfile +3 -5
- faceforge_api/main.py +43 -5
- faceforge_core/__init__.py +42 -15
- requirements.txt +12 -8
Dockerfile
CHANGED
@@ -1,7 +1,4 @@
|
|
1 |
-
FROM
|
2 |
-
|
3 |
-
# Install dependencies
|
4 |
-
RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
|
5 |
|
6 |
# Set working directory
|
7 |
WORKDIR /app
|
@@ -11,6 +8,7 @@ COPY requirements.txt ./
|
|
11 |
|
12 |
# Install dependencies
|
13 |
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
14 |
|
15 |
# Copy application code
|
16 |
COPY . .
|
@@ -22,7 +20,7 @@ EXPOSE 7860
|
|
22 |
ENV PYTHONPATH="/app"
|
23 |
ENV PYTHONUNBUFFERED=1
|
24 |
ENV API_URL="/api"
|
25 |
-
ENV MOCK_API="
|
26 |
|
27 |
# Start app (with the patch applied)
|
28 |
CMD ["python", "app.py"]
|
|
|
1 |
+
FROM huggingface/transformers:latest
|
|
|
|
|
|
|
2 |
|
3 |
# Set working directory
|
4 |
WORKDIR /app
|
|
|
8 |
|
9 |
# Install dependencies
|
10 |
RUN pip install --no-cache-dir -r requirements.txt
|
11 |
+
RUN pip install --no-cache-dir transformers
|
12 |
|
13 |
# Copy application code
|
14 |
COPY . .
|
|
|
20 |
ENV PYTHONPATH="/app"
|
21 |
ENV PYTHONUNBUFFERED=1
|
22 |
ENV API_URL="/api"
|
23 |
+
ENV MOCK_API="true"
|
24 |
|
25 |
# Start app (with the patch applied)
|
26 |
CMD ["python", "app.py"]
|
faceforge_api/main.py
CHANGED
@@ -12,9 +12,17 @@ import io
|
|
12 |
from PIL import Image
|
13 |
import json
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# Configure logging
|
20 |
logging.basicConfig(
|
@@ -47,6 +55,35 @@ class AttributeDirectionRequest(BaseModel):
|
|
47 |
labels: Optional[List[int]] = Field(None)
|
48 |
n_components: Optional[int] = 10
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
# --- FastAPI app ---
|
51 |
|
52 |
app = FastAPI(
|
@@ -67,7 +104,7 @@ app.add_middleware(
|
|
67 |
)
|
68 |
|
69 |
# Global explorer instance
|
70 |
-
explorer = LatentSpaceExplorer()
|
71 |
|
72 |
# Error handling middleware
|
73 |
@app.middleware("http")
|
@@ -165,7 +202,8 @@ def attribute_direction(req: AttributeDirectionRequest):
|
|
165 |
try:
|
166 |
logger.debug(f"Attribute direction request: {json.dumps(req.dict(), default=str)}")
|
167 |
latents = np.array(req.latents)
|
168 |
-
|
|
|
169 |
|
170 |
if req.labels is not None:
|
171 |
logger.debug("Using classifier-based direction finding")
|
|
|
12 |
from PIL import Image
|
13 |
import json
|
14 |
|
15 |
+
# Try to import core modules but handle failures gracefully
|
16 |
+
try:
|
17 |
+
import faceforge_core
|
18 |
+
from faceforge_core.latent_explorer import LatentSpaceExplorer
|
19 |
+
from faceforge_core.attribute_directions import LatentDirectionFinder
|
20 |
+
from faceforge_core.custom_loss import attribute_preserving_loss
|
21 |
+
HAS_CORE = True
|
22 |
+
except ImportError as e:
|
23 |
+
logging.warning(f"Failed to import faceforge_core modules: {e}")
|
24 |
+
logging.warning("Using mock implementations instead")
|
25 |
+
HAS_CORE = False
|
26 |
|
27 |
# Configure logging
|
28 |
logging.basicConfig(
|
|
|
55 |
labels: Optional[List[int]] = Field(None)
|
56 |
n_components: Optional[int] = 10
|
57 |
|
58 |
+
# --- Mock classes if core modules aren't available ---
|
59 |
+
|
60 |
+
class MockLatentSpaceExplorer:
|
61 |
+
def __init__(self):
|
62 |
+
self.points = []
|
63 |
+
logger.warning("Using mock LatentSpaceExplorer")
|
64 |
+
|
65 |
+
def add_point(self, text, encoding=None, xy_pos=None):
|
66 |
+
logger.debug(f"Mock add_point: {text}")
|
67 |
+
self.points.append({"text": text, "xy_pos": xy_pos})
|
68 |
+
|
69 |
+
def sample_encoding(self, player_pos, mode="distance"):
|
70 |
+
logger.debug(f"Mock sample_encoding: {player_pos}, {mode}")
|
71 |
+
# Return a dummy encoding
|
72 |
+
return np.random.randn(1, 4, 64, 64)
|
73 |
+
|
74 |
+
class MockLatentDirectionFinder:
|
75 |
+
def __init__(self, latents):
|
76 |
+
self.latents = latents
|
77 |
+
logger.warning("Using mock LatentDirectionFinder")
|
78 |
+
|
79 |
+
def classifier_direction(self, labels):
|
80 |
+
return np.random.randn(512)
|
81 |
+
|
82 |
+
def pca_direction(self, n_components=10):
|
83 |
+
components = np.random.randn(n_components, 512)
|
84 |
+
explained = np.random.rand(n_components)
|
85 |
+
return components, explained
|
86 |
+
|
87 |
# --- FastAPI app ---
|
88 |
|
89 |
app = FastAPI(
|
|
|
104 |
)
|
105 |
|
106 |
# Global explorer instance
|
107 |
+
explorer = LatentSpaceExplorer() if HAS_CORE else MockLatentSpaceExplorer()
|
108 |
|
109 |
# Error handling middleware
|
110 |
@app.middleware("http")
|
|
|
202 |
try:
|
203 |
logger.debug(f"Attribute direction request: {json.dumps(req.dict(), default=str)}")
|
204 |
latents = np.array(req.latents)
|
205 |
+
|
206 |
+
finder = LatentDirectionFinder(latents) if HAS_CORE else MockLatentDirectionFinder(latents)
|
207 |
|
208 |
if req.labels is not None:
|
209 |
logger.debug("Using classifier-based direction finding")
|
faceforge_core/__init__.py
CHANGED
@@ -1,19 +1,46 @@
|
|
1 |
from typing import List
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
import
|
10 |
-
from
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
@dataclass
|
19 |
class GameConfig:
|
|
|
1 |
from typing import List
|
2 |
+
import logging
|
3 |
+
|
4 |
+
logging.basicConfig(level=logging.DEBUG)
|
5 |
+
logger = logging.getLogger("faceforge_core")
|
6 |
+
|
7 |
+
try:
|
8 |
+
from .latent_explorer import LatentSpaceExplorer
|
9 |
+
from .attribute_directions import LatentDirectionFinder
|
10 |
+
from .custom_loss import attribute_preserving_loss
|
11 |
+
from .game_objects import Point, TextPrompt
|
12 |
+
HAS_CORE_MODULES = True
|
13 |
+
except ImportError as e:
|
14 |
+
logger.warning(f"Failed to import core modules: {e}")
|
15 |
+
logger.warning("Some faceforge_core functionality will be unavailable")
|
16 |
+
HAS_CORE_MODULES = False
|
17 |
+
|
18 |
+
try:
|
19 |
+
from dataclasses import dataclass
|
20 |
+
from .fast_sd import fast_diffusion_pipeline
|
21 |
+
HAS_DIFFUSION = True
|
22 |
+
except ImportError as e:
|
23 |
+
logger.warning(f"Failed to import diffusion modules: {e}")
|
24 |
+
logger.warning("Diffusion model functionality will be unavailable")
|
25 |
+
HAS_DIFFUSION = False
|
26 |
+
|
27 |
+
# Conditionally import these based on availability
|
28 |
+
try:
|
29 |
+
import torch
|
30 |
+
import pygame
|
31 |
+
import numpy as np
|
32 |
+
import time
|
33 |
+
from PIL import Image
|
34 |
+
except ImportError as e:
|
35 |
+
logger.warning(f"Failed to import dependency: {e}")
|
36 |
+
|
37 |
+
try:
|
38 |
+
from .sampling import (
|
39 |
+
DistanceSampling,
|
40 |
+
CircleSampling
|
41 |
+
)
|
42 |
+
except ImportError as e:
|
43 |
+
logger.warning(f"Failed to import sampling modules: {e}")
|
44 |
|
45 |
@dataclass
|
46 |
class GameConfig:
|
requirements.txt
CHANGED
@@ -1,9 +1,13 @@
|
|
1 |
-
diffusers
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
6 |
gradio==4.44.1
|
7 |
-
pytest
|
8 |
-
scikit-learn
|
9 |
-
|
|
|
|
|
|
1 |
+
diffusers>=0.24.0
|
2 |
+
transformers>=4.35.0
|
3 |
+
pygame>=2.5.0
|
4 |
+
torch>=2.0.0
|
5 |
+
torchvision>=0.15.0
|
6 |
+
fastapi>=0.103.0
|
7 |
+
uvicorn>=0.23.0
|
8 |
gradio==4.44.1
|
9 |
+
pytest>=7.4.0
|
10 |
+
scikit-learn>=1.3.0
|
11 |
+
pillow>=10.0.0
|
12 |
+
numpy>=1.25.0
|
13 |
+
requests>=2.31.0
|