AzefAziz commited on
Commit
dd6669f
·
verified ·
1 Parent(s): 01df685

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +87 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import seaborn as sns
5
+ from gradio_client import Client
6
+ import json
7
+
8
+ # Initialize the Gradio Client if not already done
9
+ client = Client("duchaba/Friendly_Text_Moderation")
10
+
11
+ def moderate_and_plot(text_input):
12
+ if not text_input:
13
+ raise gr.Error("Please enter some text for moderation.")
14
+
15
+ # Use the existing client to predict toxicity levels
16
+ prediction_result = client.predict(
17
+ msg=text_input,
18
+ safer=0.02, # This parameter might be specific to the original model's API
19
+ api_name="/fetch_toxicity_level"
20
+ )
21
+ # Parse the JSON string into a Python dictionary
22
+ dictionary_output = json.loads(prediction_result[-1])
23
+
24
+ # Filter out non-toxicity related keys from the dictionary_output for the plot
25
+ toxicity_data = {k: v for k, v in dictionary_output.items() if isinstance(v, (int, float)) and not k.startswith(('is_', 'max_', 'sum_', 'safer_')) and k != 'message'}
26
+
27
+ # Convert the dictionary to a pandas DataFrame for easier plotting
28
+ df_toxicity = pd.DataFrame(list(toxicity_data.items()), columns=['Toxicity Type', 'Score'])
29
+
30
+ # Sort the values for better visualization (optional)
31
+ df_toxicity = df_toxicity.sort_values(by='Score', ascending=False)
32
+
33
+ # Create a bar plot
34
+ plt.figure(figsize=(12, 7))
35
+ sns.barplot(x='Toxicity Type', y='Score', hue='Toxicity Type', data=df_toxicity, palette='viridis', legend=False)
36
+ plt.title('Toxicity Scores for the Sample Message')
37
+ plt.xlabel('Toxicity Type')
38
+ plt.ylabel('Score')
39
+ plt.xticks(rotation=45, ha='right') # Rotate labels to prevent overlap
40
+ plt.tight_layout()
41
+
42
+ # Add is_flagged and max_key as text labels on the plot
43
+ is_flagged_status = "Unsafe" if dictionary_output.get('is_flagged', False) else "Safe"
44
+ max_key_value = dictionary_output.get('max_key', 'N/A')
45
+
46
+ plt.text(0.02, 0.98, f"Flagged Status: {is_flagged_status}", transform=plt.gca().transAxes, fontsize=12, verticalalignment='top', bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5))
47
+ plt.text(0.02, 0.90, f"Max Toxicity Type: {max_key_value}", transform=plt.gca().transAxes, fontsize=12, verticalalignment='top', bbox=dict(boxstyle='round,pad=0.5', fc='lightblue', alpha=0.5))
48
+
49
+ # Prepare detailed toxicity scores for text output
50
+ detailed_scores_output = "Toxicity Details:\n"
51
+ keys_to_display = [
52
+ 'harassment', 'harassment_threatening', 'hate', 'hate_threatening',
53
+ 'illicit', 'illicit_violent', 'self_harm', 'self_harm_instructions',
54
+ 'self_harm_intent', 'sexual', 'sexual_minors', 'violence',
55
+ 'violence_graphic', 'harassment/threatening', 'hate/threatening',
56
+ 'illicit/violent', 'self-harm/intent', 'self-harm/instructions',
57
+ 'self-harm', 'sexual/minors', 'violence/graphic'
58
+ ]
59
+ for key in keys_to_display:
60
+ score = dictionary_output.get(key)
61
+ if score is not None:
62
+ detailed_scores_output += f"- {key.replace('_', ' ').replace('/', '/ ')}: {score:.6f}\n"
63
+
64
+ return plt.gcf(), detailed_scores_output
65
+
66
+ # Define custom CSS for centering text
67
+ custom_css = """
68
+ .centered-text textarea {
69
+ text-align: center;
70
+ }
71
+ """
72
+
73
+ # Create the Gradio interface
74
+ iface = gr.Interface(
75
+ fn=moderate_and_plot,
76
+ inputs=gr.Textbox(lines=5, label="Enter text for moderation"),
77
+ outputs=[
78
+ gr.Plot(label="Toxicity Levels Bar Graph"),
79
+ gr.Textbox(label="Detailed Toxicity Scores", lines=10, elem_classes=["centered-text"])
80
+ ],
81
+ title="Friendly Text Moderation Interface",
82
+ description="Enter text to get a visual breakdown of various toxicity levels using the `duchaba/Friendly_Text_Moderation` model.",
83
+ css=custom_css # Moved css argument here
84
+ )
85
+
86
+ # Launch the interface
87
+ iface.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio==4.29.0
2
+ pandas==2.2.2
3
+ matplotlib==3.8.4
4
+ seaborn==0.13.2
5
+ gradio-client==0.16.0