Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import os | |
# Configure the endpoint and authentication | |
ENDPOINT_URL = os.environ.get("ENDPOINT_URL", "https://yoffo4n9o2y9reiw.us-east-1.aws.endpoints.huggingface.cloud") | |
# HF_API_TOKEN = os.environ.get("HF_API_TOKEN") # Get API token from environment variable | |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "").strip() # Use strip() to remove extra whitespaces and newlines | |
# Check if the API token is configured | |
def is_token_configured(): | |
if not HF_API_TOKEN: | |
return "β οΈ Warning: HF_API_TOKEN is not configured. The app won't work until you add this secret in your Space settings." | |
return "β API token is configured" | |
# Define the function to call your endpoint | |
def check_safety(input_text): | |
if not input_text.strip(): | |
return "Please enter some text to check" | |
# Prepare the payload for your endpoint | |
payload = { | |
"inputs": input_text | |
} | |
# Set headers with authentication token | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {HF_API_TOKEN}" | |
} | |
try: | |
# Make the request to your endpoint | |
response = requests.post(ENDPOINT_URL, json=payload, headers=headers, timeout=30) | |
# Check if the request was successful | |
if response.status_code == 200: | |
result = response.json() | |
# Format the result based on your endpoint's response format | |
is_safe = result.get("is_safe", False) | |
safety_result = result.get("safety_result", "No result received") | |
if is_safe: | |
return f"β {safety_result}" | |
else: | |
return f"β {safety_result}" | |
else: | |
return f"Error: Request failed with status code {response.status_code}. Details: {response.text}" | |
except requests.exceptions.Timeout: | |
return "Error: Request timed out. The endpoint may be overloaded or unavailable." | |
except requests.exceptions.ConnectionError: | |
return "Error: Failed to connect to the endpoint. Please check the endpoint URL." | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Define the Gradio interface | |
with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !important}") as demo: | |
gr.Markdown(f"# Safety Content Classifier") | |
gr.Markdown(f"## Connected to external safety model endpoint") | |
# Add token status indicator | |
token_status = gr.Markdown(is_token_configured()) | |
with gr.Accordion("About this demo", open=False): | |
gr.Markdown(""" | |
This demo uses an external API endpoint to classify text based on safety policies. | |
It checks content against the following categories: | |
- Harassment | |
- Dangerous Content | |
- Hate Speech | |
- Sexually Explicit Information | |
The model will respond with 'Safe' or 'Unsafe' followed by any violated categories. | |
""") | |
with gr.Row(): | |
with gr.Column(): | |
input_text = gr.Textbox( | |
label="Enter text to check", | |
placeholder="Type here...", | |
lines=5 | |
) | |
check_button = gr.Button("Check Safety", variant="primary") | |
with gr.Column(): | |
output = gr.Textbox( | |
label="Safety Result", | |
lines=5 | |
) | |
# Set up event handlers | |
check_button.click(fn=check_safety, inputs=input_text, outputs=output) | |
input_text.submit(fn=check_safety, inputs=input_text, outputs=output) | |
# Example inputs | |
gr.Examples( | |
[ | |
["Hello, how are you today?"], | |
["I love your work, it's amazing!"], | |
["I want to learn how to make a bomb."], | |
["I hate people from that country."], | |
["Let's meet for coffee and discuss the project."], | |
], | |
input_text | |
) | |
# Launch the app | |
demo.launch() | |