Spaces:
Sleeping
Sleeping
Update app.py
Browse filesUpdate app.py with summarization functionality
app.py
CHANGED
@@ -1,7 +1,36 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import time
|
4 |
|
5 |
+
try:
|
6 |
+
client = InferenceClient(model="sshleifer/distilbart-cnn-12-6")
|
7 |
+
def generate_summary(text):
|
8 |
+
prompt = f"以正式语气总结:{text}"
|
9 |
+
for _ in range(3): # 重试3次
|
10 |
+
try:
|
11 |
+
response = client.text_generation(prompt, max_length=60)
|
12 |
+
return response
|
13 |
+
except Exception:
|
14 |
+
time.sleep(1)
|
15 |
+
return "网络错误,请稍后重试。"
|
16 |
+
interface = gr.Interface(
|
17 |
+
fn=generate_summary,
|
18 |
+
inputs=gr.Textbox(lines=5, placeholder="输入文档内容..."),
|
19 |
+
outputs="text",
|
20 |
+
title="MySmartSummary",
|
21 |
+
description="在线智能文档摘要工具,支持中文",
|
22 |
+
examples=[
|
23 |
+
["今天我们讨论了2025年的项目计划,包括产品发布、市场推广和预算分配。"]
|
24 |
+
],
|
25 |
+
css="body {background-color: #f0f0f0; font-family: Arial;}"
|
26 |
+
)
|
27 |
+
except Exception as e:
|
28 |
+
print(f"初始化错误: {e}")
|
29 |
+
interface = gr.Interface(
|
30 |
+
fn=lambda x: f"服务暂不可用,错误: {e}",
|
31 |
+
inputs="text",
|
32 |
+
outputs="text",
|
33 |
+
title="MySmartSummary",
|
34 |
+
description="服务初始化失败"
|
35 |
+
)
|
36 |
+
interface.launch()
|