# first 2 lines are to avoid warning messages from huggingface hub
import os
os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] = "1"
from transformers import pipeline
# 1. load the GPT 2 text generation model
generator = pipeline("text-generation", model="gpt2")
# 2. few shot chain-of-thought style prompt
prompt = """
Solve the math problems using the examples below.
Example 1:
Question:
If a person buys 3 notebooks for ₹20 each, what is the total cost?
Solution:
Each notebook costs ₹20.
Number of notebooks = 3.
Total cost = 3 × 20 = ₹60.
Final Answer: ₹60
Example 2:
Question:
If a student buys 5 pens for ₹10 each, what is the total cost?
Solution:
Each pen costs ₹10.
Number of pens = 5.
Total cost = 5 × 10 = ₹50.
Final Answer: ₹50
Now solve this problem:
Question:
A person buys 4 books for ₹25 each. What is the total cost?
Solution:
"""
# 3. generating response
result = generator(prompt, max_new_tokens=100, do_sample=False, pad_token_id=50256)
# 4. generated response - print
print(result[0]["generated_text"])