Spaces:
Running
Running
from transformers import AutoModelForCausalLM, AutoTokenizer | |
import torch | |
model_name = "microsoft/Phi-4-mini-instruct" | |
# Load model and tokenizer | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto") | |
# Generate a response | |
def chat_with_model(prompt, max_new_tokens=100): | |
inputs = tokenizer(prompt, return_tensors="pt").to("cpu") # Move to GPU if available | |
output = model.generate(**inputs, max_new_tokens=max_new_tokens) | |
return tokenizer.decode(output[0], skip_special_tokens=True) | |
# Test the model | |
user_input = "Explain quantum computing in simple terms." | |
response = chat_with_model(user_input) | |
print("Chatbot Response:", response) | |