jonathanjordan21 commited on
Commit
20511e3
Β·
verified Β·
1 Parent(s): 2fe4149

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -38
app.py CHANGED
@@ -2,48 +2,48 @@ import gradio as gr
2
  import pandas as pd
3
  import json
4
 
5
- # --- Your custom function ---
6
- def my_function(data_list):
7
- # Example transformation – replace this with your logic
8
- df = pd.DataFrame(data_list)
9
- df["new_col"] = df.index + 1
10
- return df
11
-
12
- # --- Wrapper for Gradio ---
13
- def process_json_list(json_text):
14
- try:
15
- # Parse JSON list input
16
- data = json.loads(json_text)
17
- if not isinstance(data, list):
18
- return None, "Error: Input must be a JSON list of objects"
19
-
20
- # Apply your function
21
- df = my_function(data)
22
-
23
- # Save to CSV
24
- csv_path = "output.csv"
25
- df.to_csv(csv_path, index=False)
26
-
27
- return df, csv_path
28
- except Exception as e:
29
- return None, f"Error: {str(e)}"
30
-
31
- # --- Gradio Interface ---
32
  with gr.Blocks() as demo:
33
- gr.Markdown("## 🧠 JSON β†’ DataFrame β†’ CSV Converter")
34
- gr.Markdown("Paste your JSON list below and get both a table and downloadable CSV.")
35
-
36
- json_input = gr.Code(
37
- label="Input JSON List",
38
- language="json",
39
- value='[{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}]'
40
- )
41
 
42
- run_btn = gr.Button("Convert")
43
 
44
- df_output = gr.Dataframe(label="Processed DataFrame", interactive=False)
45
  file_output = gr.File(label="Download CSV")
46
 
47
- run_btn.click(fn=process_json_list, inputs=json_input, outputs=[df_output, file_output])
 
 
 
 
48
 
49
  demo.launch()
 
2
  import pandas as pd
3
  import json
4
 
5
+ def process_inputs(user_input_json, session_id_json, project_id_json, chat_url, update_vars_json, output_vars_json):
6
+ # Convert JSON strings into Python lists
7
+ user_input = json.loads(user_input_json)
8
+ session_id = json.loads(session_id_json)
9
+ project_id = json.loads(project_id_json)
10
+ update_vars = json.loads(update_vars_json)
11
+ output_vars = json.loads(output_vars_json)
12
+
13
+ # --- Your function logic here ---
14
+ df = pd.DataFrame({
15
+ "user_input": user_input,
16
+ "session_id": session_id,
17
+ "project_id": project_id,
18
+ "chat_url": [chat_url] * len(user_input),
19
+ "update_variables": update_vars,
20
+ "output_variables": output_vars
21
+ })
22
+
23
+ csv_path = "output.csv"
24
+ df.to_csv(csv_path, index=False)
25
+ return df, csv_path
26
+
27
+
 
 
 
 
28
  with gr.Blocks() as demo:
29
+ gr.Markdown("## πŸ’¬ JSON Input ➜ DataFrame ➜ CSV Export")
30
+
31
+ user_input = gr.Code(label="user_input (list[str] or list[list[str]])", language="json", value='["Hello", ["Hi", "How are you?"]]')
32
+ session_id = gr.Code(label="session_id (list[str])", language="json", value='["s1", "s2"]')
33
+ project_id = gr.Code(label="project_id (list[str])", language="json", value='["p1", "p2"]')
34
+ chat_url = gr.Textbox(label="chat_url", value="https://example.com/chat")
35
+ update_vars = gr.Code(label="update_variables (list[str])", language="json", value='["update1", "update2"]')
36
+ output_vars = gr.Code(label="output_variables (list[str])", language="json", value='["out1", "out2"]')
37
 
38
+ run_btn = gr.Button("Run Function")
39
 
40
+ df_output = gr.Dataframe(label="Output DataFrame", interactive=False)
41
  file_output = gr.File(label="Download CSV")
42
 
43
+ run_btn.click(
44
+ fn=process_inputs,
45
+ inputs=[user_input, session_id, project_id, chat_url, update_vars, output_vars],
46
+ outputs=[df_output, file_output]
47
+ )
48
 
49
  demo.launch()