import gradio as gr import pandas as pd import numpy as np import json from io import StringIO def test(input_json): print("Received input") # Parse the input JSON string try: inputs = json.loads(input_json) except json.JSONDecodeError: inputs = json.loads(input_json.replace("'", '"')) # Accessing the 'a_list' string and converting it to a list of integers ids_index = inputs['input']['ids_list'] # Extract the datatree part which is a list of dictionaries matrix = inputs['input']["matrix"] weights = inputs['input']["weights"] alpha = inputs['input']["alpha"] alpha = float(alpha) threshold = inputs['input']["threshold"] threshold = float(threshold) df_matrix = pd.DataFrame(matrix).T df_weights = pd.DataFrame(weights).T df_matrix = df_matrix.round(0).astype(int) df_weights = df_weights.round(0).astype(int) def computeAccessibility (DistanceMatrix,destinationWeights, alpha = 0.0038, threshold = 600): decay_factors = np.exp(-alpha * DistanceMatrix) * (DistanceMatrix <= threshold) subdomainsAccessibility = pd.DataFrame(index=DistanceMatrix.index, columns=destinationWeights.columns) for col in destinationWeights.columns: subdomainsAccessibility[col] = (decay_factors * destinationWeights[col].values).sum(axis=1) #subdomainsAccessibility.drop(columns='commercial', inplace=True) return subdomainsAccessibility subdomainsAccessibility = computeAccessibility(df_matrix,df_weights,alpha,threshold) subdomainsAccessibility_dict = subdomainsAccessibility.to_dict('index') for key, subdict in data['subdomainsAccessibility_dict'].items(): ordered = OrderedDict(sorted(subdict.items(), key=lambda x: int(x[0]))) subdomainsAccessibility_dict['subdomainsAccessibility_dict'][key] = ordered # Prepare the output output = { "list": ids_index, "subdomainsAccessibility_dict": subdomainsAccessibility_dict } return json.dumps(output) # Define the Gradio interface with a single JSON input iface = gr.Interface( fn=test, inputs=gr.Textbox(label="Input JSON", lines=20, placeholder="Enter JSON with all parameters here..."), outputs=gr.JSON(label="Output JSON"), title="testspace" ) iface.launch()