Transcendental-Programmer commited on
Commit
f9f55e2
·
1 Parent(s): c9ea389

fix: base url error

Browse files
Files changed (4) hide show
  1. Dockerfile +1 -0
  2. README.md +24 -12
  3. app.py +6 -0
  4. faceforge_ui/app.py +10 -7
Dockerfile CHANGED
@@ -21,6 +21,7 @@ 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"]
 
21
  ENV PYTHONUNBUFFERED=1
22
  ENV API_URL="/api"
23
  ENV MOCK_API="true"
24
+ ENV BASE_URL=""
25
 
26
  # Start app (with the patch applied)
27
  CMD ["python", "app.py"]
README.md CHANGED
@@ -24,16 +24,19 @@ FaceForge is ready to run as a Gradio app on [Hugging Face Spaces](https://huggi
24
 
25
  ### Example Dockerfile (already included):
26
  ```Dockerfile
27
- FROM python:3.10-slim
28
- RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
29
  WORKDIR /app
30
  COPY requirements.txt ./
31
  RUN pip install --no-cache-dir -r requirements.txt
 
32
  COPY . .
33
  EXPOSE 7860
34
  ENV PYTHONPATH="/app"
35
  ENV PYTHONUNBUFFERED=1
36
- CMD ["python", "main.py"]
 
 
 
37
  ```
38
 
39
  ## Local Development (Optional)
@@ -95,24 +98,33 @@ The application includes a patch that should fix the issue automatically. This p
95
 
96
  If you see errors like:
97
  ```
98
- Request failed: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /generate
99
  ```
100
 
101
- This means the UI can't connect to the API. In the integrated version, the API is available at `/api/generate` rather than a separate server.
102
 
103
- To fix this:
104
- 1. Ensure you're using the integrated version by running `python main.py`
105
- 2. If you need to run the API separately, set the API_URL environment variable:
106
- ```bash
107
- API_URL=http://localhost:8000 python faceforge_ui/app.py
108
- ```
 
 
 
 
 
 
 
109
 
110
  #### Environment Variables
111
 
112
- - `MOCK_API`: Set to "true" to use mock API responses (for testing without API)
113
  - `API_URL`: Override the API endpoint URL
 
114
  - `PORT`: Set the port for the server (default: 7860)
115
 
116
  ## Notes
117
  - The backend and frontend are fully integrated for Spaces deployment.
 
118
  - For custom model integration, edit the core and backend modules as needed.
 
24
 
25
  ### Example Dockerfile (already included):
26
  ```Dockerfile
27
+ FROM huggingface/transformers:latest
 
28
  WORKDIR /app
29
  COPY requirements.txt ./
30
  RUN pip install --no-cache-dir -r requirements.txt
31
+ RUN pip install --no-cache-dir transformers
32
  COPY . .
33
  EXPOSE 7860
34
  ENV PYTHONPATH="/app"
35
  ENV PYTHONUNBUFFERED=1
36
+ ENV API_URL="/api"
37
+ ENV MOCK_API="true"
38
+ ENV BASE_URL=""
39
+ CMD ["python", "app.py"]
40
  ```
41
 
42
  ## Local Development (Optional)
 
98
 
99
  If you see errors like:
100
  ```
101
+ Invalid URL '/api/generate': No scheme supplied. Perhaps you meant https:///api/generate?
102
  ```
103
 
104
+ This indicates an issue with URL formatting. The application should handle this automatically with the following settings:
105
 
106
+ 1. For the integrated app, set `BASE_URL=""` in the environment
107
+ 2. For separate UI/API components, set `BASE_URL="http://localhost:7860"` (or your server URL)
108
+ 3. Using relative URLs within the same server requires the correct base URL configuration
109
+
110
+ The updated app uses proper URL formatting that works in both integrated and separated modes.
111
+
112
+ #### Missing Dependencies
113
+
114
+ If you see errors about missing Python packages like `transformers`, you have options:
115
+
116
+ 1. Install the missing package: `pip install transformers`
117
+ 2. Use mock mode: Set `MOCK_API="true"` in environment variables
118
+ 3. Use the Docker image which includes all dependencies: `docker build -t faceforge .`
119
 
120
  #### Environment Variables
121
 
122
+ - `MOCK_API`: Set to "true" to use mock API responses (for testing without ML dependencies)
123
  - `API_URL`: Override the API endpoint URL
124
+ - `BASE_URL`: Base URL for relative API paths (leave empty for integrated deployment)
125
  - `PORT`: Set the port for the server (default: 7860)
126
 
127
  ## Notes
128
  - The backend and frontend are fully integrated for Spaces deployment.
129
+ - The application will use the actual ML framework when dependencies are available, and fall back to mock implementations when they're missing.
130
  - For custom model integration, edit the core and backend modules as needed.
app.py CHANGED
@@ -58,6 +58,12 @@ def create_app():
58
  logger.info("Mounting API at /api")
59
  app.mount("/api", api_app)
60
 
 
 
 
 
 
 
61
  # Create Gradio UI
62
  logger.info("Creating Gradio UI")
63
  demo = create_demo()
 
58
  logger.info("Mounting API at /api")
59
  app.mount("/api", api_app)
60
 
61
+ # Set BASE_URL to empty string for HF Spaces deployment
62
+ # This ensures the UI makes relative API requests
63
+ if "BASE_URL" not in os.environ:
64
+ os.environ["BASE_URL"] = ""
65
+ logger.info("Setting BASE_URL to empty string for integrated app")
66
+
67
  # Create Gradio UI
68
  logger.info("Creating Gradio UI")
69
  demo = create_demo()
faceforge_ui/app.py CHANGED
@@ -26,7 +26,9 @@ logging.getLogger("gradio_client").setLevel(logging.DEBUG)
26
  # In HF Spaces, we need to use a relative path since both UI and API run on the same server
27
  # For local development with separate servers, the env var can be set to http://localhost:8000
28
  API_URL = os.environ.get("API_URL", "/api")
 
29
  logger.info(f"Using API URL: {API_URL}")
 
30
 
31
  def generate_image(prompts, mode, player_x, player_y):
32
  """Generate an image based on prompts and player position."""
@@ -59,15 +61,16 @@ def generate_image(prompts, mode, player_x, player_y):
59
 
60
  # Determine the base URL for the API
61
  if API_URL.startswith("/"):
62
- # Relative URL, construct the full URL based on the request context
63
- # For Gradio apps, we'll just use a relative path
64
- base_url = API_URL
 
65
  else:
66
  # Absolute URL, use as is
67
- base_url = API_URL
68
-
69
- logger.debug(f"Making request to: {base_url}/generate")
70
- resp = requests.post(f"{base_url}/generate", json=req, timeout=30)
71
  logger.debug(f"API response status: {resp.status_code}")
72
 
73
  if resp.ok:
 
26
  # In HF Spaces, we need to use a relative path since both UI and API run on the same server
27
  # For local development with separate servers, the env var can be set to http://localhost:8000
28
  API_URL = os.environ.get("API_URL", "/api")
29
+ BASE_URL = os.environ.get("BASE_URL", "http://localhost:7860")
30
  logger.info(f"Using API URL: {API_URL}")
31
+ logger.info(f"Using BASE URL: {BASE_URL}")
32
 
33
  def generate_image(prompts, mode, player_x, player_y):
34
  """Generate an image based on prompts and player position."""
 
61
 
62
  # Determine the base URL for the API
63
  if API_URL.startswith("/"):
64
+ # Relative URL, construct the full URL with the base URL
65
+ # Note: BASE_URL should NOT have a trailing slash
66
+ full_url = f"{BASE_URL}{API_URL}/generate"
67
+ logger.debug(f"Constructed full URL: {full_url}")
68
  else:
69
  # Absolute URL, use as is
70
+ full_url = f"{API_URL}/generate"
71
+
72
+ logger.debug(f"Making request to: {full_url}")
73
+ resp = requests.post(full_url, json=req, timeout=30)
74
  logger.debug(f"API response status: {resp.status_code}")
75
 
76
  if resp.ok: