Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from datasets import load_dataset
|
4 |
+
from io import StringIO
|
5 |
+
|
6 |
+
# دیتافریم جهانی
|
7 |
+
global_df = pd.DataFrame()
|
8 |
+
|
9 |
+
# بارگذاری دیتاست از Hugging Face
|
10 |
+
def load_hf_dataset(hf_path):
|
11 |
+
global global_df
|
12 |
+
try:
|
13 |
+
dataset = load_dataset(hf_path)
|
14 |
+
split = list(dataset.keys())[0]
|
15 |
+
data = dataset[split]
|
16 |
+
df = pd.DataFrame(data)
|
17 |
+
global_df = df.copy()
|
18 |
+
return df, f"✅ Dataset loaded from '{hf_path}' with {len(df)} rows."
|
19 |
+
except Exception as e:
|
20 |
+
return None, f"❌ Error loading dataset:\n{str(e)}"
|
21 |
+
|
22 |
+
# اضافه کردن ردیف
|
23 |
+
def add_row():
|
24 |
+
global global_df
|
25 |
+
empty_row = {col: "" for col in global_df.columns}
|
26 |
+
global_df = pd.concat([global_df, pd.DataFrame([empty_row])], ignore_index=True)
|
27 |
+
return global_df
|
28 |
+
|
29 |
+
# حذف ردیف بر اساس شماره ردیف
|
30 |
+
def delete_row(index):
|
31 |
+
global global_df
|
32 |
+
if 0 <= index < len(global_df):
|
33 |
+
global_df = global_df.drop(index).reset_index(drop=True)
|
34 |
+
return global_df
|
35 |
+
|
36 |
+
# اضافه کردن ستون
|
37 |
+
def add_column(col_name):
|
38 |
+
global global_df
|
39 |
+
if col_name and col_name not in global_df.columns:
|
40 |
+
global_df[col_name] = ""
|
41 |
+
return global_df
|
42 |
+
|
43 |
+
# حذف ستون
|
44 |
+
def delete_column(col_name):
|
45 |
+
global global_df
|
46 |
+
if col_name in global_df.columns:
|
47 |
+
global_df = global_df.drop(columns=[col_name])
|
48 |
+
return global_df
|
49 |
+
|
50 |
+
# تغییر نام ستون
|
51 |
+
def rename_column(old_name, new_name):
|
52 |
+
global global_df
|
53 |
+
if old_name in global_df.columns and new_name:
|
54 |
+
global_df = global_df.rename(columns={old_name: new_name})
|
55 |
+
return global_df
|
56 |
+
|
57 |
+
# گرفتن خروجی CSV
|
58 |
+
def download_csv():
|
59 |
+
global global_df
|
60 |
+
csv_str = global_df.to_csv(index=False)
|
61 |
+
return csv_str
|
62 |
+
|
63 |
+
# رابط کاربری Gradio
|
64 |
+
with gr.Blocks(title="HuggingFace Dataset Editor") as demo:
|
65 |
+
gr.Markdown("## 🧬 Hugging Face Dataset Editor (Gradio Web App)")
|
66 |
+
|
67 |
+
with gr.Row():
|
68 |
+
dataset_input = gr.Textbox(label="Hugging Face Dataset Path (e.g. `codersan/Persian-Wikipedia-Corpus`)", value="")
|
69 |
+
load_btn = gr.Button("🔄 Load Dataset")
|
70 |
+
|
71 |
+
status = gr.Textbox(label="Status", interactive=False)
|
72 |
+
data_table = gr.Dataframe(label="Dataset", wrap=True, interactive=True)
|
73 |
+
|
74 |
+
load_btn.click(fn=load_hf_dataset, inputs=dataset_input, outputs=[data_table, status])
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
add_row_btn = gr.Button("➕ Add Row")
|
78 |
+
del_row_idx = gr.Number(label="Row Index to Delete", value=0)
|
79 |
+
delete_row_btn = gr.Button("🗑 Delete Row")
|
80 |
+
|
81 |
+
add_row_btn.click(fn=add_row, outputs=data_table)
|
82 |
+
delete_row_btn.click(fn=delete_row, inputs=del_row_idx, outputs=data_table)
|
83 |
+
|
84 |
+
with gr.Row():
|
85 |
+
col_name_add = gr.Textbox(label="New Column Name")
|
86 |
+
add_col_btn = gr.Button("➕ Add Column")
|
87 |
+
col_name_del = gr.Textbox(label="Column Name to Delete")
|
88 |
+
del_col_btn = gr.Button("🗑 Delete Column")
|
89 |
+
|
90 |
+
add_col_btn.click(fn=add_column, inputs=col_name_add, outputs=data_table)
|
91 |
+
del_col_btn.click(fn=delete_column, inputs=col_name_del, outputs=data_table)
|
92 |
+
|
93 |
+
with gr.Row():
|
94 |
+
old_col = gr.Textbox(label="Old Column Name")
|
95 |
+
new_col = gr.Textbox(label="New Column Name")
|
96 |
+
rename_btn = gr.Button("✏️ Rename Column")
|
97 |
+
|
98 |
+
rename_btn.click(fn=rename_column, inputs=[old_col, new_col], outputs=data_table)
|
99 |
+
|
100 |
+
gr.Markdown("### 📤 Export Dataset as CSV")
|
101 |
+
|
102 |
+
csv_btn = gr.Button("📁 Generate CSV")
|
103 |
+
csv_output = gr.File(label="Download CSV")
|
104 |
+
|
105 |
+
def csv_download_link():
|
106 |
+
csv_str = download_csv()
|
107 |
+
return gr.File.update(value=StringIO(csv_str), filename="dataset.csv")
|
108 |
+
|
109 |
+
csv_btn.click(fn=csv_download_link, outputs=csv_output)
|
110 |
+
|
111 |
+
demo.launch()
|