stzhao commited on
Commit
7282a5f
·
verified ·
1 Parent(s): b19e7f6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import random
4
+
5
+ def load_jsonl(file):
6
+ """读取上传的jsonl文件并解析为字典列表"""
7
+ data = []
8
+ for line in file:
9
+ data.append(json.loads(line.decode('utf-8')))
10
+ return data
11
+
12
+ def random_data_viewer(file):
13
+ """随机抽取一条数据并格式化输出"""
14
+ if file is None:
15
+ return "请上传一个jsonl文件!"
16
+
17
+ data = load_jsonl(file)
18
+ if len(data) == 0:
19
+ return "文件为空或格式不正确!"
20
+
21
+ random_entry = random.choice(data)
22
+ output = "\n".join([f"{key}: {value}" for key, value in random_entry.items()])
23
+ return output
24
+
25
+ # 创建Gradio界面
26
+ iface = gr.Interface(
27
+ fn=random_data_viewer,
28
+ inputs=gr.File(file_types=[".jsonl"], label="上传JSONL文件"),
29
+ outputs=gr.Textbox(label="随机数据"),
30
+ title="JSONL 数据查看器",
31
+ description="上传一个JSONL文件,随机展示其中一条数据。"
32
+ )
33
+
34
+ # 启动Gradio应用
35
+ iface.launch()