Xianfish9 commited on
Commit
4202b2a
·
verified ·
1 Parent(s): ed99039

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -9
app.py CHANGED
@@ -58,22 +58,20 @@ def extract_features_from_seq(sequence_list):
58
  # --- 4. 核心预测函数 ---
59
  def predict(sequence_input):
60
  if model is None:
61
- # 抛出 Gradio 错误,而不是返回字典
62
- raise gr.Error("模型未能加载或初始化失败,请检查后台日志。")
63
 
64
  if not sequence_input or not isinstance(sequence_input, str):
65
- # 抛出 Gradio 错误
66
- raise gr.Error("请输入有效的生物序列。")
67
 
68
  cleaned_sequence = sequence_input.strip().upper()
69
  sequence_list = [cleaned_sequence]
70
 
71
  try:
 
72
  x1_np, x2_np = extract_features_from_seq(sequence_list)
73
  except Exception as e:
74
- # 在特征提取失败时,抛出包含具体异常信息的 Gradio 错误
75
- # 这样用户就能在界面上看到具体的失败原因
76
- raise gr.Error(f"特征提取失败: {e}")
77
 
78
  tensor_x1 = torch.tensor(x1_np).to(device)
79
  tensor_x2 = torch.tensor(x2_np).to(device)
@@ -86,10 +84,9 @@ def predict(sequence_input):
86
  labels = ["类别 A (a)", "类别 C (c)", "类别 M (m)", "类别 S (s)"]
87
  result = {label: float(prob) for label, prob in zip(labels, probabilities)}
88
 
89
- # 只有在成功时,才返回符合格式的字典
90
  return result
91
 
92
- # --- 5. 创建并启动 Gradio 界面 (无需改动) ---
93
  demo = gr.Interface(
94
  fn=predict,
95
  inputs=gr.Textbox(
 
58
  # --- 4. 核心预测函数 ---
59
  def predict(sequence_input):
60
  if model is None:
61
+ return {"错误": "模型未能加载或初始化失败,请检查后台日志"}
 
62
 
63
  if not sequence_input or not isinstance(sequence_input, str):
64
+ return {"错误": "请输入有效的生物序列"}
 
65
 
66
  cleaned_sequence = sequence_input.strip().upper()
67
  sequence_list = [cleaned_sequence]
68
 
69
  try:
70
+ # !!! 在这里调用了上面的函数 !!!
71
  x1_np, x2_np = extract_features_from_seq(sequence_list)
72
  except Exception as e:
73
+ # 如果特征提取失败(包括 NameError),会在这里捕获
74
+ return {f"特征提取失败": str(e)}
 
75
 
76
  tensor_x1 = torch.tensor(x1_np).to(device)
77
  tensor_x2 = torch.tensor(x2_np).to(device)
 
84
  labels = ["类别 A (a)", "类别 C (c)", "类别 M (m)", "类别 S (s)"]
85
  result = {label: float(prob) for label, prob in zip(labels, probabilities)}
86
 
 
87
  return result
88
 
89
+ # --- 5. 创建并启动 Gradio 界面 ---
90
  demo = gr.Interface(
91
  fn=predict,
92
  inputs=gr.Textbox(