jinv2 commited on
Commit
4a16b87
·
verified ·
1 Parent(s): e31f82b

Update app.py

Browse files

Add traceback for detailed error logging

Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -2,19 +2,24 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import time
4
  import os
 
5
 
6
- # 从Secrets中读取HF_TOKEN
7
  HF_TOKEN = os.getenv("HF_TOKEN")
 
 
 
8
 
9
  try:
10
  client = InferenceClient(model="sshleifer/distilbart-cnn-12-6", token=HF_TOKEN)
11
  def generate_summary(text):
12
- for _ in range(3): # 重试3次
13
  try:
14
  response = client.summarization(text)
 
15
  return response[0]['summary_text'] if response and 'summary_text' in response[0] else "摘要生成失败,请重试。"
16
  except Exception as e:
17
- print(f"尝试失败: {e}")
 
18
  time.sleep(1)
19
  return "网络错误,请稍后重试。"
20
  interface = gr.Interface(
@@ -29,9 +34,10 @@ try:
29
  css="body {background-color: #f0f0f0; font-family: Arial;}"
30
  )
31
  except Exception as e:
32
- print(f"初始化错误: {e}")
 
33
  interface = gr.Interface(
34
- fn=lambda x: f"服务暂不可用,错误: {e}",
35
  inputs="text",
36
  outputs="text",
37
  title="MySmartSummary",
 
2
  from huggingface_hub import InferenceClient
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="sshleifer/distilbart-cnn-12-6", token=HF_TOKEN)
14
  def generate_summary(text):
15
+ for _ in range(3):
16
  try:
17
  response = client.summarization(text)
18
+ print(f"API Response: {response}")
19
  return response[0]['summary_text'] if response and 'summary_text' in response[0] else "摘要生成失败,请重试。"
20
  except Exception as e:
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(
 
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",