Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import json | |
| def convert_inputs(user_input, var): | |
| if isinstance(var, str): | |
| var = [var] | |
| # diff = len(var) - len(user_input) | |
| var += var*(len(user_input)//len(var) + 1) | |
| diff = len(var) - len(user_input) | |
| # if diff < 0: | |
| # # for _ in range(-diff): | |
| # # var.append(var[0]) | |
| # add_var = var[:-diff] | |
| # if len(add_var) < -diff: | |
| # var += add_var * (1 - diff - len(add_var)) | |
| # else: | |
| # var += add_var | |
| # if len(var) - len(user_input): | |
| var = var[:-diff] | |
| print("[LENGTH]", len(var), diff, len(add_var)) | |
| return var | |
| def process_inputs(user_input_json, session_id_json, project_id_json, chat_url, update_vars_json, output_vars_json): | |
| # Convert JSON strings into Python lists | |
| user_input = json.loads(user_input_json) | |
| session_id = json.loads(session_id_json) | |
| project_id = json.loads(project_id_json) | |
| update_vars = json.loads(update_vars_json) | |
| output_vars = json.loads(output_vars_json) | |
| # if isinstance(session_id, str): | |
| # session_id = [session_id] | |
| # if isinstance(project_id, str): | |
| # session_id = [project_id] | |
| # if isinstance(chat_url, str): | |
| # session_id = [session_id] | |
| session_id = convert_inputs(user_input,session_id) | |
| project_id = convert_inputs(user_input,project_id) | |
| update_vars = convert_inputs(user_input,update_vars) | |
| output_vars = convert_inputs(user_input,output_vars) | |
| # --- Your function logic here --- | |
| df = pd.DataFrame({ | |
| "user_input": user_input, | |
| "session_id": session_id, | |
| "project_id": project_id, | |
| "chat_url": [chat_url] * len(user_input), | |
| "update_variables": update_vars, | |
| "output_variables": output_vars, | |
| "answer": [[] * len(x) if isinstance(x, list) else [] for x in user_input] | |
| }) | |
| return df | |
| def run_process(df): | |
| csv_path = "output.csv" | |
| df.to_csv(csv_path, index=False) | |
| return csv_path | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## π¬ JSON Input β DataFrame β CSV Export") | |
| user_input = gr.Code(label="user_input (list[str] or list[list[str]])", language="json", value='["Hello", ["Hi", "How are you?"]]') | |
| session_id = gr.Code(label="session_id (list[str])", language="json", value='["s1", "s2"]') | |
| project_id = gr.Code(label="project_id (list[str])", language="json", value='["p1", "p2"]') | |
| chat_url = gr.Textbox(label="chat_url", value="https://example.com/chat") | |
| update_vars = gr.Code(label="update_variables (list[str])", language="json", value='["update1", "update2"]') | |
| output_vars = gr.Code(label="output_variables (list[str])", language="json", value='["out1", "out2"]') | |
| run_btn = gr.Button("Run Function") | |
| df_output = gr.Dataframe(label="Output DataFrame", interactive=True) | |
| process_btn = gr.Button("Process") | |
| file_output = gr.File(label="Download CSV") | |
| process_btn.click( | |
| fn=run_process, | |
| inputs=df_output, | |
| outputs=file_output | |
| ) | |
| run_btn.click( | |
| fn=process_inputs, | |
| inputs=[user_input, session_id, project_id, chat_url, update_vars, output_vars], | |
| outputs=df_output | |
| ) | |
| demo.launch() | |