Spaces:
Runtime error
Runtime error
File size: 2,212 Bytes
809b1fe ad77051 809b1fe ad77051 809b1fe ad77051 809b1fe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import gradio as gr
from gradio_client import Client
import matplotlib.pyplot as plt
import numpy as np
import io
from PIL import Image
client = Client("https://duchaba-friendly-text-moderation.hf.space/--replicas/gffry/")
def moderate_text(text, safer_value):
result = client.predict(
text,
safer_value,
api_name="/censor_me"
)
# Assuming 'result' contains moderation categories and their respective counts
categories = result.get("categories", {"Safe": 1, "Unsafe": 1}) # Example structure
labels = list(categories.keys())
sizes = list(categories.values())
# Generate a pie chart
fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
ax.set_title('Moderation Result Distribution')
# Save plot to a bytes buffer
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
# Convert bytes buffer to PIL Image
plot_image = Image.open(buf)
return result, plot_image
# Define the Gradio interface
demo = gr.Interface(
fn=moderate_text,
inputs=[
gr.Textbox(label="Enter Text:", placeholder="Type your text here...", lines=5),
gr.Slider(minimum=0.005, maximum=0.1, value=0.005, label="Personalize Safer Value: (larger value is less safe)")
],
outputs=[gr.Textbox(label="Moderated Text:", lines=5), gr.Image(type="pil", label="Moderation Pie Chart")],
title="Friendly Text Moderator",
description="Enter text to be moderated and adjust the safer value to see how it affects the moderation.",
theme="compact"
)
# Customize the CSS
custom_css = """
body {
background-color: #f5f5f5;
font-family: Arial, sans-serif;
}
.gradio-container {
max-width: 800px;
margin: auto;
padding: 20px;
background-color: white;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.gr-button {
background-color: #4CAF50;
color: white;
}
.gr-button:hover {
background-color: #45a049;
}
"""
# Add the custom CSS to the Gradio app
demo.css = custom_css
# Launch the app
demo.launch()
|