Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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()
|