File size: 1,148 Bytes
510f51f
1a36fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
d71ddc4
0a8c97b
d71ddc4
 
0a8c97b
77a42a9
d71ddc4
 
1a36fb9
a204725
1a36fb9
0a8c97b
 
510f51f
 
 
 
 
 
 
 
9912ff0
1a36fb9
 
510f51f
 
0a8c97b
510f51f
0a8c97b
7ff0435
05c901d
510f51f
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
41
42
43
44
45
46
47
import gradio as gr
import numpy as np

def entropy(X):
    n = len(X)

    counts = np.bincount(X)
    probs = counts[np.nonzero(counts)] / n

    en = 0
    for i in range(len(probs)):
        en = en - probs[i] * np.log(probs[i])/np.log(2)

    return en

def string_to_list(string):
    # Divide the string into a list of substrings separated by commas
    substrings = string.split(',')
    
    # Convert each substring to an integer and add to a list
    response = substrings
    return response

def function(valores):
    return entropy(string_to_list(valores))

value1 = gr.Textbox(lines=3, label="Values", placeholder="Please enter the values separated by commas...")
value2 = gr.Textbox(lines=3, label="Result", placeholder="Result...")
                            
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 numbers."
)

demo.launch(debug=True)