Spaces:
Running on Zero
Running on Zero
feat: Refactor comp.py and add gitignore
Browse filesRefactor comp.py to:
- Add trust_remote_code=True to AutoTokenizer.
- Change system prompt to Chinese.
- Add comments for better readability.
Add .gitignore to ignore temporary directories.
Files changed (2)
- .gitignoreAdded
- comp.py+12-7
123456789106 unmodified lines171819202122232425262728293031323334353614 unmodified lines51525354555657585960616263import torchfrom transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamerfrom threading import Threadimport spaces# --- 模型加载 ---# 使用 "auto" 模式加载模型和分词器,Hugging Face Accelerate 会自动处理设备和精度MODEL_NAME = "inclusionAI/Ring-mini-2.0"tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)6 unmodified lines@spaces.GPU(duration=120)def generate_response(message, history):# Convert history to messages formatmessages = [{"role": "system", "content": "You are Ring, an assistant created by inclusionAI"}]# Add conversation historyfor human, assistant in history:messages.append({"role": "user", "content": human})messages.append({"role": "assistant", "content": assistant})# Add current messagemessages.append({"role": "user", "content": message})# Apply chat templatetext = tokenizer.apply_chat_template(messages,tokenize=False,14 unmodified linesstreamer=streamer,)thread = Thread(target=model.generate, kwargs=generation_kwargs)thread.start()# Stream the responseresponse = ""for new_text in streamer:response += new_textyield responsethread.join()123456786 unmodified lines1516171819202122232425262728293031323334353637383914 unmodified lines545556575859606162636465666768from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamerfrom threading import Threadimport spaces# Model and tokenizer initializationMODEL_NAME = "inclusionAI/Ring-mini-2.0"tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)6 unmodified lines@spaces.GPU(duration=120)def generate_response(message, history):# (msg, history) -> str: stream response (yielding partial responses)# To construct the 'chat', we start with system prompt# then append user and assistant messages from historymessages = [{"role": "system", "content": "你是 Ring,蚂蚁集团开发的智能助手,致力于为用户提供有用的信息和帮助,用中文回答用户的问题。"}]# Add conversation history# history is a list of (human, assistant) tuplesfor human, assistant in history:messages.append({"role": "user", "content": human})messages.append({"role": "assistant", "content": assistant})# Add current message from usermessages.append({"role": "user", "content": message})# Apply chat template# Doc: https://github.com/huggingface/transformers/blob/main/src/transformers/tokenization_utils_base.py#L1510text = tokenizer.apply_chat_template(messages,tokenize=False,14 unmodified linesstreamer=streamer,)# Start generation in a separate thread to enable streamingthread = Thread(target=model.generate, kwargs=generation_kwargs)thread.start()# ... and yield the generated tokens as they are producedresponse = ""for new_text in streamer:response += new_textyield response# wait for the generation thread to finishthread.join()
.gitignoreAdded
comp.py+12-7