Spaces:
Configuration error
Configuration error
File size: 5,738 Bytes
a779273 |
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
import numpy as np
import pandas as pd
import gradio as gr
from abc import ABC, abstractmethod
from modules.module_WordExplorer import WordExplorer
from modules.module_BiasExplorer import WordBiasExplorer
class Connector(ABC):
def parse_word(self, word : str):
return word.lower().strip()
def parse_words(self, array_in_string : str):
words = array_in_string.strip()
if not words:
return []
words = [self.parse_word(word) for word in words.split(',') if word.strip() != '']
return words
def process_error(self, err: str):
if err is None:
return
return "<center><h3>" + err + "</h3></center>"
class WordExplorerConnector(Connector):
def __init__(self, **kwargs):
if 'embedding' in kwargs:
embedding = kwargs.get('embedding')
else:
raise KeyError
self.word_explorer = WordExplorer(embedding)
def plot_proyection_2d( self,
wordlist_0,
wordlist_1,
wordlist_2,
wordlist_3,
wordlist_4,
color_wordlist_0,
color_wordlist_1,
color_wordlist_2,
color_wordlist_3,
color_wordlist_4,
n_alpha,
fontsize,
n_neighbors
):
err = ""
neighbors_method = 'sklearn'
wordlist_0 = self.parse_words(wordlist_0)
wordlist_1 = self.parse_words(wordlist_1)
wordlist_2 = self.parse_words(wordlist_2)
wordlist_3 = self.parse_words(wordlist_3)
wordlist_4 = self.parse_words(wordlist_4)
if not (wordlist_0 or wordlist_1 or wordlist_2 or wordlist_1 or wordlist_4):
err = self.process_error("Ingresa al menos 1 palabras para continuar")
return None, err
err = self.word_explorer.check_oov([wordlist_0, wordlist_1, wordlist_2, wordlist_3, wordlist_4])
if err:
return None, self.process_error(err)
fig = self.word_explorer.plot_projections_2d(wordlist_0,
wordlist_1,
wordlist_2,
wordlist_3,
wordlist_4,
color_wordlist_0=color_wordlist_0,
color_wordlist_1=color_wordlist_1,
color_wordlist_2=color_wordlist_2,
color_wordlist_3=color_wordlist_3,
color_wordlist_4=color_wordlist_4,
n_alpha=n_alpha,
fontsize=fontsize,
n_neighbors=n_neighbors,
nn_method = neighbors_method
)
return fig, self.process_error(err)
class BiasWordExplorerConnector(Connector):
def __init__(self, **kwargs):
if 'embedding' in kwargs:
embedding = kwargs.get('embedding')
else:
raise KeyError
self.bias_word_explorer = WordBiasExplorer(embedding)
def calculate_bias_2d(self,
wordlist_1,
wordlist_2,
to_diagnose_list
):
err = ""
wordlist_1 = self.parse_words(wordlist_1)
wordlist_2 = self.parse_words(wordlist_2)
to_diagnose_list = self.parse_words(to_diagnose_list)
word_lists = [wordlist_1, wordlist_2, to_diagnose_list]
for list in word_lists:
if not list:
err = "Debe ingresar al menos 1 palabra en las lista de palabras a diagnosticar, sesgo 1 y sesgo 2"
if err:
return None, self.process_error(err)
err = self.bias_word_explorer.check_oov(word_lists)
if err:
return None, self.process_error(err)
fig = self.bias_word_explorer.plot_biased_words(to_diagnose_list, wordlist_2, wordlist_1)
return fig, self.process_error(err)
def calculate_bias_4d(self,
wordlist_1,
wordlist_2,
wordlist_3,
wordlist_4,
to_diagnose_list
):
err = ""
wordlist_1 = self.parse_words(wordlist_1)
wordlist_2 = self.parse_words(wordlist_2)
wordlist_3 = self.parse_words(wordlist_3)
wordlist_4 = self.parse_words(wordlist_4)
to_diagnose_list = self.parse_words(to_diagnose_list)
wordlists = [wordlist_1, wordlist_2, wordlist_3, wordlist_4, to_diagnose_list]
for list in wordlists:
if not list:
err = "¡Para graficar con 4 espacios, debe ingresar al menos 1 palabra en todas las listas!"
if err:
return None, self.process_error(err)
err = self.bias_word_explorer.check_oov(wordlists)
if err:
return None, self.process_error(err)
fig = self.bias_word_explorer.plot_biased_words(to_diagnose_list, wordlist_1, wordlist_2, wordlist_3, wordlist_4)
return fig, self.process_error(err) |