Views
No views yet
git clone https://huggingface.co/justsomerandomdude264/Science_Homework_Solver-Llama3.18B1from unsloth import FastLanguageModel
2import torch
3
4# Define Your Question
5question = "A tank is filled with water to a height of 12.5 cm. The apparent depth of a needle lying at the bottom of the tank is measured by a microscope to be 9.4 cm. What is the refractive index of water? If water is replaced by a liquid of refractive index 1.63 up to the same height, by what distance would the microscope have to be moved to focus on the needle again?" # Example Question, You can change it with one of your own
6# Load the model
7model, tokenizer = FastLanguageModel.from_pretrained(
8 model_name = "Science_Homework_Solver_Llama318B/model_adapters", # The dir where the repo is cloned or "\\" for root
9 max_seq_length = 2048,
10 dtype = None,
11 load_in_4bit = True,
12 )
13# Set the model in inference model
14FastLanguageModel.for_inference(model)
15# QA template
16qa_template = """Question: {}
17Answer: {}"""
18# Tokenize inputs
19inputs = tokenizer(
20[
21 qa_template.format(
22 question, # Question
23 "", # Answer - left blank for generation
24 )
25], return_tensors = "pt").to("cuda")
26# Stream the answer/output of the model
27from transformers import TextStreamer
28text_streamer = TextStreamer(tokenizer)
29_ = model.generate(**inputs, streamer = text_streamer, max_new_tokens = 512)1from transformers import LlamaForCausalLM, AutoTokenizer
2# Load the model
3model = LlamaForCausalLM.from_pretrained(
4 "justsomerandomdude264/Science_Homework_Solver_Llama318B",
5 device_map="auto"
6)
7
8# Load the tokenizer
9tokenizer = AutoTokenizer.from_pretrained("justsomerandomdude264/Science_Homework_Solver_Llama318B")
10# Set the inputs up
11qa_template = """Question: {}
12Answer: {}"""
13
14inputs = tokenizer(
15[
16 qa_template.format(
17 "A tank is filled with water to a height of 12.5 cm. The apparent depth of a needle lying at the bottom of the tank is measured by a microscope to be 9.4 cm. What is the refractive index of water? If water is replaced by a liquid of refractive index 1.63 up to the same height, by what distance would the microscope have to be moved to focus on the needle again?", # instruction
18 "", # output - leave this blank for generation!
19 )
20], return_tensors = "pt").to("cuda")
21
22# Do a forward pass
23outputs = model.generate(**inputs, max_new_tokens = 128, use_cache = True)
24raw_output = str(tokenizer.batch_decode(outputs))
25
26# Formtting the string
27# Removing the list brackets and splitting the string by newline characters
28formatted_string = raw_output.strip("[]").replace("<|begin_of_text|>", "").replace("<|eot_id|>", "").strip("''").split("\\n")
29# Print the lines one by one
30for line in formatted_string:
31 print(line)1@misc{paliwal2024,
2 author = {Krishna Paliwal},
3 title = {Contributions to Science_Homework_Solver},
4 year = {2024},
5 email = {krishna.plwl264@gmail.com}
6}Paliwal, Krishna (2024). Contributions to Science_Homework_Solver. Email: krishna.plwl264@gmail.com .