POrg commited on
Commit
23770d2
·
verified ·
1 Parent(s): d15afc2

Upload folder using huggingface_hub

Browse files
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Ocsai Drawings
3
- emoji: 🚀
4
- colorFrom: green
5
- colorTo: blue
6
  sdk: gradio
7
  sdk_version: 4.27.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Ocsai_Drawings
3
+ app_file: gradio-ocsai-d.py
 
 
4
  sdk: gradio
5
  sdk_version: 4.27.0
 
 
6
  ---
 
 
__pycache__/gradio-ocsai-d.cpython-310.pyc ADDED
Binary file (3.29 kB). View file
 
blanks/Images_11_blank.png ADDED
blanks/Images_12_blank.png ADDED
blanks/Images_13_blank.png ADDED
blanks/Images_15_blank.png ADDED
blanks/Images_17_blank.png ADDED
blanks/Images_19_blank.png ADDED
blanks/Images_3_blank.png ADDED
blanks/Images_4_blank.png ADDED
blanks/Images_56_blank.png ADDED
blanks/Images_7_blank.png ADDED
blanks/Images_8_blank.png ADDED
blanks/Images_9_blank.png ADDED
gradio-ocsai-d.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from transformers import pipeline
3
+ from PIL import Image
4
+ import gradio as gr
5
+ import pandas as pd
6
+ import numpy as np
7
+ import altair as alt
8
+
9
+
10
+ prompt_images = {
11
+ "Images_11": "blanks/Images_11_blank.png",
12
+ "Images_4": "blanks/Images_4_blank.png",
13
+ "Images_17": "blanks/Images_17_blank.png",
14
+ "Images_9": "blanks/Images_9_blank.png",
15
+ "Images_3": "blanks/Images_3_blank.png",
16
+ "Images_8": "blanks/Images_8_blank.png",
17
+ "Images_13": "blanks/Images_13_blank.png",
18
+ "Images_15": "blanks/Images_15_blank.png",
19
+ "Images_12": "blanks/Images_12_blank.png",
20
+ "Images_7": "blanks/Images_7_blank.png",
21
+ "Images_56": "blanks/Images_56_blank.png",
22
+ "Images_19": "blanks/Images_19_blank.png",
23
+ }
24
+
25
+ dist = pd.read_csv('./score_norm_distribution.csv', dtype=float)
26
+
27
+
28
+ def get_percentile(score):
29
+ return dist[dist['score_norm'] <= score].iloc[-1, 0]
30
+
31
+
32
+ def inverse_scale(logits):
33
+ # undo the min-max scaling that was done from the JRT range to 0-1
34
+ scaler_params = {'min': -3.024, 'max': 3.164, 'range': 6.188}
35
+ return logits * (scaler_params['range']) + scaler_params['min']
36
+
37
+
38
+ def get_predictions(img_dict):
39
+ # gradio passes a dictionary with background, composite, and layers
40
+ # the composite is what we want
41
+ img = img_dict['composite']
42
+ predictions = classifier(img)
43
+ return {
44
+ 'originality' : np.round(predictions[0]['score'], 2),
45
+ 'jrt' : np.round(inverse_scale(0), 2),
46
+ 'percentile': get_percentile(predictions[0]['score'])
47
+ }
48
+
49
+
50
+ base_chart = alt.Chart(dist).mark_line().encode(
51
+ x='percentile',
52
+ y='score_norm'
53
+ )
54
+
55
+ def classify_image(img_dict):
56
+ # gradio passes a dictionary with background, composite, and layers
57
+ # the composite is what we want
58
+ img = img_dict['composite']
59
+ p = get_predictions(img_dict)
60
+
61
+ percentile_mark = alt.Chart(pd.DataFrame({'y': [p['originality']]})).mark_rule(color='red').encode(y='y')
62
+
63
+ # Text annotation for the percentile mark
64
+ text = alt.Chart(pd.DataFrame({'y': [p['originality']], 'text': [f"Percentile: {p['percentile']}; Normalized Score: {p['originality']}"]})).mark_text(
65
+ align='left',
66
+ baseline='middle',
67
+ dx=7, dy=-8 # Nudges text to right so it doesn't overlap with the line
68
+ ).encode(
69
+ y='y',
70
+ text='text'
71
+ )
72
+ return base_chart + percentile_mark + text
73
+
74
+
75
+ def update_editor(background, img_editor):
76
+ # Clear layers and set the selected background
77
+ img_editor['background'] = background
78
+ img_editor['layers'] = []
79
+ img_editor['composite'] = None
80
+ return img_editor
81
+
82
+
83
+ classifier = pipeline("image-classification", model='POrg/ocsai-d-large')
84
+
85
+ editor = gr.ImageEditor(type='pil',
86
+ value=dict(
87
+ background=Image.open(prompt_images['Images_11']),
88
+ composite=None,
89
+ layers=[]
90
+ ),
91
+ brush=gr.Brush(
92
+ default_size=2,
93
+ colors=["#000000", '#333333', '#666666'],
94
+ color_mode="fixed"
95
+ ),
96
+ transforms=[],
97
+ sources=('upload', 'clipboard'),
98
+ layers=False
99
+ )
100
+
101
+ examples = []
102
+ for k, v in prompt_images.items():
103
+ examples.append(dict(background=Image.open(v), composite=None, layers=[]))
104
+
105
+ demo = gr.Interface(fn=classify_image,
106
+ inputs=[editor],
107
+ outputs=gr.Plot(),
108
+ title="Ocsai-D",
109
+ description="Complete the drawing and classify the originality. Examples are from MTCI ([Barbot 2018](https://pubmed.ncbi.nlm.nih.gov/30618952/)). Choose the brush icon below the image to start editing.",
110
+ examples=examples
111
+ )
112
+
113
+ demo.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ altair
2
+ transformers
3
+ pillow
4
+ pandas
5
+ numpy
score_norm_distribution.csv ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ percentile,score_norm
2
+ 0,0.0
3
+ 1,0.2658954269536899
4
+ 2,0.29457921333383075
5
+ 3,0.30310490366607795
6
+ 4,0.3112277375239338
7
+ 5,0.3200521694123999
8
+ 6,0.3281155279694775
9
+ 7,0.33505868144658657
10
+ 8,0.3392089731525685
11
+ 9,0.34112156925606424
12
+ 10,0.3469171398619739
13
+ 11,0.3511710111618856
14
+ 12,0.3568430068136271
15
+ 13,0.36180990478859454
16
+ 14,0.3620405096808955
17
+ 15,0.3660549175130392
18
+ 16,0.3713281681316126
19
+ 17,0.3758821435250105
20
+ 18,0.38007216818894674
21
+ 19,0.38286859218641567
22
+ 20,0.3851654423305501
23
+ 21,0.38878772721364474
24
+ 22,0.3933188590264441
25
+ 23,0.3985148402511076
26
+ 24,0.4020135391925982
27
+ 25,0.4059742928167005
28
+ 26,0.4116649490408034
29
+ 27,0.4158741294366881
30
+ 28,0.42095292315008237
31
+ 29,0.42500796302503663
32
+ 30,0.42947442498133825
33
+ 31,0.43421050644034465
34
+ 32,0.43848988145355117
35
+ 33,0.4428767461965395
36
+ 34,0.44774875322791224
37
+ 35,0.4520888259135842
38
+ 36,0.4561818524913554
39
+ 37,0.45951053385996776
40
+ 38,0.4654170913642474
41
+ 39,0.46936765339064945
42
+ 40,0.47374096009598826
43
+ 41,0.4778733813156729
44
+ 42,0.48168420678878365
45
+ 43,0.4858078800434697
46
+ 44,0.489977885665859
47
+ 45,0.49222064341619076
48
+ 46,0.4935026730860239
49
+ 47,0.49741882057510833
50
+ 48,0.5018924541619932
51
+ 49,0.5059561160864396
52
+ 50,0.5104564475386445
53
+ 51,0.5147230242331543
54
+ 52,0.5183986906290778
55
+ 53,0.5225670918406934
56
+ 54,0.5259651360191281
57
+ 55,0.5288777252975223
58
+ 56,0.5324189434087236
59
+ 57,0.5366995325233963
60
+ 58,0.5407957310743421
61
+ 59,0.5445732698714345
62
+ 60,0.5486969650375954
63
+ 61,0.5517929075887892
64
+ 62,0.5552200888240536
65
+ 63,0.5591196062209663
66
+ 64,0.5634000324385277
67
+ 65,0.5671347562440855
68
+ 66,0.5706120211744352
69
+ 67,0.573789230587204
70
+ 68,0.577790110565303
71
+ 69,0.5822551850499115
72
+ 70,0.586409034837951
73
+ 71,0.590284528513099
74
+ 72,0.5940106484952778
75
+ 73,0.5977210767573214
76
+ 74,0.602416228103494
77
+ 75,0.6066765752140205
78
+ 76,0.6120308682527665
79
+ 77,0.6158761623245004
80
+ 78,0.6200640057567599
81
+ 79,0.6246296981878875
82
+ 80,0.6293587808987731
83
+ 81,0.6344289521636639
84
+ 82,0.6395740140855798
85
+ 83,0.6448253685442593
86
+ 84,0.650423636218271
87
+ 85,0.6558870284254413
88
+ 86,0.6615215893114951
89
+ 87,0.6666412816994163
90
+ 88,0.6733659429202951
91
+ 89,0.6803897908526316
92
+ 90,0.6873526726184538
93
+ 91,0.6954822613275397
94
+ 92,0.7039240961497323
95
+ 93,0.7140753120540401
96
+ 94,0.7245636076473253
97
+ 95,0.7356199857393928
98
+ 96,0.7505542818394643
99
+ 97,0.7701851740360041
100
+ 98,0.794328590193434
101
+ 99,0.8426026407211062
102
+ 100,0.9929171993962822