Spaces:
Sleeping
Sleeping
File size: 2,722 Bytes
2fe4149 ea40bfb 20511e3 ea40bfb 20511e3 17370e6 20511e3 4510b89 20511e3 4510b89 20511e3 2fe4149 20511e3 2fe4149 20511e3 2fe4149 4510b89 2fe4149 4510b89 e24b691 4510b89 20511e3 4510b89 20511e3 2fe4149 |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
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)
if diff < 0:
for _ in range(diff*-1):
var.append(var[0])
var += var[0:diff*-1] * (1+(len(var[0:diff*-1]) - diff))
elif diff > 0:
var = var[:-diff]
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]
# --- 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()
|