Spaces:
Runtime error
Runtime error
Srini Vangala
commited on
Commit
•
ad77051
1
Parent(s):
809b1fe
Text moderation
Browse files- app.py +75 -3
- requirements.txt +3 -0
app.py
CHANGED
@@ -1,7 +1,79 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from gradio_client import Client
|
3 |
+
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 |
|
10 |
+
def moderate_text(text, safer_value):
|
11 |
+
result = client.predict(
|
12 |
+
text,
|
13 |
+
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(
|
40 |
+
fn=moderate_text,
|
41 |
+
inputs=[
|
42 |
+
gr.Textbox(label="Enter Text:", placeholder="Type your text here...", lines=5),
|
43 |
+
gr.Slider(minimum=0.005, maximum=0.1, value=0.005, label="Personalize Safer Value: (larger value is less safe)")
|
44 |
+
],
|
45 |
+
outputs=[gr.Textbox(label="Moderated Text:", lines=5), gr.Image(type="pil", label="Moderation Pie Chart")],
|
46 |
+
title="Friendly Text Moderator",
|
47 |
+
description="Enter text to be moderated and adjust the safer value to see how it affects the moderation.",
|
48 |
+
theme="compact"
|
49 |
+
)
|
50 |
+
|
51 |
+
# Customize the CSS
|
52 |
+
custom_css = """
|
53 |
+
body {
|
54 |
+
background-color: #f5f5f5;
|
55 |
+
font-family: Arial, sans-serif;
|
56 |
+
}
|
57 |
+
.gradio-container {
|
58 |
+
max-width: 800px;
|
59 |
+
margin: auto;
|
60 |
+
padding: 20px;
|
61 |
+
background-color: white;
|
62 |
+
border: 1px solid #ddd;
|
63 |
+
border-radius: 8px;
|
64 |
+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
65 |
+
}
|
66 |
+
.gr-button {
|
67 |
+
background-color: #4CAF50;
|
68 |
+
color: white;
|
69 |
+
}
|
70 |
+
.gr-button:hover {
|
71 |
+
background-color: #45a049;
|
72 |
+
}
|
73 |
+
"""
|
74 |
+
|
75 |
+
# Add the custom CSS to the Gradio app
|
76 |
+
demo.css = custom_css
|
77 |
+
|
78 |
+
# Launch the app
|
79 |
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
gradio_client
|
3 |
+
requests
|