Views
No views yet

1
2import transformers
3import torch
4from huggingface_hub import login
5login(token="your_huggingface_access_token")
6
7from transformers import AutoTokenizer, AutoModelForCausalLM
8model_name = "olmairesearch/olm-v1.0-8b"
9tokenizer = AutoTokenizer.from_pretrained(model_name, token=True)
10model = AutoModelForCausalLM.from_pretrained(
11 model_name, torch_dtype="auto", device_map="auto",token=True
12)
13
14def call_Buddhism(model, tokenizer, history, input_):
15 context = ""
16 for role, message in history:
17 context += f"{role}: {message}\n"
18 context += f"user: {input_}\nassistant:"
19 inputs = tokenizer(context, return_tensors="pt", truncation=True)
20 inputs = inputs.to("cuda" if torch.cuda.is_available() else "cpu")
21 outputs = model.generate(
22 **inputs,
23 max_new_tokens=256,
24 eos_token_id=tokenizer.eos_token_id,
25 do_sample=True,
26 temperature=0.6,
27 top_p=0.9,
28 )
29 generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
30 response = generated_text[len(context):].strip()
31 return response
32
33history = [
34 ("user", "What is the purpose of life?"),
35 ("assistant", "The purpose of life is to seek inner peace and happiness.")
36]
37input_ = "How can I achieve inner peace?"
response = call_Buddhism(model, tokenizer, history, user_input)
print("Buddha:", response)input_: "How can I find happiness that lasts?"
output: "Happiness is not found in external things, but in the cultivation of your own mind. by letting go of attachments and desires, you can find a sense of peace and freedom."
input_: "What can I do for this?"
output: "You can start by letting go of attachments and desires. focus on the present moment and cultivate mindfulness."
input_: "how to cultivate mindfulness"
output: "mindfulness is the key to understanding the present moment. practice meditation and observe your thoughts and emotions without judgment."
input_: "meditation?"
output: "yes, meditation is a powerful tool to quiet the mind and connect with your true nature. but it's not just about sitting still, it's about cultivating mindfulness and compassion in every moment."
input_: "What should I think about when I sit quietly?"
output: "When you sit quietly, focus on your breath and let go of all thoughts and distractions. this will help you cultivate mindfulness and inner peace."
input_: "how many times should i spend on it?"
output: "It is not about the quantity of time, but the quality of attention. take a few moments each day to reflect on your thoughts and actions, and you will find that inner peace is always within reach."