epinapala commited on
Commit
e0e8a14
1 Parent(s): 1277029

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -25
app.py CHANGED
@@ -8,7 +8,7 @@ from PIL import Image
8
  import base64
9
 
10
  # Load Hugging Face token from environment variable
11
- HF_TOKEN = os.getenv("HF_TOKEN", "your_default_hf_token")
12
 
13
  def get_dynamic_endpoint():
14
  """
@@ -17,16 +17,20 @@ def get_dynamic_endpoint():
17
  Returns:
18
  str: The current dynamic endpoint.
19
  """
20
- api_url = "https://api.huggingface.co/space/duchaba/friendly-text-moderation"
21
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
 
22
 
23
- response = requests.get(api_url, headers=headers)
24
- response.raise_for_status() # Raise an error for bad status codes
25
 
26
- # Extract the endpoint from the response
27
- data = response.json()
28
- endpoint = data.get("url")
29
- return endpoint
 
 
 
30
 
31
  # Fetch the dynamic endpoint
32
  dynamic_endpoint = get_dynamic_endpoint()
@@ -46,22 +50,26 @@ def moderate_text(text, safer_value):
46
  result (dict): The moderation result.
47
  img (PIL.Image): The moderation pie chart.
48
  """
49
- result = client.predict(
50
- text,
51
- safer_value,
52
- api_name="/censor_me"
53
- )
54
-
55
- # Example structure of the result
56
- base64_data = result.get('plot', '').split(',')[1]
57
-
58
- # Decode base64 to bytes
59
- img_data = base64.b64decode(base64_data)
60
-
61
- # Convert bytes to PIL Image
62
- img = Image.open(io.BytesIO(img_data))
63
-
64
- return result, img
 
 
 
 
65
 
66
  # Define the Gradio interface
67
  demo = gr.Interface(
 
8
  import base64
9
 
10
  # Load Hugging Face token from environment variable
11
+ HF_TOKEN = os.getenv("HF_TOKEN", os.environ['API_TOKEN'])
12
 
13
  def get_dynamic_endpoint():
14
  """
 
17
  Returns:
18
  str: The current dynamic endpoint.
19
  """
20
+ try:
21
+ api_url = "https://huggingface.co/api/spaces/duchaba/friendly-text-moderation"
22
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
23
 
24
+ response = requests.get(api_url, headers=headers)
25
+ response.raise_for_status() # Raise an error for bad status codes
26
 
27
+ # Extract the endpoint from the response
28
+ data = response.json()
29
+ endpoint = data.get("url")
30
+ return endpoint
31
+ except Exception as e:
32
+ print(f"Error fetching dynamic endpoint: {e}")
33
+ return "https://default-endpoint.hf.space/--replicas/default/"
34
 
35
  # Fetch the dynamic endpoint
36
  dynamic_endpoint = get_dynamic_endpoint()
 
50
  result (dict): The moderation result.
51
  img (PIL.Image): The moderation pie chart.
52
  """
53
+ try:
54
+ result = client.predict(
55
+ text,
56
+ safer_value,
57
+ api_name="/censor_me"
58
+ )
59
+
60
+ # Example structure of the result
61
+ base64_data = result.get('plot', '').split(',')[1]
62
+
63
+ # Decode base64 to bytes
64
+ img_data = base64.b64decode(base64_data)
65
+
66
+ # Convert bytes to PIL Image
67
+ img = Image.open(io.BytesIO(img_data))
68
+
69
+ return result, img
70
+ except Exception as e:
71
+ print(f"Error during moderation: {e}")
72
+ return {"error": "Failed to moderate text"}, None
73
 
74
  # Define the Gradio interface
75
  demo = gr.Interface(