Srini Vangala commited on
Commit
f76e239
1 Parent(s): ad77051

Render plot as piechart

Browse files
Files changed (1) hide show
  1. app.py +9 -18
app.py CHANGED
@@ -4,6 +4,7 @@ import matplotlib.pyplot as plt
4
  import numpy as np
5
  import io
6
  from PIL import Image
 
7
 
8
  client = Client("https://duchaba-friendly-text-moderation.hf.space/--replicas/gffry/")
9
 
@@ -14,26 +15,16 @@ def moderate_text(text, safer_value):
14
  api_name="/censor_me"
15
  )
16
 
17
- # Assuming 'result' contains moderation categories and their respective counts
18
- categories = result.get("categories", {"Safe": 1, "Unsafe": 1}) # Example structure
19
- labels = list(categories.keys())
20
- sizes = list(categories.values())
21
-
22
- # Generate a pie chart
23
- fig, ax = plt.subplots()
24
- ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
25
- ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
26
- ax.set_title('Moderation Result Distribution')
27
 
28
- # Save plot to a bytes buffer
29
- buf = io.BytesIO()
30
- plt.savefig(buf, format='png')
31
- buf.seek(0)
32
-
33
- # Convert bytes buffer to PIL Image
34
- plot_image = Image.open(buf)
35
 
36
- return result, plot_image
 
 
 
37
 
38
  # Define the Gradio interface
39
  demo = gr.Interface(
 
4
  import numpy as np
5
  import io
6
  from PIL import Image
7
+ import base64
8
 
9
  client = Client("https://duchaba-friendly-text-moderation.hf.space/--replicas/gffry/")
10
 
 
15
  api_name="/censor_me"
16
  )
17
 
18
+ # Example structure of the result
19
+ base64_data = result.get('plot', '').split(',')[1]
 
 
 
 
 
 
 
 
20
 
21
+ # Decode base64 to bytes
22
+ img_data = base64.b64decode(base64_data)
 
 
 
 
 
23
 
24
+ # Convert bytes to PIL Image
25
+ img = Image.open(io.BytesIO(img_data))
26
+
27
+ return result, img
28
 
29
  # Define the Gradio interface
30
  demo = gr.Interface(