Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 62 |
-
raise gr.Error("模型未能加载或初始化失败,请检查后台日志。")
|
| 63 |
|
| 64 |
if not sequence_input or not isinstance(sequence_input, str):
|
| 65 |
-
|
| 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 |
-
#
|
| 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(
|