Shunfeng Zheng commited on
Commit
4f30708
·
verified ·
1 Parent(s): ce041ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -16
app.py CHANGED
@@ -1,20 +1,33 @@
1
  import gradio as gr
2
- import time # 模拟处理耗时
 
 
3
 
4
- def process_api(input_text):
5
- # 这里编写实际的后端处理逻辑
6
- time.sleep(1) # 模拟处理延迟
7
- return {
8
- "status": "success",
9
- "result": f"Processed: {input_text.upper()}",
10
- "timestamp": time.time()
11
- }
12
 
13
- # 设置API格式为JSON
14
- gr.Interface(
15
- fn=process_api,
16
- inputs="text",
17
- outputs="json",
18
- title="Backend API",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  allow_flagging="never"
20
- ).launch()
 
 
 
 
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()