stzhao commited on
Commit
4d6022a
·
verified ·
1 Parent(s): f6f42c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -23
app.py CHANGED
@@ -1,46 +1,60 @@
1
  import gradio as gr
2
  import json
3
  import random
 
4
 
5
- def load_jsonl(file):
6
- """读取上传的jsonl文件并解析为字典列表"""
7
- data = []
8
- with open(file, "r", encoding="utf-8") as f:
9
- for line in f:
10
- data.append(json.loads(line))
11
- return data
12
 
13
- def random_data_viewer(file):
14
- """读取文件并随机抽取一条数据"""
15
- if file is None:
16
- return "请上传一个JSONL文件!", None
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- data = load_jsonl(file)
19
  if len(data) == 0:
20
- return "文件为空或格式不正确!", None
21
 
22
- random_entry = random.choice(data)
23
- # 格式化输出为Markdown
 
 
 
 
 
24
  output = "\n".join([f"**{key}**: {value}" for key, value in random_entry.items()])
25
- return output, data # 返回数据以存储供后续使用
26
 
27
  def sample_more(data):
28
  """从已有数据中再采样一条"""
29
  if not data:
30
- return "没有可用数据,请先上传JSONL文件!"
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("# JSONL 数据查看器")
40
- gr.Markdown("上传一个JSONL文件,随机展示其中一条数据。点击按钮可以重新采样。")
41
 
42
  with gr.Row():
43
- file_upload = gr.File(file_types=[".jsonl"], label="上传JSONL文件")
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
- file_upload.change(random_data_viewer, inputs=file_upload, outputs=[output_box, state_data])
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)