Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from scipy.stats import multivariate_normal as mvn
|
| 3 |
+
from scipy.stats import norm
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
from functions import *
|
| 8 |
+
|
| 9 |
+
def main(file):
|
| 10 |
+
|
| 11 |
+
# Read input file
|
| 12 |
+
with open(file.name) as json_file:
|
| 13 |
+
data = json.load(json_file)
|
| 14 |
+
|
| 15 |
+
# Validate input
|
| 16 |
+
team_stats = [person['attributes'] for person in data['team']]
|
| 17 |
+
applicant_stats = [person['attributes'] for person in data['applicants']]
|
| 18 |
+
if len(team_stats) < 2:
|
| 19 |
+
print('Input file must have at least 2 team members to score applicants')
|
| 20 |
+
return None
|
| 21 |
+
if len(team_stats[0]) == 0:
|
| 22 |
+
print('Team members must have at least 1 attribute')
|
| 23 |
+
return None
|
| 24 |
+
if not all_equal([tuple(person.keys()) for person in team_stats + applicant_stats]):
|
| 25 |
+
print('Input file has incomplete data. Please input all attributes for all people')
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Build model
|
| 30 |
+
# Model each attribute as a normal distribution, with each attribute independent from eachother
|
| 31 |
+
attributes = tuple(team_stats[0].keys())
|
| 32 |
+
dist = [[person[attribute] for person in team_stats] for attribute in attributes]
|
| 33 |
+
multivariate_normal_model = {
|
| 34 |
+
"attributes": attributes,
|
| 35 |
+
"distribution": dist,
|
| 36 |
+
"means":[np.mean(feature) for feature in dist],
|
| 37 |
+
"covariance_matrix": make_independent(np.cov(dist))
|
| 38 |
+
}
|
| 39 |
+
model = mvn(mean=multivariate_normal_model['means'],
|
| 40 |
+
cov=multivariate_normal_model['covariance_matrix'], allow_singular=True)
|
| 41 |
+
|
| 42 |
+
# Calculate scores
|
| 43 |
+
scores = [{'name': applicant['name'],
|
| 44 |
+
'score': compatibility(model, multivariate_normal_model['attributes'], applicant['attributes'])}
|
| 45 |
+
for applicant in data['applicants']]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# Write results to scores.json
|
| 49 |
+
json_object = json.dumps({"scoredApplicants": scores}, indent=4)
|
| 50 |
+
with open("scores.json", "w") as outfile:
|
| 51 |
+
outfile.write(json_object)
|
| 52 |
+
|
| 53 |
+
return "scores.json"
|
| 54 |
+
|
| 55 |
+
demo = gr.Interface(main, "file", "file")
|
| 56 |
+
|
| 57 |
+
demo.launch()
|