Victoria Reis commited on
Commit
d4a0f6d
1 Parent(s): 0b0b452

feature:chart visualization

Browse files
Files changed (2) hide show
  1. app.py +26 -25
  2. flagged/log.csv +2 -0
app.py CHANGED
@@ -4,9 +4,10 @@ import torch
4
  from collections import Counter
5
  from scipy.special import softmax
6
  import plotly.express as px
 
7
 
8
  # Article string
9
- article_string = "Author: <a href=\"https://huggingface.co/FpOliveira\">Felipe Ramos de Oliveira</a>. Read more about our <a href=\"https://github.com/Silly-Machine/TuPi-Portuguese-Hate-Speech-Dataset\">The Portuguese hate speech dataset (TuPI) </a>."
10
 
11
  # App title
12
  app_title = "Portuguese hate speech identifier (Multiclass) - Identificador de discurso de ódio em português (Multiclasse)"
@@ -97,8 +98,6 @@ def most_frequent(array):
97
  occurence_count = Counter(array)
98
  return occurence_count.most_common(1)[0][0]
99
 
100
-
101
- # Prediction function
102
  def predict(s1, chosen_model):
103
  # Clear previous figure instance
104
  fig = None
@@ -133,21 +132,23 @@ def predict(s1, chosen_model):
133
  # Get the categories and probabilities for all classes
134
  all_categories = [hate_speech_categories[index] for index in valid_indices]
135
  all_probabilities = [logits[index] for index in valid_indices]
136
-
137
- # Create a bar plot using Plotly
138
  fig = px.bar(x=all_categories, y=all_probabilities, labels={'x': 'Categories', 'y': 'Probabilities'},
139
- title="Classes Predicted Probabilities", text=all_probabilities)
140
- fig.update_traces(texttemplate='%{text:.4f}', textposition='outside')
141
- fig.show()
142
-
143
- # # Save the figure as HTML
144
- # html_figure = fig.to_html()
145
-
146
- # Get the categories and probabilities for the top two
147
- top_category = [hate_speech_categories[index] for index in top_indices]
148
- top_probability = [logits[index] for index in top_indices]
149
- result = top_category[0], 1-top_probability[1]
150
- return result
 
 
 
151
 
152
  # Input components
153
  inputs = [
@@ -155,14 +156,14 @@ inputs = [
155
  gr.Dropdown(label="Model", choices=user_friendly_name_list, value=user_friendly_name_list[0])
156
  ]
157
 
158
- # Output components
159
  outputs = [
160
- gr.Label(label="Predominant category"),
161
- gr.Label(label="Probability"),
162
  ]
163
 
164
- # Gradio interface
165
- gr.Interface(fn=predict, inputs=inputs, outputs=outputs, title=app_title,
166
- description=app_description,
167
- examples=app_examples,
168
- article=article_string).launch()
 
 
 
4
  from collections import Counter
5
  from scipy.special import softmax
6
  import plotly.express as px
7
+ import plotly.io as pio # Add this import
8
 
9
  # Article string
10
+ article_string = "Authors: <a href=\"https://huggingface.co/FpOliveira\">Felipe Oliveira</a> & <a href=\"https://huggingface.co/victoriadreis\">Victoria Reis</a>. Read more about our <a href=\"https://github.com/Silly-Machine/TuPi-Portuguese-Hate-Speech-Dataset\">The Portuguese hate speech dataset (TuPI) </a>."
11
 
12
  # App title
13
  app_title = "Portuguese hate speech identifier (Multiclass) - Identificador de discurso de ódio em português (Multiclasse)"
 
98
  occurence_count = Counter(array)
99
  return occurence_count.most_common(1)[0][0]
100
 
 
 
101
  def predict(s1, chosen_model):
102
  # Clear previous figure instance
103
  fig = None
 
132
  # Get the categories and probabilities for all classes
133
  all_categories = [hate_speech_categories[index] for index in valid_indices]
134
  all_probabilities = [logits[index] for index in valid_indices]
135
+
 
136
  fig = px.bar(x=all_categories, y=all_probabilities, labels={'x': 'Categories', 'y': 'Probabilities'},
137
+ title=" ",
138
+ text=all_probabilities, color_discrete_sequence=['#ff7400'])
139
+
140
+ fig.update_traces(texttemplate='%{text:.2f}', textposition='outside')
141
+
142
+ # Rotate the text in x-axis by 90 degrees
143
+ fig.update_layout(xaxis_tickangle=-90)
144
+
145
+ # Increase the space around the chart
146
+ fig.update_layout(margin=dict(l=50, r=50, b=100, t=100))
147
+
148
+ # Set the y-axis range to go up to 1.1
149
+ fig.update_layout(yaxis=dict(range=[0, 1.1]))
150
+
151
+ return fig
152
 
153
  # Input components
154
  inputs = [
 
156
  gr.Dropdown(label="Model", choices=user_friendly_name_list, value=user_friendly_name_list[0])
157
  ]
158
 
 
159
  outputs = [
160
+ gr.Plot(label="Classes Predicted Probabilities") # Add this line
 
161
  ]
162
 
163
+ # Gradio interface without launching
164
+ interface = gr.Interface(fn=predict, inputs=inputs, outputs=outputs, title=app_title,
165
+ description=app_description, examples=app_examples, article=article_string, live=False)
166
+
167
+ # Launch the interface
168
+ interface.launch()
169
+
flagged/log.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Text,Model,Predominant category,Probability,flag,username,timestamp
2
+ mds mas o viado vir responder meus status falando q a taylor foi racista foi o auge 😂😂,BERTimbau large (TuPi),"{""label"":null,""confidences"":null}","{""label"":null,""confidences"":null}",,,2023-12-04 14:13:25.289476