File size: 1,198 Bytes
57c7d18 322a100 a31e26d 57c7d18 322a100 a31e26d 322a100 a31e26d 57c7d18 322a100 a31e26d 57c7d18 |
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 |
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
# Specify the model and tokenizer
model_name = "deepseek-ai/deepseek-math-7b-instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
model.generation_config = GenerationConfig.from_pretrained(model_name)
model.generation_config.pad_token_id = model.generation_config.eos_token_id
# Function to read text from a file
def read_input_text(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
return text.strip()
# Example usage: Replace 'input.txt' with your file path
input_text = read_input_text('input.txt')
# Prepare input as a chat message
messages = [{"role": "user", "content": input_text}]
input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
# Generate outputs from the model
outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
# Decode the generated output
result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
print(result)
|