File size: 1,990 Bytes
ce05002
 
 
ce04ad4
2273894
ce05002
 
 
ce04ad4
 
 
 
 
 
 
 
 
 
ce05002
ce04ad4
 
 
 
 
 
 
ce05002
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce04ad4
 
 
 
2273894
 
ce05002
 
ce04ad4
ce05002
ce04ad4
ce05002
 
 
 
e028d48
 
 
 
 
 
 
ce05002
 
2273894
e028d48
ce05002
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import gradio as gr
import matplotlib.pyplot as plt
from matplotlib_venn import venn3
from io import BytesIO
from PIL import Image

def calculate_probabilities(A, B, C, AB, AC, BC, ABC):
    total = A + B + C - AB - AC - BC + ABC
    if total == 0:
        return {
            "P(A)": 0,
            "P(B)": 0,
            "P(C)": 0,
            "P(A ∩ B)": 0,
            "P(A ∩ C)": 0,
            "P(B ∩ C)": 0,
            "P(A ∩ B ∩ C)": 0,
        }

    P_A = A / total if total > 0 else 0
    P_B = B / total if total > 0 else 0
    P_C = C / total if total > 0 else 0
    P_AB = AB / total if total > 0 else 0
    P_AC = AC / total if total > 0 else 0
    P_BC = BC / total if total > 0 else 0
    P_ABC = ABC / total if total > 0 else 0
    
    return {
        "P(A)": P_A,
        "P(B)": P_B,
        "P(C)": P_C,
        "P(A ∩ B)": P_AB,
        "P(A ∩ C)": P_AC,
        "P(B ∩ C)": P_BC,
        "P(A ∩ B ∩ C)": P_ABC,
    }

def plot_venn(A, B, C, AB, AC, BC, ABC):
    plt.figure(figsize=(8, 8))
    venn = venn3(subsets=(A, B, AB, C, AC, BC, ABC), set_labels=('A', 'B', 'C'))
    plt.title("Venn Diagram")
    
    buf = BytesIO()
    plt.savefig(buf, format='png')
    buf.seek(0)
    img = Image.open(buf)
    return img

def main(A, B, C, AB, AC, BC, ABC):
    venn_diagram = plot_venn(A, B, C, AB, AC, BC, ABC)
    probabilities = calculate_probabilities(A, B, C, AB, AC, BC, ABC)
    return venn_diagram, probabilities

iface = gr.Interface(
    fn=main,
    inputs=[
        gr.Number(label="Cantidad en A"),
        gr.Number(label="Cantidad en B"),
        gr.Number(label="Cantidad en C"),
        gr.Number(label="Cantidad en A ∩ B"),
        gr.Number(label="Cantidad en A ∩ C"),
        gr.Number(label="Cantidad en B ∩ C"),
        gr.Number(label="Cantidad en A ∩ B ∩ C")
    ],
    outputs=[
        gr.Image(type="pil", label="Diagrama de Venn"),
        gr.JSON(label="Probabilidades Calculadas")
    ]
)

iface.launch()