chenkw1029 commited on
Commit
4b3c4db
·
verified ·
1 Parent(s): f12cdc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -20
app.py CHANGED
@@ -9,7 +9,7 @@ set_seed(42)
9
 
10
  # 从环境变量获取 token
11
  HF_TOKEN = os.environ.get('HF_TOKEN')
12
- print(f"Token 状态: {'已设置' if HF_TOKEN else '未设置'}")
13
 
14
  # 初始化模型管道
15
  try:
@@ -20,17 +20,22 @@ try:
20
  torch_dtype="auto"
21
  )
22
  MODEL_LOADED = True
23
- print("✅ 本地模型加载成功")
 
24
  except Exception as e:
25
  print(f"❌ 本地模型加载失败: {e}")
26
  MODEL_LOADED = False
27
  protein_generator = None
 
 
 
 
 
 
 
28
 
29
  def generate_with_local_model(instruction, max_length=100):
30
  """使用本地加载的模型生成蛋白质序列"""
31
- if not MODEL_LOADED or protein_generator is None:
32
- return "❌ 模型未正确加载,请检查控制台日志"
33
-
34
  try:
35
  result = protein_generator(
36
  instruction,
@@ -72,7 +77,6 @@ def generate_with_api(instruction, max_length=100):
72
  }
73
  }
74
 
75
- print("🔄 正在调用 API...")
76
  response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
77
 
78
  if response.status_code == 200:
@@ -94,34 +98,33 @@ def generate_with_api(instruction, max_length=100):
94
 
95
  def generate_protein(instruction, max_length=100):
96
  """主生成函数"""
97
- yield "🔄 正在处理您的请求..."
 
 
 
98
  time.sleep(0.5)
99
 
100
  try:
101
- # 显示当前模式
102
- mode = "本地模型" if MODEL_LOADED else "API调用"
103
- yield f"🔄 使用 {mode} 处理中..."
104
-
105
  # 优先使用本地模型,失败时使用API
106
  if MODEL_LOADED:
107
  result = generate_with_local_model(instruction, max_length)
108
  else:
109
  result = generate_with_api(instruction, max_length)
110
 
111
- yield result
112
 
113
  except Exception as e:
114
- yield f"❌ 生成过程中出现未预期错误: {str(e)}"
115
 
116
- # 创建界面 - 移除了不兼容的参数
117
  with gr.Blocks(title="ProtTeX 蛋白质生成器") as demo:
118
 
119
- gr.Markdown("""
120
  # 🧬 ProtTeX 蛋白质生成器
121
 
122
  **使用自然语言指令生成蛋白质序列**
123
 
124
- *当前模式: {'🧠 本地模型' if MODEL_LOADED else '🌐 API 调用'} | Token: {'✅ 已设置' if HF_TOKEN else '❌ 未设置'}*
125
  """)
126
 
127
  with gr.Row():
@@ -150,7 +153,6 @@ with gr.Blocks(title="ProtTeX 蛋白质生成器") as demo:
150
  output = gr.Textbox(
151
  label="🧬 生成的蛋白质序列",
152
  lines=8
153
- # 移除了 show_copy_button 参数
154
  )
155
 
156
  # 示例部分
@@ -163,7 +165,10 @@ with gr.Blocks(title="ProtTeX 蛋白质生成器") as demo:
163
  ["Design a protein with enzymatic activity for hydrolysis"],
164
  ["Generate a stable protein for high temperature environments"]
165
  ],
166
- inputs=[instruction]
 
 
 
167
  )
168
 
169
  # 连接按钮事件
@@ -177,8 +182,9 @@ with gr.Blocks(title="ProtTeX 蛋白质生成器") as demo:
177
  gr.Markdown(f"""
178
  ---
179
  **系统状态**:
180
- - 运行模式: {'🧠 本地模型' if MODEL_LOADED else '🌐 API 调用'}
181
- - Token 状态: {'✅ 已设置' if HF_TOKEN else '❌ 未设置'}
 
182
  - 硬件: CPU Basic
183
 
184
  *基于 [mzcwd/ProtTeX](https://huggingface.co/mzcwd/ProtTeX) 模型*
 
9
 
10
  # 从环境变量获取 token
11
  HF_TOKEN = os.environ.get('HF_TOKEN')
12
+ token_status = " 已设置" if HF_TOKEN else "❌ 未设置"
13
 
14
  # 初始化模型管道
15
  try:
 
20
  torch_dtype="auto"
21
  )
22
  MODEL_LOADED = True
23
+ model_status = "✅ 本地模型加载成功"
24
+ print(model_status)
25
  except Exception as e:
26
  print(f"❌ 本地模型加载失败: {e}")
27
  MODEL_LOADED = False
28
  protein_generator = None
29
+ model_status = "❌ 本地模型加载失败,使用API模式"
30
+
31
+ # 确定运行模式
32
+ if MODEL_LOADED:
33
+ run_mode = "🧠 本地模型"
34
+ else:
35
+ run_mode = "🌐 API调用"
36
 
37
  def generate_with_local_model(instruction, max_length=100):
38
  """使用本地加载的模型生成蛋白质序列"""
 
 
 
39
  try:
40
  result = protein_generator(
41
  instruction,
 
77
  }
78
  }
79
 
 
80
  response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
81
 
82
  if response.status_code == 200:
 
98
 
99
  def generate_protein(instruction, max_length=100):
100
  """主生成函数"""
101
+ if not instruction or instruction.strip() == "":
102
+ return "❌ 请输入有效的蛋白质生成指令"
103
+
104
+ # 显示处理状态
105
  time.sleep(0.5)
106
 
107
  try:
 
 
 
 
108
  # 优先使用本地模型,失败时使用API
109
  if MODEL_LOADED:
110
  result = generate_with_local_model(instruction, max_length)
111
  else:
112
  result = generate_with_api(instruction, max_length)
113
 
114
+ return result
115
 
116
  except Exception as e:
117
+ return f"❌ 生成过程中出现未预期错误: {str(e)}"
118
 
119
+ # 创建界面
120
  with gr.Blocks(title="ProtTeX 蛋白质生成器") as demo:
121
 
122
+ gr.Markdown(f"""
123
  # 🧬 ProtTeX 蛋白质生成器
124
 
125
  **使用自然语言指令生成蛋白质序列**
126
 
127
+ *当前模式: {run_mode} | Token状态: {token_status}*
128
  """)
129
 
130
  with gr.Row():
 
153
  output = gr.Textbox(
154
  label="🧬 生成的蛋白质序列",
155
  lines=8
 
156
  )
157
 
158
  # 示例部分
 
165
  ["Design a protein with enzymatic activity for hydrolysis"],
166
  ["Generate a stable protein for high temperature environments"]
167
  ],
168
+ inputs=[instruction],
169
+ outputs=[output],
170
+ fn=generate_protein,
171
+ cache_examples=False
172
  )
173
 
174
  # 连接按钮事件
 
182
  gr.Markdown(f"""
183
  ---
184
  **系统状态**:
185
+ - 运行模式: {run_mode}
186
+ - Token状态: {token_status}
187
+ - 模型状态: {model_status}
188
  - 硬件: CPU Basic
189
 
190
  *基于 [mzcwd/ProtTeX](https://huggingface.co/mzcwd/ProtTeX) 模型*