Spaces:
Sleeping
Sleeping
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 | |
a_list = inputs['input']['a_list'] | |
# Extract the datatree part which is a list of dictionaries | |
datatree = inputs["datatree"] | |
# Rebuild a Python dictionary from the datatree | |
# Initialize an empty dictionary | |
python_dict = {} | |
# Iterate through each item in the datatree list | |
for branch_dict in datatree: | |
# Each branch_dict is a dictionary with one key-value pair | |
for key, value in branch_dict.items(): | |
# Assign the key and value to the new dictionary | |
python_dict[key] = value | |
#a_list = [int(item.strip()) for item in a_list_string.split(',')] | |
#print("Parsed input keys:", inputs.keys()) | |
#print("Parsed input values:", inputs.values()) | |
#sum_list = sum(float(item) for item in inputs["a_list"]) | |
#multiplication = [-float((inputs["alpha"])) * float(item) for item in inputs["a_list"]] | |
#new_df = pd.DataFrame(index=inputs["dataframe"].index, columns=inputs["dataframe"].columns) | |
#multiplier_series = pd.Series(float(inputs["a_list"]), index=inputs["dataframe"].index) | |
#new_df["new column"] = float(inputs["dataframe"]).mul(multiplier_series, axis=0) | |
# Prepare the output | |
output = { | |
"list": a_list, | |
"matrix": python_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() |