Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,117 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import time
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
try:
|
| 12 |
API_URL = "https://api-inference.huggingface.co/models/mzcwd/ProtTeX"
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
payload = {
|
| 15 |
"inputs": instruction,
|
| 16 |
"parameters": {
|
| 17 |
-
"
|
| 18 |
"temperature": 0.8,
|
| 19 |
"do_sample": True,
|
| 20 |
"return_full_text": False
|
| 21 |
-
},
|
| 22 |
-
"options": {
|
| 23 |
-
"wait_for_model": True
|
| 24 |
}
|
| 25 |
}
|
| 26 |
|
| 27 |
-
|
| 28 |
-
response = requests.post(API_URL, json=payload)
|
| 29 |
|
| 30 |
if response.status_code == 200:
|
| 31 |
result = response.json()
|
| 32 |
if isinstance(result, list) and len(result) > 0:
|
| 33 |
-
generated_text = result[0].get('generated_text', '')
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
yield f"响应格式异常: {result}"
|
| 37 |
-
elif response.status_code == 503:
|
| 38 |
-
# 模型正在加载,等待后重试
|
| 39 |
-
yield "模型正在启动中,请等待约30秒后重试..."
|
| 40 |
else:
|
| 41 |
-
error_msg = f"请求失败 (状态码: {response.status_code})"
|
| 42 |
-
if response.
|
| 43 |
-
error_msg += "\n
|
| 44 |
-
|
| 45 |
|
|
|
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# 创建界面
|
| 50 |
with gr.Blocks(
|
|
@@ -52,7 +119,8 @@ with gr.Blocks(
|
|
| 52 |
theme=gr.themes.Soft(),
|
| 53 |
css="""
|
| 54 |
.gradio-container {
|
| 55 |
-
max-width:
|
|
|
|
| 56 |
}
|
| 57 |
"""
|
| 58 |
) as demo:
|
|
@@ -60,16 +128,18 @@ with gr.Blocks(
|
|
| 60 |
gr.Markdown("""
|
| 61 |
# 🧬 ProtTeX 蛋白质生成器
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
""")
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
with gr.Column(scale=1):
|
| 68 |
instruction = gr.Textbox(
|
| 69 |
-
label="蛋白质生成指令",
|
| 70 |
placeholder="例如:Generate a protein with alpha-helical structure for membrane binding",
|
| 71 |
lines=3,
|
| 72 |
-
|
| 73 |
)
|
| 74 |
|
| 75 |
max_length = gr.Slider(
|
|
@@ -77,7 +147,7 @@ with gr.Blocks(
|
|
| 77 |
maximum=300,
|
| 78 |
value=150,
|
| 79 |
step=10,
|
| 80 |
-
label="序列最大长度"
|
| 81 |
)
|
| 82 |
|
| 83 |
generate_btn = gr.Button(
|
|
@@ -88,14 +158,14 @@ with gr.Blocks(
|
|
| 88 |
|
| 89 |
with gr.Column(scale=1):
|
| 90 |
output = gr.Textbox(
|
| 91 |
-
label="生成的蛋白质序列",
|
| 92 |
-
lines=
|
| 93 |
show_copy_button=True,
|
| 94 |
-
|
| 95 |
)
|
| 96 |
|
| 97 |
# 示例部分
|
| 98 |
-
gr.Markdown("### 💡
|
| 99 |
|
| 100 |
examples = gr.Examples(
|
| 101 |
examples=[
|
|
@@ -105,10 +175,7 @@ with gr.Blocks(
|
|
| 105 |
["Generate a stable protein for high temperature environments"]
|
| 106 |
],
|
| 107 |
inputs=[instruction],
|
| 108 |
-
|
| 109 |
-
fn=generate_protein,
|
| 110 |
-
cache_examples=False,
|
| 111 |
-
label="点击示例快速尝试"
|
| 112 |
)
|
| 113 |
|
| 114 |
# 连接按钮事件
|
|
@@ -118,14 +185,20 @@ with gr.Blocks(
|
|
| 118 |
outputs=[output]
|
| 119 |
)
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
gr.Markdown("""
|
| 123 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
*基于 [mzcwd/ProtTeX](https://huggingface.co/mzcwd/ProtTeX) 模型*
|
| 125 |
""")
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
demo.launch(
|
| 129 |
server_name="0.0.0.0",
|
| 130 |
-
share=False
|
|
|
|
| 131 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import time
|
| 4 |
+
import os
|
| 5 |
+
from transformers import pipeline, set_seed
|
| 6 |
|
| 7 |
+
# 设置随机种子
|
| 8 |
+
set_seed(42)
|
| 9 |
+
|
| 10 |
+
# 从环境变量获取 token(现在应该可以正常工作了)
|
| 11 |
+
HF_TOKEN = os.environ.get('ProtTex')
|
| 12 |
+
print(f"Token 状态: {'已设置' if HF_TOKEN else '未设置'}")
|
| 13 |
+
|
| 14 |
+
# 初始化模型管道
|
| 15 |
+
try:
|
| 16 |
+
protein_generator = pipeline(
|
| 17 |
+
"text-generation",
|
| 18 |
+
model="mzcwd/ProtTeX",
|
| 19 |
+
device=-1, # 使用CPU
|
| 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,
|
| 37 |
+
max_length=max_length,
|
| 38 |
+
num_return_sequences=1,
|
| 39 |
+
temperature=0.8,
|
| 40 |
+
do_sample=True,
|
| 41 |
+
pad_token_id=50256
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if result and len(result) > 0:
|
| 45 |
+
return f"✅ 生成成功:\n\n{result[0]['generated_text']}"
|
| 46 |
+
else:
|
| 47 |
+
return "❌ 生成失败,未获得有效结果"
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"❌ 生成过程中出现错误: {str(e)}"
|
| 51 |
+
|
| 52 |
+
def generate_with_api(instruction, max_length=100):
|
| 53 |
+
"""使用HuggingFace API生成蛋白质序列"""
|
| 54 |
+
if not HF_TOKEN:
|
| 55 |
+
return "❌ 未设置 HuggingFace Token,无法使用 API 功能"
|
| 56 |
|
| 57 |
try:
|
| 58 |
API_URL = "https://api-inference.huggingface.co/models/mzcwd/ProtTeX"
|
| 59 |
|
| 60 |
+
headers = {
|
| 61 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 62 |
+
"Content-Type": "application/json"
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
payload = {
|
| 66 |
"inputs": instruction,
|
| 67 |
"parameters": {
|
| 68 |
+
"max_new_tokens": max_length,
|
| 69 |
"temperature": 0.8,
|
| 70 |
"do_sample": True,
|
| 71 |
"return_full_text": False
|
|
|
|
|
|
|
|
|
|
| 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:
|
| 79 |
result = response.json()
|
| 80 |
if isinstance(result, list) and len(result) > 0:
|
| 81 |
+
generated_text = result[0].get('generated_text', '生成成功但无内容返回')
|
| 82 |
+
return f"✅ API 生成成功:\n\n{generated_text}"
|
| 83 |
+
return f"✅ API 响应: {str(result)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
+
error_msg = f"❌ API请求失败 (状态码: {response.status_code})"
|
| 86 |
+
if response.text:
|
| 87 |
+
error_msg += f"\n详细信息: {response.text}"
|
| 88 |
+
return error_msg
|
| 89 |
|
| 90 |
+
except requests.exceptions.Timeout:
|
| 91 |
+
return "⏰ 请求超时,请稍后重试"
|
| 92 |
except Exception as e:
|
| 93 |
+
return f"❌ API调用错误: {str(e)}"
|
| 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(
|
|
|
|
| 119 |
theme=gr.themes.Soft(),
|
| 120 |
css="""
|
| 121 |
.gradio-container {
|
| 122 |
+
max-width: 900px;
|
| 123 |
+
margin: auto;
|
| 124 |
}
|
| 125 |
"""
|
| 126 |
) as demo:
|
|
|
|
| 128 |
gr.Markdown("""
|
| 129 |
# 🧬 ProtTeX 蛋白质生成器
|
| 130 |
|
| 131 |
+
**使用自然语言指令生成蛋白质序列**
|
| 132 |
+
|
| 133 |
+
*当前模式: {'🧠 本地模型' if MODEL_LOADED else '🌐 API 调用'} | Token: {'✅ 已设置' if HF_TOKEN else '❌ 未设置'}*
|
| 134 |
""")
|
| 135 |
|
| 136 |
with gr.Row():
|
| 137 |
with gr.Column(scale=1):
|
| 138 |
instruction = gr.Textbox(
|
| 139 |
+
label="🧪 蛋白质生成指令",
|
| 140 |
placeholder="例如:Generate a protein with alpha-helical structure for membrane binding",
|
| 141 |
lines=3,
|
| 142 |
+
value="Generate a hydrophobic transmembrane protein sequence"
|
| 143 |
)
|
| 144 |
|
| 145 |
max_length = gr.Slider(
|
|
|
|
| 147 |
maximum=300,
|
| 148 |
value=150,
|
| 149 |
step=10,
|
| 150 |
+
label="📏 序列最大长度"
|
| 151 |
)
|
| 152 |
|
| 153 |
generate_btn = gr.Button(
|
|
|
|
| 158 |
|
| 159 |
with gr.Column(scale=1):
|
| 160 |
output = gr.Textbox(
|
| 161 |
+
label="🧬 生成的蛋白质序列",
|
| 162 |
+
lines=8,
|
| 163 |
show_copy_button=True,
|
| 164 |
+
elem_id="output-box"
|
| 165 |
)
|
| 166 |
|
| 167 |
# 示例部分
|
| 168 |
+
gr.Markdown("### 💡 示例指令(点击试用)")
|
| 169 |
|
| 170 |
examples = gr.Examples(
|
| 171 |
examples=[
|
|
|
|
| 175 |
["Generate a stable protein for high temperature environments"]
|
| 176 |
],
|
| 177 |
inputs=[instruction],
|
| 178 |
+
label="点击下面的示例快速尝试"
|
|
|
|
|
|
|
|
|
|
| 179 |
)
|
| 180 |
|
| 181 |
# 连接按钮事件
|
|
|
|
| 185 |
outputs=[output]
|
| 186 |
)
|
| 187 |
|
| 188 |
+
# 状态信息
|
| 189 |
+
gr.Markdown(f"""
|
| 190 |
---
|
| 191 |
+
**系统状态**:
|
| 192 |
+
- 运行模式: {'🧠 本地模型' if MODEL_LOADED else '🌐 API 调用'}
|
| 193 |
+
- Token 状态: {'✅ 已设置' if HF_TOKEN else '❌ 未设置'}
|
| 194 |
+
- 硬件: CPU Basic
|
| 195 |
+
|
| 196 |
*基于 [mzcwd/ProtTeX](https://huggingface.co/mzcwd/ProtTeX) 模型*
|
| 197 |
""")
|
| 198 |
|
| 199 |
if __name__ == "__main__":
|
| 200 |
demo.launch(
|
| 201 |
server_name="0.0.0.0",
|
| 202 |
+
share=False,
|
| 203 |
+
show_error=True
|
| 204 |
)
|