Machlovi commited on
Commit
56af1aa
·
verified ·
1 Parent(s): f4bc135

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
app.py CHANGED
@@ -2,11 +2,11 @@ import gradio as gr
2
  import requests
3
  import os
4
  import json
 
5
 
6
  # Configure the endpoint and authentication
7
  ENDPOINT_URL = os.environ.get("ENDPOINT_URL", "https://dz0eq6vxq3nm0uh7.us-east-1.aws.endpoints.huggingface.cloud")
8
- # HF_API_TOKEN = os.environ.get("HF_API_TOKEN") # Get API token from environment variable
9
- HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "").strip() # Use strip() to remove extra whitespaces and newlines
10
 
11
 
12
  def is_token_configured():
@@ -14,14 +14,10 @@ def is_token_configured():
14
  return "⚠️ Warning: HF_API_TOKEN is not configured. The app won't work until you add this secret in your Space settings."
15
  return "✅ API token is configured"
16
 
17
-
18
- # Assuming your class with extract_and_parse_json exists
19
- # Or you can define it separately if needed
20
-
21
  class SafetyChecker:
22
  def __init__(self):
23
- self.ENDPOINT_URL = os.environ.get("ENDPOINT_URL", "https://your-endpoint")
24
- self.HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "").strip()
25
 
26
  def extract_and_parse_json(self, response: str):
27
  match = re.search(r'```(?:json)?\s*(.*?)\s*```', response, re.DOTALL)
@@ -64,14 +60,13 @@ class SafetyChecker:
64
  response = requests.post(self.ENDPOINT_URL, json=payload, headers=headers, timeout=30)
65
 
66
  if response.status_code == 200:
67
- result_raw = response.json() # still a string inside triple backticks
68
 
69
  if isinstance(result_raw, str):
70
  parsed_result = self.extract_and_parse_json(result_raw)
71
  else:
72
  parsed_result = result_raw
73
 
74
- # Now parsed_result is a dictionary
75
  safety = parsed_result.get("Safety", "Unknown")
76
  score = parsed_result.get("Score", "")
77
  categories = parsed_result.get("Unsafe Categories", "")
@@ -92,6 +87,10 @@ class SafetyChecker:
92
  except Exception as e:
93
  return f"❗ Error: {str(e)}"
94
 
 
 
 
 
95
  with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !important}") as demo:
96
  gr.Markdown("# Safety Content Classifier")
97
  gr.Markdown("## Connected to external safety model endpoint")
@@ -126,9 +125,9 @@ with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !im
126
  lines=8
127
  )
128
 
129
- # Update event handlers
130
- check_button.click(fn=check_safety, inputs=[input_text], outputs=output)
131
- input_text.submit(fn=check_safety, inputs=[input_text], outputs=output)
132
 
133
  gr.Examples(
134
  [
@@ -141,4 +140,3 @@ with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !im
141
  )
142
 
143
  demo.launch()
144
-
 
2
  import requests
3
  import os
4
  import json
5
+ import re
6
 
7
  # Configure the endpoint and authentication
8
  ENDPOINT_URL = os.environ.get("ENDPOINT_URL", "https://dz0eq6vxq3nm0uh7.us-east-1.aws.endpoints.huggingface.cloud")
9
+ HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "").strip()
 
10
 
11
 
12
  def is_token_configured():
 
14
  return "⚠️ Warning: HF_API_TOKEN is not configured. The app won't work until you add this secret in your Space settings."
15
  return "✅ API token is configured"
16
 
 
 
 
 
17
  class SafetyChecker:
18
  def __init__(self):
19
+ self.ENDPOINT_URL = ENDPOINT_URL
20
+ self.HF_API_TOKEN = HF_API_TOKEN
21
 
22
  def extract_and_parse_json(self, response: str):
23
  match = re.search(r'```(?:json)?\s*(.*?)\s*```', response, re.DOTALL)
 
60
  response = requests.post(self.ENDPOINT_URL, json=payload, headers=headers, timeout=30)
61
 
62
  if response.status_code == 200:
63
+ result_raw = response.json()
64
 
65
  if isinstance(result_raw, str):
66
  parsed_result = self.extract_and_parse_json(result_raw)
67
  else:
68
  parsed_result = result_raw
69
 
 
70
  safety = parsed_result.get("Safety", "Unknown")
71
  score = parsed_result.get("Score", "")
72
  categories = parsed_result.get("Unsafe Categories", "")
 
87
  except Exception as e:
88
  return f"❗ Error: {str(e)}"
89
 
90
+ # Create an instance of SafetyChecker
91
+ checker = SafetyChecker()
92
+
93
+ # Build Gradio App
94
  with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !important}") as demo:
95
  gr.Markdown("# Safety Content Classifier")
96
  gr.Markdown("## Connected to external safety model endpoint")
 
125
  lines=8
126
  )
127
 
128
+ # Notice: Now we use checker.check_safety instead of check_safety directly
129
+ check_button.click(fn=checker.check_safety, inputs=[input_text], outputs=output)
130
+ input_text.submit(fn=checker.check_safety, inputs=[input_text], outputs=output)
131
 
132
  gr.Examples(
133
  [
 
140
  )
141
 
142
  demo.launch()