nivashuggingface commited on
Commit
8fd1381
·
verified ·
1 Parent(s): 1e13411

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -18
app.py CHANGED
@@ -43,29 +43,35 @@ def moderate_text(text, safer_value):
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
 
49
- # Initialize the client with the dynamic endpoint
50
- client = Client(dynamic_endpoint, hf_token=HF_TOKEN)
51
- result = client.predict(
52
- text,
53
- safer_value,
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)
64
 
65
- # Convert bytes to PIL Image
66
- img = Image.open(io.BytesIO(img_data))
67
 
68
- return result, img
 
 
 
 
 
69
 
70
  # Define the Gradio interface
71
  demo = gr.Interface(
 
43
  Returns:
44
  tuple: A tuple containing the moderation result and the generated image.
45
  """
46
+ try:
47
+ # Fetch the dynamic endpoint
48
+ dynamic_endpoint = get_dynamic_endpoint()
49
 
50
+ # Initialize the client with the dynamic endpoint
51
+ client = Client(dynamic_endpoint, hf_token=HF_TOKEN)
52
+ result = client.predict(
53
+ text,
54
+ safer_value,
55
+ api_name="/censor_me"
56
+ )
57
 
58
+ # Ensure the result contains the expected data
59
+ base64_data = result.get('plot', '').split(',')[1] if 'plot' in result else None
60
+ if not base64_data:
61
+ raise ValueError("Expected plot data not found in the result.")
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
+
71
+ except Exception as e:
72
+ # Log the error for debugging purposes
73
+ print(f"Error occurred: {e}")
74
+ return str(e), None
75
 
76
  # Define the Gradio interface
77
  demo = gr.Interface(