raghav commited on
Commit
8773f23
·
1 Parent(s): 1d4ebe5

Fix backend client initialization (lazy loading)

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -8,12 +8,18 @@ from gradio_client import Client
8
  from huggingface_hub import login
9
  import os
10
 
11
- # Authenticate and connect to protected backend
12
- HF_TOKEN = os.getenv("HF_TOKEN")
13
- if HF_TOKEN:
14
- login(token=HF_TOKEN)
15
 
16
- backend = Client("rg321/Construction_Material_Classification")
 
 
 
 
 
 
 
 
17
 
18
 
19
  def classify_material(image):
@@ -25,7 +31,8 @@ def classify_material(image):
25
  print(f"📤 Calling backend API with image type: {type(image)}")
26
 
27
  # Call protected space API
28
- result = backend.predict(image=image, api_name="/classify_material")
 
29
 
30
  print(f"📥 Backend response: {result}")
31
 
 
8
  from huggingface_hub import login
9
  import os
10
 
11
+ # Global backend client
12
+ backend = None
 
 
13
 
14
+ def get_backend():
15
+ """Lazy initialization of backend client"""
16
+ global backend
17
+ if backend is None:
18
+ HF_TOKEN = os.getenv("HF_TOKEN")
19
+ if HF_TOKEN:
20
+ login(token=HF_TOKEN)
21
+ backend = Client("rg321/Construction_Material_Classification")
22
+ return backend
23
 
24
 
25
  def classify_material(image):
 
31
  print(f"📤 Calling backend API with image type: {type(image)}")
32
 
33
  # Call protected space API
34
+ client = get_backend()
35
+ result = client.predict(image=image, api_name="/classify_material")
36
 
37
  print(f"📥 Backend response: {result}")
38