nivashuggingface commited on
Commit
1e13411
·
verified ·
1 Parent(s): 7e27ea5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -7,16 +7,15 @@ from PIL import Image
7
  import base64
8
  import os
9
  import requests
10
- #client = Client("https://duchaba-friendly-text-moderation.hf.space/--replicas/gffry/")
11
- #client = Client("https://duchaba-friendly-text-moderation.hf.space/--replicas/6rx2j/")
12
 
13
  # Load Hugging Face token from environment variable
14
- HF_TOKEN = os.getenv("HF_TOKEN") #, "your_default_hf_token")
 
 
15
 
16
  def get_dynamic_endpoint():
17
  """
18
  Fetch the dynamic endpoint using the Hugging Face API.
19
-
20
  Returns:
21
  str: The current dynamic endpoint.
22
  """
@@ -29,11 +28,21 @@ def get_dynamic_endpoint():
29
  # Extract the endpoint from the response
30
  data = response.json()
31
  endpoint = data.get("url")
 
 
32
  return endpoint
33
 
34
-
35
-
36
  def moderate_text(text, safer_value):
 
 
 
 
 
 
 
 
 
 
37
  # Fetch the dynamic endpoint
38
  dynamic_endpoint = get_dynamic_endpoint()
39
 
@@ -45,8 +54,10 @@ def moderate_text(text, safer_value):
45
  api_name="/censor_me"
46
  )
47
 
48
- # Example structure of the result
49
- base64_data = result.get('plot', '').split(',')[1]
 
 
50
 
51
  # Decode base64 to bytes
52
  img_data = base64.b64decode(base64_data)
@@ -97,4 +108,4 @@ body {
97
  demo.css = custom_css
98
 
99
  # Launch the app
100
- demo.launch()
 
7
  import base64
8
  import os
9
  import requests
 
 
10
 
11
  # Load Hugging Face token from environment variable
12
+ HF_TOKEN = os.getenv("HF_TOKEN")
13
+ if not HF_TOKEN:
14
+ raise ValueError("Hugging Face token not found in environment variables.")
15
 
16
  def get_dynamic_endpoint():
17
  """
18
  Fetch the dynamic endpoint using the Hugging Face API.
 
19
  Returns:
20
  str: The current dynamic endpoint.
21
  """
 
28
  # Extract the endpoint from the response
29
  data = response.json()
30
  endpoint = data.get("url")
31
+ if not endpoint:
32
+ raise ValueError("Endpoint URL not found in the response.")
33
  return endpoint
34
 
 
 
35
  def moderate_text(text, safer_value):
36
+ """
37
+ Moderate the given text using the Hugging Face API.
38
+
39
+ Args:
40
+ text (str): The text to be moderated.
41
+ safer_value (float): The safer value to be used for moderation.
42
+
43
+ Returns:
44
+ tuple: A tuple containing the moderation result and the generated image.
45
+ """
46
  # Fetch the dynamic endpoint
47
  dynamic_endpoint = get_dynamic_endpoint()
48
 
 
54
  api_name="/censor_me"
55
  )
56
 
57
+ # Ensure the result contains the expected data
58
+ base64_data = result.get('plot', '').split(',')[1] if 'plot' in result else None
59
+ if not base64_data:
60
+ raise ValueError("Expected plot data not found in the result.")
61
 
62
  # Decode base64 to bytes
63
  img_data = base64.b64decode(base64_data)
 
108
  demo.css = custom_css
109
 
110
  # Launch the app
111
+ demo.launch()