Update src/gradio_server.py
Browse files- src/gradio_server.py +6 -2
src/gradio_server.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
from fastapi import FastAPI
|
|
@@ -96,7 +97,10 @@ def gradio_detect_radicalization(user_text: str):
|
|
| 96 |
|
| 97 |
def gradio_generate_policy_enforcement(user_text: str, violation_context: str):
|
| 98 |
# violation_context needs to be provided in a valid JSON format
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
| 100 |
request = PolicyEnforcementRequest(user_text=user_text, violation_context=context_dict)
|
| 101 |
response = generate_policy_enforcement(request)
|
| 102 |
return response.values
|
|
@@ -113,7 +117,7 @@ iface = gr.Interface(
|
|
| 113 |
# Second interface for policy enforcement
|
| 114 |
iface2 = gr.Interface(
|
| 115 |
fn=gradio_generate_policy_enforcement, # Function to generate policy enforcement
|
| 116 |
-
inputs=["text",
|
| 117 |
outputs="json", # Return JSON output
|
| 118 |
title="Policy Enforcement Decision",
|
| 119 |
description="Enter user text and context to generate a policy enforcement decision."
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
from fastapi import FastAPI
|
|
|
|
| 97 |
|
| 98 |
def gradio_generate_policy_enforcement(user_text: str, violation_context: str):
|
| 99 |
# violation_context needs to be provided in a valid JSON format
|
| 100 |
+
try:
|
| 101 |
+
context_dict = json.loads(violation_context) # Parse violation_context as JSON
|
| 102 |
+
except json.JSONDecodeError:
|
| 103 |
+
return {"error": "Invalid JSON format for violation_context"}
|
| 104 |
request = PolicyEnforcementRequest(user_text=user_text, violation_context=context_dict)
|
| 105 |
response = generate_policy_enforcement(request)
|
| 106 |
return response.values
|
|
|
|
| 117 |
# Second interface for policy enforcement
|
| 118 |
iface2 = gr.Interface(
|
| 119 |
fn=gradio_generate_policy_enforcement, # Function to generate policy enforcement
|
| 120 |
+
inputs=["text", gr.JSON()], # Two text inputs, one for user text, one for violation context
|
| 121 |
outputs="json", # Return JSON output
|
| 122 |
title="Policy Enforcement Decision",
|
| 123 |
description="Enter user text and context to generate a policy enforcement decision."
|