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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -3,20 +3,20 @@ from huggingface_hub import InferenceClient, HfApi
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,8 +26,7 @@ def get_api_status():
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)}"
@@ -107,7 +106,7 @@ with gr.Blocks() as demo:
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
 
3
  from huggingface_hub.utils import HfHubHTTPError
4
  import os
5
 
6
+ def check_api_status(model_id, token):
7
  try:
8
+ client = InferenceClient(model_id, token=token)
9
+ # Test if we can connect to the API
10
+ response = client.chat_completion(
11
+ [{"role": "user", "content": "test"}],
12
+ max_tokens=1,
13
+ stream=False
14
+ )
15
+ return "API is accessible and responding"
16
  except Exception as e:
17
+ if "rate limit" in str(e).lower():
18
+ return "API is accessible (rate limited)"
19
+ return f"API status: {str(e)}"
 
 
 
 
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
+ status = check_api_status(model_id, token)
 
30
  return f"✅ Connected to {model_id} | {status}"
31
  except Exception as e:
32
  return f"❌ Error: {str(e)}"
 
106
  ],
107
  )
108
 
109
+ # Add API status at the footer with improved status check
110
  footer = gr.HTML(
111
  value=f"<div style='text-align: center; padding: 10px; background-color: #f0f0f0; border-top: 1px solid #ddd;'>{get_api_status()}</div>",
112
  every=30 # Updates every 30 seconds