Pavankumar9026 commited on
Commit
20cc711
·
1 Parent(s): 55702b8

Add retry logic and wait_for_model

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -1,14 +1,23 @@
1
  import gradio as gr
2
  import requests
3
  import os
 
4
 
5
  HF_TOKEN = os.environ.get("HF_TOKEN")
6
  API_URL = "https://api-inference.huggingface.co/models/Hate-speech-CNERG/dehatebert-mono-english"
7
 
8
  def query(text):
9
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
10
- response = requests.post(API_URL, headers=headers, json={"inputs": text})
11
- return response.json()
 
 
 
 
 
 
 
 
12
 
13
  def predict(comment):
14
  if not comment.strip():
@@ -17,9 +26,8 @@ def predict(comment):
17
  try:
18
  result = query(comment)
19
 
20
- # Show raw result for debugging
21
- if isinstance(result, dict):
22
- return f"API Error: {result}", ""
23
 
24
  if isinstance(result, list):
25
  result = result[0][0]
@@ -35,7 +43,7 @@ def predict(comment):
35
 
36
  return prediction, f"{probability}%"
37
 
38
- return f"Unexpected response: {result}", ""
39
 
40
  except Exception as e:
41
  return f"Exception: {str(e)}", ""
 
1
  import gradio as gr
2
  import requests
3
  import os
4
+ import time
5
 
6
  HF_TOKEN = os.environ.get("HF_TOKEN")
7
  API_URL = "https://api-inference.huggingface.co/models/Hate-speech-CNERG/dehatebert-mono-english"
8
 
9
  def query(text):
10
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
11
+ # Retry up to 3 times if model is loading
12
+ for i in range(3):
13
+ response = requests.post(API_URL, headers=headers, json={
14
+ "inputs": text,
15
+ "options": {"wait_for_model": True}
16
+ })
17
+ if response.status_code == 200:
18
+ return response.json()
19
+ time.sleep(3)
20
+ return {"error": f"Status: {response.status_code}, Response: {response.text}"}
21
 
22
  def predict(comment):
23
  if not comment.strip():
 
26
  try:
27
  result = query(comment)
28
 
29
+ if isinstance(result, dict) and "error" in result:
30
+ return f"API Error: {result['error']}", ""
 
31
 
32
  if isinstance(result, list):
33
  result = result[0][0]
 
43
 
44
  return prediction, f"{probability}%"
45
 
46
+ return f"Unexpected: {result}", ""
47
 
48
  except Exception as e:
49
  return f"Exception: {str(e)}", ""