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

Debug raw response

Browse files
Files changed (1) hide show
  1. app.py +12 -14
app.py CHANGED
@@ -8,26 +8,24 @@ API_URL = "https://api-inference.huggingface.co/models/Hate-speech-CNERG/dehateb
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():
24
  return "⚠️ Please enter some text.", ""
25
 
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,7 +41,7 @@ def predict(comment):
43
 
44
  return prediction, f"{probability}%"
45
 
46
- return f"Unexpected: {result}", ""
47
 
48
  except Exception as e:
49
  return f"Exception: {str(e)}", ""
 
8
 
9
  def query(text):
10
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
11
+ response = requests.post(API_URL, headers=headers, json={
12
+ "inputs": text,
13
+ "options": {"wait_for_model": True}
14
+ })
15
+ return response.status_code, response.text
 
 
 
 
 
16
 
17
  def predict(comment):
18
  if not comment.strip():
19
  return "⚠️ Please enter some text.", ""
20
 
21
  try:
22
+ status, text = query(comment)
23
 
24
+ if status != 200:
25
+ return f"Status {status}: {text}", ""
26
+
27
+ import json
28
+ result = json.loads(text)
29
 
30
  if isinstance(result, list):
31
  result = result[0][0]
 
41
 
42
  return prediction, f"{probability}%"
43
 
44
+ return f"Response: {text}", ""
45
 
46
  except Exception as e:
47
  return f"Exception: {str(e)}", ""