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

Add debug error handling

Browse files
Files changed (1) hide show
  1. app.py +23 -22
app.py CHANGED
@@ -2,9 +2,7 @@ import gradio as gr
2
  import requests
3
  import os
4
 
5
- # Load API token from environment variable
6
  HF_TOKEN = os.environ.get("HF_TOKEN")
7
-
8
  API_URL = "https://api-inference.huggingface.co/models/Hate-speech-CNERG/dehatebert-mono-english"
9
 
10
  def query(text):
@@ -16,32 +14,35 @@ def predict(comment):
16
  if not comment.strip():
17
  return "⚠️ Please enter some text.", ""
18
 
19
- result = query(comment)
20
-
21
- if isinstance(result, list):
22
- result = result[0][0]
23
- label = result["label"]
24
- score = result["score"]
25
 
26
- if label == "HATE":
27
- prediction = "🚨 Hate Speech Detected"
28
- probability = round(score * 100, 2)
29
- else:
30
- prediction = "✅ No Hate Speech"
31
- probability = round((1 - score) * 100, 2)
 
 
 
 
 
 
 
32
 
33
- return prediction, f"{probability}%"
34
 
35
- return "❌ Error - Model loading, try again.", ""
 
36
 
37
- # Gradio UI
38
  demo = gr.Interface(
39
  fn=predict,
40
- inputs=gr.Textbox(
41
- lines=4,
42
- placeholder="Enter a comment to analyze...",
43
- label="Comment"
44
- ),
45
  outputs=[
46
  gr.Textbox(label="Prediction"),
47
  gr.Textbox(label="Confidence")
 
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):
 
14
  if not comment.strip():
15
  return "⚠️ Please enter some text.", ""
16
 
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]
26
+ label = result["label"]
27
+ score = result["score"]
28
+
29
+ if label == "HATE":
30
+ prediction = "🚨 Hate Speech Detected"
31
+ probability = round(score * 100, 2)
32
+ else:
33
+ prediction = "✅ No Hate Speech"
34
+ probability = round((1 - score) * 100, 2)
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)}", ""
42
 
 
43
  demo = gr.Interface(
44
  fn=predict,
45
+ inputs=gr.Textbox(lines=4, placeholder="Enter a comment...", label="Comment"),
 
 
 
 
46
  outputs=[
47
  gr.Textbox(label="Prediction"),
48
  gr.Textbox(label="Confidence")