Spaces:
Running
on
Zero
Running
on
Zero
xiaoyuxi
commited on
Commit
·
8a148af
1
Parent(s):
af98c4c
backend
Browse files
app.py
CHANGED
|
@@ -138,75 +138,22 @@ def initialize_backend():
|
|
| 138 |
"""Initialize backend connection using gradio_client"""
|
| 139 |
global backend_client, BACKEND_AVAILABLE
|
| 140 |
|
| 141 |
-
# First check user permissions
|
| 142 |
-
print("🔍 Step 1: Checking user permissions...")
|
| 143 |
-
has_permissions = check_user_permissions()
|
| 144 |
-
|
| 145 |
-
if not has_permissions:
|
| 146 |
-
print("⚠️ Permission check failed, but continuing with connection attempt...")
|
| 147 |
-
|
| 148 |
-
# Then check if the backend space is actually running
|
| 149 |
-
print("🔍 Step 2: Verifying backend space status...")
|
| 150 |
-
space_running = check_backend_space_status()
|
| 151 |
-
|
| 152 |
-
if not space_running:
|
| 153 |
-
print("⚠️ Backend space is not running or accessible")
|
| 154 |
-
print("🔧 Possible solutions:")
|
| 155 |
-
print(" 1. If HTTP 401: Contact the Space owner to add you as collaborator")
|
| 156 |
-
print(" 2. If HTTP 404: Verify the Space URL is correct")
|
| 157 |
-
print(" 3. If sleeping: Wait for the space to wake up")
|
| 158 |
-
print(" 4. Try accessing the Space directly in browser first")
|
| 159 |
-
print(f" 5. Space URL: https://huggingface.co/spaces/{BACKEND_SPACE_URL}")
|
| 160 |
-
|
| 161 |
-
# For now, let's continue and try the gradio_client anyway
|
| 162 |
-
# Sometimes the web page check fails but the API still works
|
| 163 |
-
print("🔄 Continuing with gradio_client attempt despite web check failure...")
|
| 164 |
-
# Don't return False here, let's try the gradio_client connection
|
| 165 |
-
pass
|
| 166 |
-
|
| 167 |
try:
|
| 168 |
-
print(f"🔍 Step 3: Attempting gradio_client connection to: {BACKEND_SPACE_URL}")
|
| 169 |
-
|
| 170 |
-
# Use gradio_client for proper API access
|
| 171 |
from gradio_client import Client
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
print(f"🔧 Backend client: {backend_client}")
|
| 179 |
|
| 180 |
-
#
|
| 181 |
-
|
| 182 |
-
print("🔧 Skipping API config check due to known issues")
|
| 183 |
-
print("🔧 Marking backend as available for direct API calls")
|
| 184 |
BACKEND_AVAILABLE = True
|
| 185 |
return True
|
| 186 |
|
| 187 |
-
except ImportError as import_error:
|
| 188 |
-
print(f"❌ gradio_client import failed: {import_error}")
|
| 189 |
-
print("📦 Please install gradio_client: pip install gradio_client")
|
| 190 |
-
BACKEND_AVAILABLE = False
|
| 191 |
-
return False
|
| 192 |
-
|
| 193 |
except Exception as e:
|
| 194 |
print(f"❌ Backend connection failed: {e}")
|
| 195 |
-
print(f"🔧 Error type: {type(e).__name__}")
|
| 196 |
-
print(f"🔧 Error details: {str(e)}")
|
| 197 |
-
|
| 198 |
-
# Check if it's a specific configuration issue
|
| 199 |
-
if "Could not fetch config" in str(e):
|
| 200 |
-
print("🔧 This might be due to:")
|
| 201 |
-
print(" - Backend Space is not running or deployed")
|
| 202 |
-
print(" - Backend Space is private and requires authentication")
|
| 203 |
-
print(" - Network connectivity issues")
|
| 204 |
-
print(" - Backend Space URL is incorrect")
|
| 205 |
-
|
| 206 |
-
elif "timeout" in str(e).lower():
|
| 207 |
-
print("🔧 Connection timeout - backend might be slow to respond")
|
| 208 |
-
|
| 209 |
-
print("⚠️ Running in standalone mode (some features may be limited)")
|
| 210 |
BACKEND_AVAILABLE = False
|
| 211 |
return False
|
| 212 |
|
|
|
|
| 138 |
"""Initialize backend connection using gradio_client"""
|
| 139 |
global backend_client, BACKEND_AVAILABLE
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
try:
|
|
|
|
|
|
|
|
|
|
| 142 |
from gradio_client import Client
|
| 143 |
|
| 144 |
+
# Connect to HF Space
|
| 145 |
+
if hf_token:
|
| 146 |
+
backend_client = Client(BACKEND_SPACE_URL, hf_token=hf_token)
|
| 147 |
+
else:
|
| 148 |
+
backend_client = Client(BACKEND_SPACE_URL)
|
|
|
|
| 149 |
|
| 150 |
+
# Test the connection
|
| 151 |
+
backend_client.view_api()
|
|
|
|
|
|
|
| 152 |
BACKEND_AVAILABLE = True
|
| 153 |
return True
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
except Exception as e:
|
| 156 |
print(f"❌ Backend connection failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
BACKEND_AVAILABLE = False
|
| 158 |
return False
|
| 159 |
|