File size: 1,013 Bytes
960447f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np

def entropy_calculator(values):
    n = len(values)

    counts = np.bincount(values)
    probabilities = counts[np.nonzero(counts)] / n
    
    result = 0
    for i in range(len(probabilities)):
        result = result - probabilities[i] * np.log(probabilities[i])/np.log(2)

    return result

def function(values):
    substrings = values.split(',')
    return entropy_calculator(substrings)

value1 = gr.Textbox(lines=3, label="Values", placeholder="Please enter the values separated by commas...")
value2 = gr.Textbox(lines=3, label="Entropy", placeholder="Entropy...")
                            
examples = [
    ["1,2,3,4,5,6,7,8,9"],
    ["1,1,1,1,1,1,1,1,2"],
    ["1,2,1,2,1,2,1,2,1"],
    ["8,8,8,8,8,8,8,8,8"],
    ["1,2,3,1,2,3,1,2,3"]
]

demo = gr.Interface(
    fn=function,
    inputs=value1,
    outputs=value2,
    title="Entropy calculator",
    examples=examples,
    description="Calculate the entropy of a set of values."
)

demo.launch(debug=True)