Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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",
|
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 |
-
|
21 |
-
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
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 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
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(
|