inclusive-ml commited on
Commit
e95b0bd
·
1 Parent(s): a298c47

gender of sentence

Browse files
Files changed (1) hide show
  1. app.py +23 -12
app.py CHANGED
@@ -1,17 +1,28 @@
1
  import gradio as gr
 
2
 
 
 
 
 
 
 
3
 
4
- def main(text):
5
- # A gradio function that lowercases text and returns it
6
- return text.lower()
 
 
 
 
 
 
 
 
 
 
7
 
8
-
9
- # A simple gradio app for text
10
  app = gr.Interface(
11
- main,
12
- inputs="text",
13
- outputs="text",
14
- title="Spaces Template Gradio",
15
- description="A template app for Hugging Face Spaces",
16
- )
17
- app.launch(debug=True)
 
1
  import gradio as gr
2
+ import re
3
 
4
+ male_words, female_words = ["he", "his", "him"], ["she", "her"]
5
+ def gender_of_sentence(sentence):
6
+ male_count = len([word for word in sentence.split() if word.lower() in male_words])
7
+ female_count = len([word for word in sentence.split() if word.lower() in female_words])
8
+ total = max(male_count + female_count, 1)
9
+ return {"male": male_count / total, "female": female_count / total}
10
 
11
+ def interpret_gender(sentence):
12
+ result = gender_of_sentence(sentence)
13
+ is_male = result["male"] > result["female"]
14
+ interpretation = []
15
+ for word in re.split('( )', sentence):
16
+ score = 0
17
+ token = word.lower()
18
+ if (is_male and token in male_words) or (not is_male and token in female_words):
19
+ score = 1
20
+ elif (is_male and token in female_words) or (not is_male and token in male_words):
21
+ score = -1
22
+ interpretation.append((word, score))
23
+ return interpretation
24
 
 
 
25
  app = gr.Interface(
26
+ fn=gender_of_sentence, inputs=gr.inputs.Textbox(default="She went to his house to get her keys."),
27
+ outputs="label", interpretation=interpret_gender, enable_queue=True)
28
+ app.launch()