stzhao commited on
Commit
aeb88e5
·
verified ·
1 Parent(s): 52a6230

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -10
app.py CHANGED
@@ -10,26 +10,50 @@ def load_jsonl(file):
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文件!"
17
 
18
- data = load_jsonl(file)
19
- if len(data) == 0:
20
- return "文件为空或格式不正确!"
21
 
22
- random_entry = random.choice(data)
 
 
 
23
  output = "\n".join([f"{key}: {value}" for key, value in random_entry.items()])
24
- return output
25
 
26
  # 创建Gradio界面
27
  iface = gr.Interface(
28
  fn=random_data_viewer,
29
- inputs=gr.File(file_types=[".jsonl"], label="上传JSONL文件"),
30
- outputs=gr.Markdown(label="随机数据", render=True),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  title="JSONL 数据查看器",
32
- description="上传一个JSONL文件,随机展示其中一条数据。"
 
33
  )
34
 
35
  # 启动Gradio应用
 
10
  data.append(json.loads(line))
11
  return data
12
 
13
+ def random_data_viewer(file, state):
14
  """随机抽取一条数据并格式化输出"""
15
  if file is None:
16
+ return "请上传一个jsonl文件!", state
17
 
18
+ if state is None:
19
+ state = load_jsonl(file)
 
20
 
21
+ if len(state) == 0:
22
+ return "文件为空或格式不正确!", state
23
+
24
+ random_entry = random.choice(state)
25
  output = "\n".join([f"{key}: {value}" for key, value in random_entry.items()])
26
+ return output, state
27
 
28
  # 创建Gradio界面
29
  iface = gr.Interface(
30
  fn=random_data_viewer,
31
+ inputs=[
32
+ gr.File(file_types=[".jsonl"], label="上传JSONL文件"),
33
+ gr.State() # 用于存储加载的数据
34
+ ],
35
+ outputs=[
36
+ gr.Markdown(label="随机数据", render=True),
37
+ gr.State() # 用于保持数据状态
38
+ ],
39
+ title="JSONL 数据查看器",
40
+ description="上传一个JSONL文件,随机展示其中一条数据。点击按钮重新采样。"
41
+ )
42
+
43
+ # 添加一个按钮用于重新采样
44
+ iface = gr.Interface(
45
+ fn=random_data_viewer,
46
+ inputs=[
47
+ gr.File(file_types=[".jsonl"], label="上传JSONL文件"),
48
+ gr.State() # 用于存储加载的数据
49
+ ],
50
+ outputs=[
51
+ gr.Markdown(label="随机数据", render=True),
52
+ gr.State() # 用于保持数据状态
53
+ ],
54
  title="JSONL 数据查看器",
55
+ description="上传一个JSONL文件,随机展示其中一条数据。点击按钮重新采样。",
56
+ live=True # 允许实时更新
57
  )
58
 
59
  # 启动Gradio应用