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