|
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) |