engasm89 commited on
Commit
93014eb
·
verified ·
1 Parent(s): e9e04b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -3,16 +3,20 @@ from huggingface_hub import InferenceClient, HfApi
3
  from huggingface_hub.utils import HfHubHTTPError
4
  import os
5
 
6
- def check_and_enable_api(model_id):
7
  try:
8
- api = HfApi()
9
- model_info = api.model_info(model_id)
10
- if not model_info.api:
 
 
 
 
 
11
  api.toggle_model_api(model_id, True)
12
  return "API was disabled. Now enabled for the model."
13
- return "API already enabled for the model."
14
- except Exception as e:
15
- return f"Error checking API status: {str(e)}"
16
 
17
  def get_api_status():
18
  token = os.getenv('HF_TOKEN')
@@ -22,11 +26,9 @@ def get_api_status():
22
  return "⚠️ No API token found. Please set HF_TOKEN environment variable."
23
 
24
  try:
25
- status = check_and_enable_api(model_id)
26
  client = InferenceClient(model_id, token=token)
 
27
  return f"✅ Connected to {model_id} | {status}"
28
- except HfHubHTTPError as e:
29
- return f"❌ API Error: {str(e)}"
30
  except Exception as e:
31
  return f"❌ Error: {str(e)}"
32
 
@@ -105,8 +107,8 @@ with gr.Blocks() as demo:
105
  ],
106
  )
107
 
108
- # Add API status at the footer
109
- gr.HTML(
110
  value=f"<div style='text-align: center; padding: 10px; background-color: #f0f0f0; border-top: 1px solid #ddd;'>{get_api_status()}</div>",
111
  every=30 # Updates every 30 seconds
112
  )
 
3
  from huggingface_hub.utils import HfHubHTTPError
4
  import os
5
 
6
+ def check_api_status(model_id):
7
  try:
8
+ client = InferenceClient(model_id)
9
+ # Simple test request to check if API is responsive
10
+ client.health_check()
11
+ return "API is accessible"
12
+ except Exception as e:
13
+ try:
14
+ # Try to enable the API
15
+ api = HfApi()
16
  api.toggle_model_api(model_id, True)
17
  return "API was disabled. Now enabled for the model."
18
+ except Exception as enable_err:
19
+ return f"Could not enable API: {str(enable_err)}"
 
20
 
21
  def get_api_status():
22
  token = os.getenv('HF_TOKEN')
 
26
  return "⚠️ No API token found. Please set HF_TOKEN environment variable."
27
 
28
  try:
 
29
  client = InferenceClient(model_id, token=token)
30
+ status = check_api_status(model_id)
31
  return f"✅ Connected to {model_id} | {status}"
 
 
32
  except Exception as e:
33
  return f"❌ Error: {str(e)}"
34
 
 
107
  ],
108
  )
109
 
110
+ # Add API status at the footer with error handling
111
+ footer = gr.HTML(
112
  value=f"<div style='text-align: center; padding: 10px; background-color: #f0f0f0; border-top: 1px solid #ddd;'>{get_api_status()}</div>",
113
  every=30 # Updates every 30 seconds
114
  )