Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,59 +15,6 @@ def is_token_configured():
|
|
| 15 |
return "β
API token is configured"
|
| 16 |
|
| 17 |
|
| 18 |
-
def check_safety(input_text, uploaded_image):
|
| 19 |
-
if not input_text.strip() and uploaded_image is None:
|
| 20 |
-
return "β οΈ Please enter text or upload an image to check."
|
| 21 |
-
|
| 22 |
-
payload = {}
|
| 23 |
-
|
| 24 |
-
if input_text.strip():
|
| 25 |
-
payload["inputs"] = input_text
|
| 26 |
-
# if uploaded_image is not None:
|
| 27 |
-
# # In Gradio, uploaded_image will be a local temp file path
|
| 28 |
-
# # Your endpoint expects a URL or base64. Here, we send as base64.
|
| 29 |
-
# import base64
|
| 30 |
-
|
| 31 |
-
# with open(uploaded_image, "rb") as img_file:
|
| 32 |
-
# img_bytes = img_file.read()
|
| 33 |
-
# img_base64 = base64.b64encode(img_bytes).decode('utf-8')
|
| 34 |
-
|
| 35 |
-
# payload["image"] = img_base64 # Assume your backend can accept image this way
|
| 36 |
-
|
| 37 |
-
headers = {
|
| 38 |
-
"Content-Type": "application/json",
|
| 39 |
-
"Authorization": f"Bearer {HF_API_TOKEN}"
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
try:
|
| 43 |
-
response = requests.post(ENDPOINT_URL, json=payload, headers=headers, timeout=30)
|
| 44 |
-
|
| 45 |
-
if response.status_code == 200:
|
| 46 |
-
result = response.json()
|
| 47 |
-
|
| 48 |
-
is_safe = result.get("is_safe", False)
|
| 49 |
-
safety_result = result.get("safety_result", {})
|
| 50 |
-
|
| 51 |
-
safety = safety_result.get("Safety", "Unknown")
|
| 52 |
-
score = safety_result.get("Score", "")
|
| 53 |
-
categories = safety_result.get("Unsafe Categories", "")
|
| 54 |
-
|
| 55 |
-
if is_safe:
|
| 56 |
-
return f"β
Safe\n\nSafety: {safety}\nScore: {score}\nUnsafe Categories: {categories}"
|
| 57 |
-
else:
|
| 58 |
-
return f"β Unsafe\n\nSafety: {safety}\nScore: {score}\nUnsafe Categories: {categories}"
|
| 59 |
-
|
| 60 |
-
else:
|
| 61 |
-
return f"β Error: Request failed with status code {response.status_code}.\nDetails: {response.text}"
|
| 62 |
-
|
| 63 |
-
except requests.exceptions.Timeout:
|
| 64 |
-
return "β Error: Request timed out."
|
| 65 |
-
|
| 66 |
-
except requests.exceptions.ConnectionError:
|
| 67 |
-
return "β Error: Failed to connect to the endpoint."
|
| 68 |
-
|
| 69 |
-
except Exception as e:
|
| 70 |
-
return f"β Error: {str(e)}"
|
| 71 |
with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !important}") as demo:
|
| 72 |
gr.Markdown(f"# Safety Content Classifier")
|
| 73 |
gr.Markdown(f"## Connected to external safety model endpoint")
|
|
@@ -93,10 +40,7 @@ with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !im
|
|
| 93 |
placeholder="Type here...",
|
| 94 |
lines=5
|
| 95 |
)
|
| 96 |
-
|
| 97 |
-
label="Upload Image to check",
|
| 98 |
-
type="filepath" # important: get local path
|
| 99 |
-
)
|
| 100 |
check_button = gr.Button("Check Safety", variant="primary")
|
| 101 |
|
| 102 |
with gr.Column():
|
|
|
|
| 15 |
return "β
API token is configured"
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
with gr.Blocks(title="Safety Content Classifier", css="footer {display: none !important}") as demo:
|
| 19 |
gr.Markdown(f"# Safety Content Classifier")
|
| 20 |
gr.Markdown(f"## Connected to external safety model endpoint")
|
|
|
|
| 40 |
placeholder="Type here...",
|
| 41 |
lines=5
|
| 42 |
)
|
| 43 |
+
|
|
|
|
|
|
|
|
|
|
| 44 |
check_button = gr.Button("Check Safety", variant="primary")
|
| 45 |
|
| 46 |
with gr.Column():
|