Commit
·
fc8ee1e
1
Parent(s):
a5b4ed4
Fix runtime timeout error: implement lazy client initialization with retry logic
Browse files- .gitignore +12 -0
- app.py +43 -6
.gitignore
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.history/
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.pyc
|
| 4 |
+
*.pyo
|
| 5 |
+
*.pyd
|
| 6 |
+
.Python
|
| 7 |
+
env/
|
| 8 |
+
venv/
|
| 9 |
+
.env
|
| 10 |
+
static/
|
| 11 |
+
*.log
|
| 12 |
+
|
app.py
CHANGED
|
@@ -14,8 +14,35 @@ load_dotenv()
|
|
| 14 |
# Initialize FastAPI
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
-
# Initialize Hugging Face Client (background remover Space)
|
| 18 |
-
client =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Create static folder to store files
|
| 21 |
os.makedirs("static", exist_ok=True)
|
|
@@ -64,10 +91,20 @@ async def bg_remove(
|
|
| 64 |
f.write(await file.read())
|
| 65 |
|
| 66 |
# Call Hugging Face Space via gradio_client (returns transparent PNG)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# Ensure output has .png extension
|
| 73 |
output_id = f"bgremoved_{os.path.splitext(file_id)[0]}.png"
|
|
|
|
| 14 |
# Initialize FastAPI
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
+
# Initialize Hugging Face Client (background remover Space) - lazy initialization
|
| 18 |
+
client = None
|
| 19 |
+
|
| 20 |
+
def get_client():
|
| 21 |
+
"""Lazy initialization of Gradio Client with timeout handling"""
|
| 22 |
+
global client
|
| 23 |
+
if client is None:
|
| 24 |
+
max_retries = 3
|
| 25 |
+
retry_delay = 2 # seconds
|
| 26 |
+
|
| 27 |
+
for attempt in range(max_retries):
|
| 28 |
+
try:
|
| 29 |
+
# Initialize with longer timeout
|
| 30 |
+
# Note: timeout parameter may vary by gradio_client version
|
| 31 |
+
client = Client(
|
| 32 |
+
"LogicGoInfotechSpaces/background-remover",
|
| 33 |
+
timeout=60.0 # 60 second timeout
|
| 34 |
+
)
|
| 35 |
+
print(f"Gradio Client initialized successfully (attempt {attempt + 1})")
|
| 36 |
+
break
|
| 37 |
+
except Exception as e:
|
| 38 |
+
if attempt < max_retries - 1:
|
| 39 |
+
print(f"Error initializing Gradio Client (attempt {attempt + 1}/{max_retries}): {e}")
|
| 40 |
+
print(f"Retrying in {retry_delay} seconds...")
|
| 41 |
+
time.sleep(retry_delay)
|
| 42 |
+
else:
|
| 43 |
+
print(f"Failed to initialize Gradio Client after {max_retries} attempts: {e}")
|
| 44 |
+
raise
|
| 45 |
+
return client
|
| 46 |
|
| 47 |
# Create static folder to store files
|
| 48 |
os.makedirs("static", exist_ok=True)
|
|
|
|
| 91 |
f.write(await file.read())
|
| 92 |
|
| 93 |
# Call Hugging Face Space via gradio_client (returns transparent PNG)
|
| 94 |
+
try:
|
| 95 |
+
hf_client = get_client()
|
| 96 |
+
result_path = hf_client.predict(
|
| 97 |
+
f=handle_file(input_path),
|
| 98 |
+
api_name="/png"
|
| 99 |
+
)
|
| 100 |
+
except Exception as e:
|
| 101 |
+
# Clean up input file on error
|
| 102 |
+
if os.path.exists(input_path):
|
| 103 |
+
os.remove(input_path)
|
| 104 |
+
raise HTTPException(
|
| 105 |
+
status_code=503,
|
| 106 |
+
detail=f"Background removal service unavailable: {str(e)}"
|
| 107 |
+
)
|
| 108 |
|
| 109 |
# Ensure output has .png extension
|
| 110 |
output_id = f"bgremoved_{os.path.splitext(file_id)[0]}.png"
|