import gradio as gr import numpy as np def calculate_correlation(x_values, y_values): try: x_values = list(map(float, x_values.split('\n'))) y_values = list(map(float, y_values.split('\n'))) except ValueError: return "Error: All values must be numbers." if len(x_values) != len(y_values): return "Error: The number of X and Y values must be the same." n = len(x_values) sum_x = sum(x_values) sum_y = sum(y_values) sum_xy = sum(x * y for x, y in zip(x_values, y_values)) sum_x_squared = sum(x * x for x in x_values) sum_y_squared = sum(y * y for y in y_values) numerator = n * sum_xy - sum_x * sum_y denominator = np.sqrt((n * sum_x_squared - sum_x ** 2) * (n * sum_y_squared - sum_y ** 2)) if denominator == 0: return "Error: Cannot calculate correlation coefficient. Check your data." correlation = numerator / denominator return f"The Karl Pearson correlation coefficient is: {correlation:.10f}" iface = gr.Interface( fn=calculate_correlation, inputs=[ gr.components.Textbox(lines=10, placeholder="Enter X values (one per line)"), gr.components.Textbox(lines=10, placeholder="Enter Y values (one per line)") ], outputs="text", title="Karl Pearson Correlation Coefficient Calculator", description="Enter your X and Y values (one per line) to calculate the Karl Pearson correlation coefficient.", theme="compact" ) iface.launch()