File size: 5,994 Bytes
c747b91
 
7fe5ef7
c5fc9bb
 
c747b91
c5fc9bb
 
 
43e2c94
 
4b3c4db
c5fc9bb
 
 
 
 
 
 
 
 
 
4b3c4db
 
c5fc9bb
43e2c94
c5fc9bb
 
4b3c4db
 
 
 
 
 
 
c5fc9bb
 
 
 
 
 
 
 
 
 
 
 
 
 
43e2c94
c5fc9bb
43e2c94
c5fc9bb
 
43e2c94
c5fc9bb
 
 
 
43e2c94
c747b91
 
7fe5ef7
 
c5fc9bb
 
 
 
 
7fe5ef7
 
 
c5fc9bb
7fe5ef7
 
 
 
 
 
43e2c94
7fe5ef7
 
 
 
43e2c94
 
 
7fe5ef7
43e2c94
 
 
 
7fe5ef7
c5fc9bb
43e2c94
c747b91
43e2c94
c5fc9bb
 
 
4b3c4db
 
 
 
c5fc9bb
 
 
 
 
 
 
 
 
4b3c4db
c5fc9bb
 
4b3c4db
c747b91
4b3c4db
43e2c94
7fe5ef7
4b3c4db
7fe5ef7
 
43e2c94
 
4b3c4db
7fe5ef7
c747b91
 
43e2c94
c747b91
43e2c94
 
7fe5ef7
c5fc9bb
c747b91
7fe5ef7
c747b91
7fe5ef7
 
 
 
43e2c94
c747b91
7fe5ef7
43e2c94
 
 
 
 
 
c747b91
43e2c94
 
c747b91
 
43e2c94
 
 
 
7fe5ef7
 
 
43e2c94
 
7fe5ef7
4b3c4db
 
 
 
c747b91
 
43e2c94
c747b91
 
 
43e2c94
c747b91
43e2c94
 
 
 
 
4b3c4db
 
 
43e2c94
 
 
 
c747b91
 
43e2c94
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import gradio as gr
import requests
import time
import os
from transformers import pipeline, set_seed

# 设置随机种子
set_seed(42)

# 从环境变量获取 token
HF_TOKEN = os.environ.get('HF_TOKEN')
token_status = "✅ 已设置" if HF_TOKEN else "❌ 未设置"

# 初始化模型管道
try:
    protein_generator = pipeline(
        "text-generation", 
        model="mzcwd/ProtTeX",
        device=-1,  # 使用CPU
        torch_dtype="auto"
    )
    MODEL_LOADED = True
    model_status = "✅ 本地模型加载成功"
    print(model_status)
except Exception as e:
    print(f"❌ 本地模型加载失败: {e}")
    MODEL_LOADED = False
    protein_generator = None
    model_status = "❌ 本地模型加载失败,使用API模式"

# 确定运行模式
if MODEL_LOADED:
    run_mode = "🧠 本地模型"
else:
    run_mode = "🌐 API调用"

def generate_with_local_model(instruction, max_length=100):
    """使用本地加载的模型生成蛋白质序列"""
    try:
        result = protein_generator(
            instruction,
            max_length=max_length,
            num_return_sequences=1,
            temperature=0.8,
            do_sample=True,
            pad_token_id=50256
        )
        
        if result and len(result) > 0:
            return f"✅ 生成成功:\n\n{result[0]['generated_text']}"
        else:
            return "❌ 生成失败,未获得有效结果"
            
    except Exception as e:
        return f"❌ 生成过程中出现错误: {str(e)}"

def generate_with_api(instruction, max_length=100):
    """使用HuggingFace API生成蛋白质序列"""
    if not HF_TOKEN:
        return "❌ 未设置 HuggingFace Token,无法使用 API 功能"
    
    try:
        API_URL = "https://api-inference.huggingface.co/models/mzcwd/ProtTeX"
        
        headers = {
            "Authorization": f"Bearer {HF_TOKEN}",
            "Content-Type": "application/json"
        }
        
        payload = {
            "inputs": instruction,
            "parameters": {
                "max_new_tokens": max_length,
                "temperature": 0.8,
                "do_sample": True,
                "return_full_text": False
            }
        }
        
        response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
        
        if response.status_code == 200:
            result = response.json()
            if isinstance(result, list) and len(result) > 0:
                generated_text = result[0].get('generated_text', '生成成功但无内容返回')
                return f"✅ API 生成成功:\n\n{generated_text}"
            return f"✅ API 响应: {str(result)}"
        else:
            error_msg = f"❌ API请求失败 (状态码: {response.status_code})"
            if response.text:
                error_msg += f"\n详细信息: {response.text}"
            return error_msg
            
    except requests.exceptions.Timeout:
        return "⏰ 请求超时,请稍后重试"
    except Exception as e:
        return f"❌ API调用错误: {str(e)}"

def generate_protein(instruction, max_length=100):
    """主生成函数"""
    if not instruction or instruction.strip() == "":
        return "❌ 请输入有效的蛋白质生成指令"
    
    # 显示处理状态
    time.sleep(0.5)
    
    try:
        # 优先使用本地模型,失败时使用API
        if MODEL_LOADED:
            result = generate_with_local_model(instruction, max_length)
        else:
            result = generate_with_api(instruction, max_length)
        
        return result
        
    except Exception as e:
        return f"❌ 生成过程中出现未预期错误: {str(e)}"

# 创建界面
with gr.Blocks(title="ProtTeX 蛋白质生成器") as demo:
    
    gr.Markdown(f"""
    # 🧬 ProtTeX 蛋白质生成器
    
    **使用自然语言指令生成蛋白质序列**
    
    *当前模式: {run_mode} | Token状态: {token_status}*
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            instruction = gr.Textbox(
                label="🧪 蛋白质生成指令",
                placeholder="例如:Generate a protein with alpha-helical structure for membrane binding",
                lines=3,
                value="Generate a hydrophobic transmembrane protein sequence"
            )
            
            max_length = gr.Slider(
                minimum=50,
                maximum=300,
                value=150,
                step=10,
                label="📏 序列最大长度"
            )
            
            generate_btn = gr.Button(
                "🚀 生成蛋白质序列",
                variant="primary"
            )
            
        with gr.Column(scale=1):
            output = gr.Textbox(
                label="🧬 生成的蛋白质序列",
                lines=8
            )
    
    # 示例部分
    gr.Markdown("### 💡 示例指令(点击试用)")
    
    examples = gr.Examples(
        examples=[
            ["Generate a hydrophobic transmembrane protein sequence"],
            ["Create a water-soluble protein with beta-sheet structure"],
            ["Design a protein with enzymatic activity for hydrolysis"],
            ["Generate a stable protein for high temperature environments"]
        ],
        inputs=[instruction],
        outputs=[output],
        fn=generate_protein,
        cache_examples=False
    )
    
    # 连接按钮事件
    generate_btn.click(
        fn=generate_protein,
        inputs=[instruction, max_length],
        outputs=[output]
    )
    
    # 状态信息
    gr.Markdown(f"""
    ---
    **系统状态**:
    - 运行模式: {run_mode}
    - Token状态: {token_status}
    - 模型状态: {model_status}
    - 硬件: CPU Basic
    
    *基于 [mzcwd/ProtTeX](https://huggingface.co/mzcwd/ProtTeX) 模型*
    """)

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        share=False
    )