Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,60 @@
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
import random
|
|
|
4 |
|
5 |
-
def
|
6 |
-
"""
|
7 |
-
|
8 |
-
|
9 |
-
for line in f:
|
10 |
-
data.append(json.loads(line))
|
11 |
-
return data
|
12 |
|
13 |
-
def
|
14 |
-
"""
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
data =
|
19 |
if len(data) == 0:
|
20 |
-
return "
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
24 |
output = "\n".join([f"**{key}**: {value}" for key, value in random_entry.items()])
|
25 |
-
return output,
|
26 |
|
27 |
def sample_more(data):
|
28 |
"""从已有数据中再采样一条"""
|
29 |
if not data:
|
30 |
-
return "
|
31 |
|
32 |
random_entry = random.choice(data)
|
33 |
-
# 格式化输出为Markdown
|
34 |
output = "\n".join([f"**{key}**: {value}" for key, value in random_entry.items()])
|
35 |
return output
|
36 |
|
37 |
# Gradio 界面
|
38 |
with gr.Blocks() as app:
|
39 |
-
gr.Markdown("#
|
40 |
-
gr.Markdown("
|
41 |
|
42 |
with gr.Row():
|
43 |
-
|
44 |
sample_button = gr.Button("再采样")
|
45 |
|
46 |
output_box = gr.Textbox(label="随机数据", lines=10, max_lines=20)
|
@@ -48,8 +62,8 @@ with gr.Blocks() as app:
|
|
48 |
# 用于存储加载后的数据
|
49 |
state_data = gr.State()
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
|
54 |
# 绑定事件:点击按钮后从已有数据中再采样
|
55 |
sample_button.click(sample_more, inputs=state_data, outputs=output_box)
|
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
import random
|
4 |
+
from datasets import load_dataset
|
5 |
|
6 |
+
def load_huggingface_dataset(dataset_name):
|
7 |
+
"""从 Hugging Face 加载数据集"""
|
8 |
+
dataset = load_dataset(dataset_name)
|
9 |
+
return dataset['train'] # 假设我们使用训练集
|
|
|
|
|
|
|
10 |
|
11 |
+
def parse_json_or_jsonl(data):
|
12 |
+
"""解析 JSON 或 JSONL 格式的数据"""
|
13 |
+
parsed_data = []
|
14 |
+
if isinstance(data, list): # 如果是 JSON 格式(列表)
|
15 |
+
parsed_data = data
|
16 |
+
elif isinstance(data, str): # 如果是 JSONL 格式(每行一个 JSON 对象)
|
17 |
+
for line in data.splitlines():
|
18 |
+
if line.strip(): # 跳过空行
|
19 |
+
parsed_data.append(json.loads(line))
|
20 |
+
return parsed_data
|
21 |
+
|
22 |
+
def random_data_viewer(dataset_name):
|
23 |
+
"""从 Hugging Face 数据集中随机抽取一条数据"""
|
24 |
+
if dataset_name is None:
|
25 |
+
return "请选择一个数据集!", None
|
26 |
|
27 |
+
data = load_huggingface_dataset(dataset_name)
|
28 |
if len(data) == 0:
|
29 |
+
return "数据集为空或格式不正确!", None
|
30 |
|
31 |
+
# 将数据集转换为列表形式
|
32 |
+
data_list = [item for item in data]
|
33 |
+
|
34 |
+
# 随机选择一条数据
|
35 |
+
random_entry = random.choice(data_list)
|
36 |
+
|
37 |
+
# 格式化输出为 Markdown
|
38 |
output = "\n".join([f"**{key}**: {value}" for key, value in random_entry.items()])
|
39 |
+
return output, data_list # 返回数据以存储供后续使用
|
40 |
|
41 |
def sample_more(data):
|
42 |
"""从已有数据中再采样一条"""
|
43 |
if not data:
|
44 |
+
return "没有可用数据,请先选择一个数据集!"
|
45 |
|
46 |
random_entry = random.choice(data)
|
47 |
+
# 格式化输出为 Markdown
|
48 |
output = "\n".join([f"**{key}**: {value}" for key, value in random_entry.items()])
|
49 |
return output
|
50 |
|
51 |
# Gradio 界面
|
52 |
with gr.Blocks() as app:
|
53 |
+
gr.Markdown("# Hugging Face 数据集查看器")
|
54 |
+
gr.Markdown("选择一个 Hugging Face 数据集,随机展示其中一条数据。点击按钮可以重新采样。")
|
55 |
|
56 |
with gr.Row():
|
57 |
+
dataset_name = gr.Textbox(label="输入 Hugging Face 数据集名称", placeholder="例如: imdb")
|
58 |
sample_button = gr.Button("再采样")
|
59 |
|
60 |
output_box = gr.Textbox(label="随机数据", lines=10, max_lines=20)
|
|
|
62 |
# 用于存储加载后的数据
|
63 |
state_data = gr.State()
|
64 |
|
65 |
+
# 绑定事件:输入数据集名称后随机取一条数据
|
66 |
+
dataset_name.change(random_data_viewer, inputs=dataset_name, outputs=[output_box, state_data])
|
67 |
|
68 |
# 绑定事件:点击按钮后从已有数据中再采样
|
69 |
sample_button.click(sample_more, inputs=state_data, outputs=output_box)
|