Spaces:
Paused
Paused
Update app.py
Browse files1 unmodified line2345678910111213141516171819202122232425262728293031323334351 unmodified linefrom transformers import AutoTokenizer, AutoModelForCausalLMimport torch# Load model and tokenizermodel_name = "deepseek-ai/DeepSeek-V3-0324"tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto", trust_remote_code=True)# Function to generate responsedef chat_with_bot(user_input, history):history = history or []prompt = ""for i, (user, bot) in enumerate(history):prompt += f"<|user|>\n{user}\n<|assistant|>\n{bot}\n"prompt += f"<|user|>\n{user_input}\n<|assistant|>\n"inputs = tokenizer(prompt, return_tensors="pt").to(model.device)outputs = model.generate(**inputs, max_new_tokens=512, do_sample=True, temperature=0.7, top_p=0.9, eos_token_id=tokenizer.eos_token_id)decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)response = decoded_output.split("<|assistant|>\n")[-1].strip()history.append((user_input, response))return response, history# Gradio UIchatbot_ui = gr.ChatInterface(fn=chat_with_bot,title="DeepSeek Chatbot",theme="soft",examples=["Tell me a joke", "What's the capital of France?", "Explain quantum computing simply."])chatbot_ui.launch()1 unmodified line2345678910111213141516171819202122232425262728293031323334353637383940414243444546471 unmodified linefrom transformers import AutoTokenizer, AutoModelForCausalLMimport torch# Model namemodel_name = "deepseek-ai/DeepSeek-V3-0324"# Load tokenizer and model in float32 for CPUtokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained(model_name,trust_remote_code=True,torch_dtype=torch.float32, # CPU-compatible precisiondevice_map={"": "cpu"} # Force CPU)# Chat functiondef chat_with_bot(user_input, history):history = history or []prompt = ""for user, bot in history:prompt += f"<|user|>\n{user}\n<|assistant|>\n{bot}\n"prompt += f"<|user|>\n{user_input}\n<|assistant|>\n"inputs = tokenizer(prompt, return_tensors="pt").to("cpu")outputs = model.generate(**inputs,max_new_tokens=256,do_sample=True,temperature=0.7,top_p=0.9,eos_token_id=tokenizer.eos_token_id,)decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)response = decoded.split("<|assistant|>\n")[-1].strip()history.append((user_input, response))return response, history# Gradio UIgr.ChatInterface(fn=chat_with_bot,title="DeepSeek CPU Chatbot",theme="soft",examples=["Tell me a joke", "What's the capital of Italy?", "What is a black hole?"]).launch()
app.py+27-15