crevelop commited on
Commit
1103c27
1 Parent(s): 7d701e3
Files changed (2) hide show
  1. app.py +85 -13
  2. requirements.txt +0 -1
app.py CHANGED
@@ -17,8 +17,8 @@ from trellis.pipelines import TrellisImageTo3DPipeline
17
  from trellis.representations import Gaussian, MeshExtractResult
18
  from trellis.utils import render_utils, postprocessing_utils
19
  from huggingface_hub import InferenceClient
20
- from PIL import Image
21
- import io
22
 
23
  MAX_SEED = np.iinfo(np.int32).max
24
  TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
@@ -106,6 +106,22 @@ def get_seed(randomize_seed: bool, seed: int) -> int:
106
  """
107
  return np.random.randint(0, MAX_SEED) if randomize_seed else seed
108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  def text_to_image(
111
  prompt: str,
@@ -124,24 +140,80 @@ def text_to_image(
124
  if not api_token:
125
  logger.error("API token not found")
126
  raise gr.Error("API token not found. Please set HF_API_TOKEN environment variable.")
 
 
 
 
127
 
128
- client = InferenceClient(
129
- model="black-forest-labs/FLUX.1-schnell",
130
- token=api_token
131
- )
132
 
 
 
 
 
 
 
 
 
 
 
133
  logger.info("Sending request to HF Inference API...")
134
- image = client.text_to_image(
135
- prompt,
136
- height=height,
137
- width=width,
138
- guidance_scale=guidance_scale,
139
- num_inference_steps=num_inference_steps,
140
- )
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  logger.info(f"Successfully created image: {image.size} {image.mode}")
143
  return image
144
 
 
 
 
 
 
 
145
  except Exception as e:
146
  logger.exception("Error in text_to_image")
147
  raise gr.Error(f"Failed to generate image: {str(e)}")
 
17
  from trellis.representations import Gaussian, MeshExtractResult
18
  from trellis.utils import render_utils, postprocessing_utils
19
  from huggingface_hub import InferenceClient
20
+ import requests
21
+ import time
22
 
23
  MAX_SEED = np.iinfo(np.int32).max
24
  TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
 
106
  """
107
  return np.random.randint(0, MAX_SEED) if randomize_seed else seed
108
 
109
+ def test_connection():
110
+ """Test basic internet connectivity"""
111
+ try:
112
+ # Test general internet connectivity
113
+ response = requests.get("https://www.google.com", timeout=5)
114
+ logger.info(f"Google connection test: {response.status_code}")
115
+
116
+ # Test HF connection
117
+ response = requests.get("https://huggingface.co", timeout=5)
118
+ logger.info(f"HuggingFace connection test: {response.status_code}")
119
+
120
+ return True
121
+ except Exception as e:
122
+ logger.error(f"Connection test failed: {str(e)}")
123
+ return False
124
+
125
 
126
  def text_to_image(
127
  prompt: str,
 
140
  if not api_token:
141
  logger.error("API token not found")
142
  raise gr.Error("API token not found. Please set HF_API_TOKEN environment variable.")
143
+
144
+ # Test connection before making the main request
145
+ if not test_connection():
146
+ raise gr.Error("Network connectivity issues detected")
147
 
148
+ headers = {
149
+ "Authorization": f"Bearer {api_token}",
150
+ "Content-Type": "application/json"
151
+ }
152
 
153
+ payload = {
154
+ "inputs": prompt,
155
+ "parameters": {
156
+ "height": height,
157
+ "width": width,
158
+ "guidance_scale": guidance_scale,
159
+ "num_inference_steps": num_inference_steps,
160
+ }
161
+ }
162
+
163
  logger.info("Sending request to HF Inference API...")
 
 
 
 
 
 
 
164
 
165
+ # Use session for better connection handling
166
+ with requests.Session() as session:
167
+ # First test the API endpoint
168
+ try:
169
+ test_response = session.get(
170
+ "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell",
171
+ timeout=5
172
+ )
173
+ logger.info(f"API status test: {test_response.status_code}")
174
+ except Exception as e:
175
+ logger.error(f"API status test failed: {str(e)}")
176
+
177
+ # Main request with retries
178
+ for attempt in range(3):
179
+ try:
180
+ response = session.post(
181
+ "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell",
182
+ headers=headers,
183
+ json=payload,
184
+ timeout=(10, 30) # (connect timeout, read timeout)
185
+ )
186
+ break
187
+ except requests.exceptions.Timeout:
188
+ if attempt == 2: # Last attempt
189
+ raise
190
+ logger.warning(f"Attempt {attempt + 1} timed out, retrying...")
191
+ time.sleep(2) # Wait before retry
192
+
193
+ logger.info(f"Response status code: {response.status_code}")
194
+
195
+ if response.status_code == 503:
196
+ logger.warning("Model is loading...")
197
+ raise gr.Error("The model is currently loading. Please try again in a few moments.")
198
+ elif response.status_code != 200:
199
+ logger.error(f"Error response: {response.text}")
200
+ raise gr.Error(f"API request failed with status {response.status_code}")
201
+
202
+ # Log response content type and size
203
+ logger.info(f"Response content type: {response.headers.get('content-type')}")
204
+ logger.info(f"Response size: {len(response.content)} bytes")
205
+
206
+ # Try to open the image and log success
207
+ image = Image.open(io.BytesIO(response.content))
208
  logger.info(f"Successfully created image: {image.size} {image.mode}")
209
  return image
210
 
211
+ except requests.exceptions.Timeout:
212
+ logger.error("Request timed out")
213
+ raise gr.Error("Request timed out. The server took too long to respond.")
214
+ except requests.exceptions.ConnectionError as e:
215
+ logger.error(f"Connection error: {str(e)}")
216
+ raise gr.Error("Failed to connect to the server. Please check your network connection.")
217
  except Exception as e:
218
  logger.exception("Error in text_to_image")
219
  raise gr.Error(f"Failed to generate image: {str(e)}")
requirements.txt CHANGED
@@ -21,7 +21,6 @@ xformers==0.0.27.post2
21
  spconv-cu120==2.3.6
22
  transformers==4.46.3
23
  gradio_litmodel3d==0.0.1
24
- huggingface-hub>=0.20.3
25
  https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.0.post2/flash_attn-2.7.0.post2+cu12torch2.4cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
26
  https://huggingface.co/spaces/JeffreyXiang/TRELLIS/resolve/main/wheels/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl?download=true
27
  https://huggingface.co/spaces/JeffreyXiang/TRELLIS/resolve/main/wheels/nvdiffrast-0.3.3-cp310-cp310-linux_x86_64.whl?download=true
 
21
  spconv-cu120==2.3.6
22
  transformers==4.46.3
23
  gradio_litmodel3d==0.0.1
 
24
  https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.0.post2/flash_attn-2.7.0.post2+cu12torch2.4cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
25
  https://huggingface.co/spaces/JeffreyXiang/TRELLIS/resolve/main/wheels/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl?download=true
26
  https://huggingface.co/spaces/JeffreyXiang/TRELLIS/resolve/main/wheels/nvdiffrast-0.3.3-cp310-cp310-linux_x86_64.whl?download=true