Spaces:
Runtime error
Runtime error
Update app.py
Browse filesRevert to InferenceClient to avoid local model loading
2 unmodified lines345678910111213141516171819202122232425262728293031323334353637383 unmodified lines4243444546474849505152535455565758596061622 unmodified linesimport timeimport osimport tracebackfrom transformers import MBartForConditionalGeneration, MBartTokenizerHF_TOKEN = os.getenv("HF_TOKEN")if not HF_TOKEN:raise ValueError("HF_TOKEN not found in Secrets. Please set it in Space settings.")print(f"HF_TOKEN loaded: {len(HF_TOKEN) if HF_TOKEN else 0} characters")# 尝试初始化 Hugging Face API 客户端client = Nonetry:client = InferenceClient(model="facebook/mbart-large-50", token=HF_TOKEN)response = client.get_model_info()if not response:raise ConnectionError("无法连接到 Hugging Face API")print("Hugging Face API 连接成功")except Exception as e:print(f"Hugging Face API 初始化失败,错误: {e},将使用本地推理。")# 本地推理备用方案def local_generate_summary(text):model_name = "facebook/mbart-large-50"tokenizer = MBartTokenizer.from_pretrained(model_name)model = MBartForConditionalGeneration.from_pretrained(model_name)inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)summary_ids = model.generate(inputs.input_ids, max_length=200, min_length=50)return tokenizer.decode(summary_ids[0], skip_special_tokens=True)def generate_summary(text):if client:for _ in range(3):try:response = client.summarization(text)3 unmodified lineserror_details = traceback.format_exc()print(f"尝试失败,错误类型: {type(e).__name__}, 错误详情: {str(e)}, 堆栈: {error_details}")time.sleep(1)print("API 不可用,切换到本地推理。")# 如果 API 不可用,使用本地推理return local_generate_summary(text)interface = gr.Interface(fn=generate_summary,inputs=gr.Textbox(lines=5, placeholder="输入文档内容..."),outputs="text",title="MySmartSummary",description="在线智能文档摘要工具,支持中文",examples=[["今天我们讨论了2025年的项目计划,包括产品发布、市场推广和预算分配。"]],css="body {background-color: #f0f0f0; font-family: Arial;}")interface.launch()2 unmodified lines345678910111213141516173 unmodified lines21222324252627282930313233343536373839404142434445462 unmodified linesimport timeimport osimport tracebackHF_TOKEN = os.getenv("HF_TOKEN")if not HF_TOKEN:raise ValueError("HF_TOKEN not found in Secrets. Please set it in Space settings.")print(f"HF_TOKEN loaded: {len(HF_TOKEN) if HF_TOKEN else 0} characters")try:client = InferenceClient(model="facebook/mbart-large-50", token=HF_TOKEN)def generate_summary(text):for _ in range(3):try:response = client.summarization(text)3 unmodified lineserror_details = traceback.format_exc()print(f"尝试失败,错误类型: {type(e).__name__}, 错误详情: {str(e)}, 堆栈: {error_details}")time.sleep(1)return "网络错误,请稍后重试。"interface = gr.Interface(fn=generate_summary,inputs=gr.Textbox(lines=5, placeholder="输入文档内容..."),outputs="text",title="MySmartSummary",description="在线智能文档摘要工具,支持中文",examples=[["今天我们讨论了2025年的项目计划,包括产品发布、市场推广和预算分配。"]],css="body {background-color: #f0f0f0; font-family: Arial;}")except Exception as e:error_details = traceback.format_exc()print(f"初始化错误,错误类型: {type(e).__name__}, 错误详情: {str(e)}, 堆栈: {error_details}")interface = gr.Interface(fn=lambda x: f"服务暂不可用,错误: {str(e)}",inputs="text",outputs="text",title="MySmartSummary",description="服务初始化失败")interface.launch()
app.py+24-40