Chryslerx10 commited on
Commit
7804c76
·
verified ·
1 Parent(s): ed8c139

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +36 -0
README.md CHANGED
@@ -78,4 +78,40 @@ Libraries Used:
78
  tokenizer.pad_token = tokenizer.eos_token
79
 
80
  peft_loaded_model = PeftModel.from_pretrained(model, peft_model_id, device_map='auto')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  ```
 
78
  tokenizer.pad_token = tokenizer.eos_token
79
 
80
  peft_loaded_model = PeftModel.from_pretrained(model, peft_model_id, device_map='auto')
81
+ ```
82
+
83
+ ## Inference the model
84
+ ```python
85
+ def create_chat_template(question):
86
+ text = (
87
+ "[Instruction] You are a question-answering agent which answers the question based on the related reviews. "
88
+ "If related reviews are not provided, you can generate the answer based on the question.\n"
89
+ f"[Question] {question}\n"
90
+ "[Related Reviews] {context}\n"
91
+ "[Answer] "
92
+ )
93
+ return text
94
+
95
+ def generate_response(question, context):
96
+ text = create_chat_template(question, context)
97
+ inputs = tokenizer([text], return_tensors='pt', padding=True, truncation=True).to(device)
98
+
99
+ config = GenerationConfig(
100
+ max_length=256,
101
+ temperature=0.5,
102
+ top_k=5,
103
+ top_p=0.95,
104
+ repetition_penalty=1.2,
105
+ do_sample=True,
106
+ penalty_alpha=0.6
107
+ )
108
+
109
+ response = model.generate(**inputs, generation_config=config)
110
+ output = tokenizer.decode(response[0], skip_special_tokens=True)
111
+ return output
112
+
113
+ # Example usage
114
+ question = "Explain the process of photosynthesis."
115
+ response = generate_response(question)
116
+ print(response)
117
  ```