Upload with huggingface_hub
Browse files
README.md
CHANGED
@@ -6,6 +6,6 @@ colorFrom: indigo
|
|
6 |
colorTo: indigo
|
7 |
sdk: gradio
|
8 |
sdk_version: 3.4.1
|
9 |
-
app_file:
|
10 |
pinned: false
|
11 |
---
|
|
|
6 |
colorTo: indigo
|
7 |
sdk: gradio
|
8 |
sdk_version: 3.4.1
|
9 |
+
app_file: run.py
|
10 |
pinned: false
|
11 |
---
|
run.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
male_words, female_words = ["he", "his", "him"], ["she", "hers", "her"]
|
6 |
+
|
7 |
+
|
8 |
+
def gender_of_sentence(sentence):
|
9 |
+
male_count = len([word for word in sentence.split() if word.lower() in male_words])
|
10 |
+
female_count = len(
|
11 |
+
[word for word in sentence.split() if word.lower() in female_words]
|
12 |
+
)
|
13 |
+
total = max(male_count + female_count, 1)
|
14 |
+
return {"male": male_count / total, "female": female_count / total}
|
15 |
+
|
16 |
+
|
17 |
+
# Number of arguments to interpretation function must
|
18 |
+
# match number of inputs to prediction function
|
19 |
+
def interpret_gender(sentence):
|
20 |
+
result = gender_of_sentence(sentence)
|
21 |
+
is_male = result["male"] > result["female"]
|
22 |
+
interpretation = []
|
23 |
+
for word in re.split("( )", sentence):
|
24 |
+
score = 0
|
25 |
+
token = word.lower()
|
26 |
+
if (is_male and token in male_words) or (not is_male and token in female_words):
|
27 |
+
score = 1
|
28 |
+
elif (is_male and token in female_words) or (
|
29 |
+
not is_male and token in male_words
|
30 |
+
):
|
31 |
+
score = -1
|
32 |
+
interpretation.append((word, score))
|
33 |
+
# Output must be a list of lists containing the same number of elements as inputs
|
34 |
+
# Each element corresponds to the interpretation scores for the given input
|
35 |
+
return [interpretation]
|
36 |
+
|
37 |
+
|
38 |
+
demo = gr.Interface(
|
39 |
+
fn=gender_of_sentence,
|
40 |
+
inputs=gr.Textbox(value="She went to his house to get her keys."),
|
41 |
+
outputs="label",
|
42 |
+
interpretation=interpret_gender,
|
43 |
+
)
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
demo.launch()
|