dockerb / app.py
Shunfeng Zheng
Update app.py
4f30708 verified
raw
history blame contribute delete
727 Bytes
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()