Spaces:
Running
on
Zero
Running
on
Zero
File size: 10,333 Bytes
ffce453 e77dcbd ffce453 e77dcbd ffce453 e77dcbd ffce453 e77dcbd ffce453 e77dcbd ffce453 e77dcbd ffce453 e77dcbd ffce453 e77dcbd ffce453 e77dcbd ffce453 e77dcbd ffce453 e77dcbd ffce453 |
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
import gradio as gr
import os
import spaces
from transformers import AutoTokenizer, AutoModelForCausalLM
from threading import Thread
import torch
import time
# Set environment variables
HF_TOKEN = os.environ.get("HF_TOKEN", None)
# Apollo system prompt
SYSTEM_PROMPT = "You are Apollo, a multilingual medical model. You communicate with people and assist them."
# Apollo model options
APOLLO_MODELS = {
"Apollo": [
"FreedomIntelligence/Apollo-7B",
"FreedomIntelligence/Apollo-6B",
"FreedomIntelligence/Apollo-2B",
"FreedomIntelligence/Apollo-0.5B",
],
"Apollo2": [
"FreedomIntelligence/Apollo2-7B",
"FreedomIntelligence/Apollo2-3.8B",
"FreedomIntelligence/Apollo2-2B",
],
"Apollo-MoE": [
"FreedomIntelligence/Apollo-MoE-7B",
"FreedomIntelligence/Apollo-MoE-1.5B",
"FreedomIntelligence/Apollo-MoE-0.5B",
]
}
# CSS styles
css = """
h1 {
text-align: center;
display: block;
}
.gradio-container {
max-width: 1200px;
margin: auto;
}
"""
# Global variables to store currently loaded model and tokenizer
current_model = None
current_tokenizer = None
current_model_path = None
@spaces.GPU(duration=120)
def load_model(model_path, progress=gr.Progress()):
"""Load the selected model and tokenizer"""
global current_model, current_tokenizer, current_model_path
# If the same model is already loaded, don't reload it
if current_model_path == model_path and current_model is not None:
return "Model already loaded, no need to reload."
# Clean up previously loaded model (if any)
if current_model is not None:
del current_model
del current_tokenizer
torch.cuda.empty_cache()
progress(0.1, desc=f"Starting to load model {model_path}...")
try:
progress(0.3, desc="Loading tokenizer...")
current_tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
progress(0.5, desc="Loading model...")
current_model = AutoModelForCausalLM.from_pretrained(
model_path,
device_map="auto",
torch_dtype=torch.float16
)
current_model_path = model_path
progress(1.0, desc="Model loading complete!")
return f"Model {model_path} successfully loaded."
except Exception as e:
progress(1.0, desc="Model loading failed!")
return f"Model loading failed: {str(e)}"
@spaces.GPU(duration=120)
def generate_response_non_streaming(instruction, model_name, temperature=0.7, max_tokens=1024):
"""Generate a response from the Apollo model (non-streaming)"""
global current_model, current_tokenizer, current_model_path
# If model is not yet loaded, load it first
if current_model_path != model_name or current_model is None:
load_message = load_model(model_name)
if "failed" in load_message.lower():
return load_message
try:
# 检查模型是否有聊天模板
if hasattr(current_tokenizer, 'chat_template') and current_tokenizer.chat_template:
# 使用模型的聊天模板
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": instruction}
]
# 使用模型的聊天模板格式化输入
chat_input = current_tokenizer.apply_chat_template(
messages,
tokenize=True,
return_tensors="pt"
).to(current_model.device)
else:
# 使用指定的提示格式
prompt = f"User:{instruction}\nAssistant:"
chat_input = current_tokenizer.encode(prompt, return_tensors="pt").to(current_model.device)
# 获取<|endoftext|>的token id,用于停止生成
eos_token_id = current_tokenizer.eos_token_id
# 生成响应
output = current_model.generate(
input_ids=chat_input,
max_new_tokens=max_tokens,
temperature=temperature,
do_sample=(temperature > 0),
eos_token_id=current_tokenizer.eos_token_id # 使用<|endoftext|>作为停止标记
)
# 解码并返回生成的文本
generated_text = current_tokenizer.decode(output[0][len(chat_input[0]):], skip_special_tokens=True)
return generated_text
except Exception as e:
return f"生成响应时出错: {str(e)}"
def update_chat_with_response(chatbot, instruction, model_name, temperature, max_tokens):
"""Updates the chatbot with non-streaming response"""
global current_model, current_tokenizer, current_model_path
# If model is not yet loaded, load it first
if current_model_path != model_name or current_model is None:
load_result = load_model(model_name)
if "failed" in load_result.lower():
new_chat = list(chatbot)
new_chat[-1] = (instruction, load_result)
return new_chat
# Generate response using the non-streaming function
response = generate_response_non_streaming(instruction, model_name, temperature, max_tokens)
# Create a copy of the current chatbot and add the response
new_chat = list(chatbot)
new_chat[-1] = (instruction, response)
return new_chat
def on_model_series_change(model_series):
"""Update available model list based on selected model series"""
if model_series in APOLLO_MODELS:
return gr.update(choices=APOLLO_MODELS[model_series], value=APOLLO_MODELS[model_series][0])
return gr.update(choices=[], value=None)
# Create Gradio interface
with gr.Blocks(css=css) as demo:
# Title and description
favicon = "🩺"
gr.Markdown(
f"""# {favicon} Apollo Playground
This is a demo of the multilingual medical model series **[Apollo](https://huggingface.co/FreedomIntelligence/Apollo-7B-GGUF)**.
[Apollo1](https://arxiv.org/abs/2403.03640) supports 6 languages. [Apollo2](https://arxiv.org/abs/2410.10626) supports 50 languages.
"""
)
with gr.Row():
with gr.Column(scale=1):
# Model selection controls
model_series = gr.Dropdown(
choices=list(APOLLO_MODELS.keys()),
value="Apollo",
label="Select Model Series",
info="First choose Apollo, Apollo2 or Apollo-MoE"
)
model_name = gr.Dropdown(
choices=APOLLO_MODELS["Apollo"],
value=APOLLO_MODELS["Apollo"][0],
label="Select Model Size",
info="Select the specific model size based on the chosen model series"
)
# Parameter settings
with gr.Accordion("Generation Parameters", open=False):
temperature = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.7,
step=0.05,
label="Temperature"
)
max_tokens = gr.Slider(
minimum=128,
maximum=2048,
value=1024,
step=32,
label="Maximum Tokens"
)
# Load model button
load_button = gr.Button("Load Model")
model_status = gr.Textbox(label="Model Status", value="No model loaded yet")
with gr.Column(scale=2):
# Chat interface
chatbot = gr.Chatbot(label="Conversation", height=500, value=[]) # Initialize with empty list
user_input = gr.Textbox(
label="Input Medical Question",
placeholder="Example: What are the symptoms of hypertension? 高血压有哪些症状?",
lines=3
)
submit_button = gr.Button("Submit")
clear_button = gr.Button("Clear Chat")
# Event handling
# Update model selection when model series changes
model_series.change(
fn=on_model_series_change,
inputs=model_series,
outputs=model_name
)
# Load model
load_button.click(
fn=load_model,
inputs=model_name,
outputs=model_status
)
# Handle message submission
def user_message_submitted(message, chat_history):
"""Handle user submitted message"""
# Ensure chat_history is a list
if chat_history is None:
chat_history = []
if message.strip() == "":
return "", chat_history
# Add user message to chat history
chat_history = list(chat_history)
chat_history.append((message, None))
return "", chat_history
# Bind message submission
submit_event = user_input.submit(
fn=user_message_submitted,
inputs=[user_input, chatbot],
outputs=[user_input, chatbot]
).then(
fn=update_chat_with_response,
inputs=[chatbot, user_input, model_name, temperature, max_tokens],
outputs=chatbot
)
submit_button.click(
fn=user_message_submitted,
inputs=[user_input, chatbot],
outputs=[user_input, chatbot]
).then(
fn=update_chat_with_response,
inputs=[chatbot, user_input, model_name, temperature, max_tokens],
outputs=chatbot
)
# Clear chat
clear_button.click(
fn=lambda: [],
outputs=chatbot
)
examples = [
["Últimamente tengo la tensión un poco alta, ¿cómo debo adaptar mis hábitos?"],
["What are the common side effects of metformin?"],
["中医和西医在治疗高血压方面有什么不同的观点?"],
["मेरा सिर दर्द कर रहा है, मुझे क्या करना चाहिए? "],
["Comment savoir si je suis diabétique ?"],
["ما الدواء الذي يمكنني تناوله إذا لم أستطع النوم ليلاً؟"]
]
gr.Examples(
examples=examples,
inputs=user_input
)
if __name__ == "__main__":
demo.launch()
|