Spaces:
Runtime error
Runtime error
Update app.py
Browse files7 unmodified lines891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253547 unmodified linespipe = pipeline("text-generation",model=model_id,torch_dtype=torch.float16,device_map="auto",)@spaces.GPU(duration=120)def respond(message, history):# Build prompt from historyprompt = ""for user_msg, assistant_msg in history:if user_msg:prompt += f"User: {user_msg}\n"if assistant_msg:prompt += f"Assistant: {assistant_msg}\n"# Add current messageprompt += f"User: {message}\nAssistant: "# Generate response with streamingstreamer = pipe.tokenizer.decode# Generate tokensinputs = pipe.tokenizer(prompt, return_tensors="pt").to(pipe.model.device)with torch.no_grad():outputs = pipe.model.generate(**inputs,max_new_tokens=10000,temperature=0.7,do_sample=True,pad_token_id=pipe.tokenizer.eos_token_id,)# Decode the generated tokens, skipping the input tokensgenerated_tokens = outputs[0][inputs['input_ids'].shape[-1]:]# Stream the output token by tokenresponse_text = ""for i in range(len(generated_tokens)):token = generated_tokens[i:i+1]token_text = pipe.tokenizer.decode(token, skip_special_tokens=True)response_text += token_textyield response_text# Create the chat interface7 unmodified lines8910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777 unmodified linespipe = pipeline("text-generation",model=model_id,torch_dtype="auto",device_map="auto",)@spaces.GPU(duration=120)def respond(message, history):# Build messages list from historymessages = []# Add system message based on content type detectionif any(kw in message.lower() for kw in ["python", "def ", "function"]):messages.append({"role": "system","content": ("\nYou are a helpful and harmless assistant. You should think step-by-step before responding to the instruction below.\n\n""Please use python programming language only.\n""You must use ```python for just the final solution code block with the following format:\n""```python\n# Your code here\n```\n")})elif any(kw in message.lower() for kw in ["c++", "cpp", "#include", "cout"]):messages.append({"role": "system","content": ("\nYou are a helpful and harmless assistant. You should think step-by-step before responding to the instruction below.\n\n""Please use c++ programming language only.\n""You must use ```cpp for just the final solution code block with the following format:\n""```cpp\n// Your code here\n```\n")})elif any(kw in message.lower() for kw in ["compute", "calculate", "math", "+", "-", "*", "/"]):messages.append({"role": "system","content": "Please reason step by step, and put your final answer within \\boxed{}."})else:messages.append({"role": "system","content": "You are a helpful AI assistant."})# Add conversation historyfor user_msg, assistant_msg in history:if user_msg:messages.append({"role": "user", "content": user_msg})if assistant_msg:messages.append({"role": "assistant", "content": assistant_msg})# Add current messagemessages.append({"role": "user", "content": message})# Generate responseoutputs = pipe(messages,max_new_tokens=8192,temperature=0.7,do_sample=True,)# Extract and stream the generated textfull_response = outputs[0]["generated_text"][-1]["content"]response_text = ""for char in full_response:response_text += charyield response_text# Create the chat interface
app.py+51-28