Spaces:
Sleeping
Sleeping
Update app.py
Browse filesRevert to InferenceClient to avoid local model loading
app.py
CHANGED
|
@@ -3,36 +3,15 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
import time
|
| 4 |
import os
|
| 5 |
import traceback
|
| 6 |
-
from transformers import MBartForConditionalGeneration, MBartTokenizer
|
| 7 |
|
| 8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
if not HF_TOKEN:
|
| 10 |
raise ValueError("HF_TOKEN not found in Secrets. Please set it in Space settings.")
|
| 11 |
print(f"HF_TOKEN loaded: {len(HF_TOKEN) if HF_TOKEN else 0} characters")
|
| 12 |
|
| 13 |
-
# 尝试初始化 Hugging Face API 客户端
|
| 14 |
-
client = None
|
| 15 |
try:
|
| 16 |
client = InferenceClient(model="facebook/mbart-large-50", token=HF_TOKEN)
|
| 17 |
-
|
| 18 |
-
if not response:
|
| 19 |
-
raise ConnectionError("无法连接到 Hugging Face API")
|
| 20 |
-
print("Hugging Face API 连接成功")
|
| 21 |
-
except Exception as e:
|
| 22 |
-
print(f"Hugging Face API 初始化失败,错误: {e},将使用本地推理。")
|
| 23 |
-
|
| 24 |
-
# 本地推理备用方案
|
| 25 |
-
def local_generate_summary(text):
|
| 26 |
-
model_name = "facebook/mbart-large-50"
|
| 27 |
-
tokenizer = MBartTokenizer.from_pretrained(model_name)
|
| 28 |
-
model = MBartForConditionalGeneration.from_pretrained(model_name)
|
| 29 |
-
|
| 30 |
-
inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
|
| 31 |
-
summary_ids = model.generate(inputs.input_ids, max_length=200, min_length=50)
|
| 32 |
-
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 33 |
-
|
| 34 |
-
def generate_summary(text):
|
| 35 |
-
if client:
|
| 36 |
for _ in range(3):
|
| 37 |
try:
|
| 38 |
response = client.summarization(text)
|
|
@@ -42,21 +21,26 @@ def generate_summary(text):
|
|
| 42 |
error_details = traceback.format_exc()
|
| 43 |
print(f"尝试失败,错误类型: {type(e).__name__}, 错误详情: {str(e)}, 堆栈: {error_details}")
|
| 44 |
time.sleep(1)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import time
|
| 4 |
import os
|
| 5 |
import traceback
|
|
|
|
| 6 |
|
| 7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
if not HF_TOKEN:
|
| 9 |
raise ValueError("HF_TOKEN not found in Secrets. Please set it in Space settings.")
|
| 10 |
print(f"HF_TOKEN loaded: {len(HF_TOKEN) if HF_TOKEN else 0} characters")
|
| 11 |
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
client = InferenceClient(model="facebook/mbart-large-50", token=HF_TOKEN)
|
| 14 |
+
def generate_summary(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
for _ in range(3):
|
| 16 |
try:
|
| 17 |
response = client.summarization(text)
|
|
|
|
| 21 |
error_details = traceback.format_exc()
|
| 22 |
print(f"尝试失败,错误类型: {type(e).__name__}, 错误详情: {str(e)}, 堆栈: {error_details}")
|
| 23 |
time.sleep(1)
|
| 24 |
+
return "网络错误,请稍后重试。"
|
| 25 |
+
interface = gr.Interface(
|
| 26 |
+
fn=generate_summary,
|
| 27 |
+
inputs=gr.Textbox(lines=5, placeholder="输入文档内容..."),
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="MySmartSummary",
|
| 30 |
+
description="在线智能文档摘要工具,支持中文",
|
| 31 |
+
examples=[
|
| 32 |
+
["今天我们讨论了2025年的项目计划,包括产品发布、市场推广和预算分配。"]
|
| 33 |
+
],
|
| 34 |
+
css="body {background-color: #f0f0f0; font-family: Arial;}"
|
| 35 |
+
)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
error_details = traceback.format_exc()
|
| 38 |
+
print(f"初始化错误,错误类型: {type(e).__name__}, 错误详情: {str(e)}, 堆栈: {error_details}")
|
| 39 |
+
interface = gr.Interface(
|
| 40 |
+
fn=lambda x: f"服务暂不可用,错误: {str(e)}",
|
| 41 |
+
inputs="text",
|
| 42 |
+
outputs="text",
|
| 43 |
+
title="MySmartSummary",
|
| 44 |
+
description="服务初始化失败"
|
| 45 |
+
)
|
| 46 |
+
interface.launch()
|