Spaces:
Sleeping
Sleeping
File size: 727 Bytes
3a47676 4f30708 3a47676 4f30708 3a47676 4f30708 3a47676 4f30708 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import gradio as gr
import spacy
import os
import uuid
# 自动创建保存目录
os.makedirs("saved", exist_ok=True)
# 加载模型(初始化只加载一次)
nlp = spacy.load("en_core_web_md")
def process_text(input_text):
doc = nlp(input_text)
# 保存为 JSON
json_path = f"saved/{uuid.uuid4().hex}.json"
with open(json_path, "w") as f:
f.write(doc.to_json(indent=2))
# 返回下载链接
return json_path
# Gradio 接口
demo = gr.Interface(
fn=process_text,
inputs=gr.Textbox(label="输入文本"),
outputs=gr.File(label="下载文件"),
title="📦 文本处理 → 下载 JSON",
allow_flagging="never"
)
if __name__ == "__main__":
demo.launch()
|