Nymbo commited on
Commit
03c08ce
·
verified ·
1 Parent(s): c9c9a1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -10
app.py CHANGED
@@ -7,6 +7,7 @@ from huggingface_hub import InferenceClient
7
  import tempfile
8
  import json
9
  import uuid
 
10
 
11
  # Project by Nymbo - Converted to MCP Server
12
 
@@ -14,6 +15,40 @@ import uuid
14
  API_TOKEN = os.getenv("HF_READ_TOKEN")
15
  timeout = 100
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def flux_krea_generate(
18
  prompt: str,
19
  negative_prompt: str = "(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos",
@@ -98,16 +133,9 @@ def flux_krea_generate(
98
  # This matches the format you saw: /gradio_api/file=<file_path>
99
  gradio_file_url = f"/gradio_api/file={temp_file.name}"
100
 
101
- # For hosted spaces, we also need the full URL
102
- # Try to detect if we're running on Spaces
103
- space_name = os.getenv("SPACE_ID")
104
- if space_name:
105
- # Running on HF Spaces
106
- full_url = f"https://{space_name}.hf.space{gradio_file_url}"
107
- else:
108
- # Running locally
109
- server_port = os.getenv("GRADIO_SERVER_PORT", "7860")
110
- full_url = f"http://localhost:{server_port}{gradio_file_url}"
111
 
112
  print(f'\033[1mMCP Generation {generation_id} completed with {provider}!\033[0m')
113
  print(f'🌐 Image accessible at: {full_url}')
 
7
  import tempfile
8
  import json
9
  import uuid
10
+ import re
11
 
12
  # Project by Nymbo - Converted to MCP Server
13
 
 
15
  API_TOKEN = os.getenv("HF_READ_TOKEN")
16
  timeout = 100
17
 
18
+ # Helper: build a public base URL for the running app (HF Spaces or local)
19
+ def _slugify_for_subdomain(s: str) -> str:
20
+ s = s.strip().lower()
21
+ s = s.replace(".", "-").replace("_", "-").replace(" ", "-")
22
+ s = re.sub(r"[^a-z0-9-]", "-", s)
23
+ s = re.sub(r"-+", "-", s).strip("-")
24
+ return s
25
+
26
+ def _build_public_base_url() -> str:
27
+ # Allow explicit override via env
28
+ for var in ("HF_SPACE_URL", "SPACE_URL", "PUBLIC_SPACE_URL"):
29
+ val = os.getenv(var)
30
+ if val:
31
+ return val.rstrip("/")
32
+
33
+ space_id = os.getenv("SPACE_ID")
34
+ if space_id:
35
+ # If a full URL was provided, use it directly
36
+ if space_id.startswith("http://") or space_id.startswith("https://"):
37
+ return space_id.rstrip("/")
38
+ # Typical HF Spaces SPACE_ID is "owner/space-name"
39
+ if "/" in space_id:
40
+ owner, space = space_id.split("/", 1)
41
+ sub = f"{_slugify_for_subdomain(owner)}-{_slugify_for_subdomain(space)}"
42
+ else:
43
+ # Fall back to slugifying the whole string
44
+ sub = _slugify_for_subdomain(space_id)
45
+ return f"https://{sub}.hf.space"
46
+
47
+ # Local fallback
48
+ host = os.getenv("GRADIO_SERVER_NAME", "localhost")
49
+ port = os.getenv("GRADIO_SERVER_PORT", os.getenv("PORT", "7860"))
50
+ return f"http://{host}:{port}"
51
+
52
  def flux_krea_generate(
53
  prompt: str,
54
  negative_prompt: str = "(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos",
 
133
  # This matches the format you saw: /gradio_api/file=<file_path>
134
  gradio_file_url = f"/gradio_api/file={temp_file.name}"
135
 
136
+ # Build a proper public base URL (HF Spaces or local)
137
+ base_url = _build_public_base_url()
138
+ full_url = f"{base_url}{gradio_file_url}"
 
 
 
 
 
 
 
139
 
140
  print(f'\033[1mMCP Generation {generation_id} completed with {provider}!\033[0m')
141
  print(f'🌐 Image accessible at: {full_url}')