feat: 支持上传换行分割的.txt文件

This commit is contained in:
maxmon 2023-04-16 17:45:16 +08:00
parent e0beff9fc7
commit dec3115831
2 changed files with 34 additions and 1 deletions

View File

@ -102,6 +102,37 @@ def get_jsonl_data():
return json.dumps(ret_info, default=lambda o: o.__dict__)
@api.route('/get_txt_data', methods=['POST'])
def get_txt_data():
ret_info = ReturnInfo()
try:
project_name = request.form.get('projectName')
upload_file = request.files['file']
file_path = os.path.join(PROJECT_PATH.format(project_name), upload_file.filename)
file_name = upload_file.filename.replace('.txt', '')
target_path = PROJECT_PATH.format(project_name) + '/data'
upload_file.save(file_path)
with open(file_path, 'r', encoding='utf-8') as fh:
l = fh.readline().strip()
idx = 0
while l:
idx += 1
_idx = ('000000'+str(idx))[-6:]
if len(str(idx)) > 6:
_idx = str(idx)
open(f'{target_path}/{file_name}_{_idx}', 'w', encoding='utf-8').write(l)
l = fh.readline().strip()
os.remove(file_path)
except Exception as e:
print(e)
ret_info.errCode = 404
ret_info.errMsg = str(e)
return json.dumps(ret_info, default=lambda o: o.__dict__)
@api.route('/create', methods=['POST'])
def create_project():

View File

@ -53,7 +53,7 @@
</div>
<p>上传文本</p>
<p style="font-size:10px">请选择包含文本文件的ziptar文件</p>
<input type="file" id="file-input" accept=".zip,.tar,.jsonl"/>
<input type="file" id="file-input" accept=".zip,.tar,.jsonl,.txt"/>
<p class="edit-box-btn-area">
<button class="button danger" @click="del" v-if="page==='edit'">删除</button>
<button class="button" @click="submit">提交</button>
@ -178,6 +178,8 @@ export default {
form('/v1/project/get_zipped_data', formData)
} else if (file.name.endsWith('.jsonl')) {
form('/v1/project/get_jsonl_data', formData)
} else if (file.name.endsWith('.txt')) {
form('/v1/project/get_txt_data', formData)
}
}
})