jaygala24 commited on
Commit
55676e8
1 Parent(s): e21c277

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +64 -2
README.md CHANGED
@@ -20,8 +20,6 @@ Please check the corresponding huggingface dataset card for more details.
20
  This was trained as part of the blog [Introducing Airavata: Hindi Instruction-tuned Chat Model](https://ai4bharat.github.io/airavata).
21
  The codebase used to train and evaluate this model can be found at [https://github.com/AI4Bharat/IndicInstruct](https://github.com/AI4Bharat/IndicInstruct).
22
 
23
-
24
-
25
  ## Usage
26
 
27
  Clone [https://github.com/AI4Bharat/IndicInstruct](https://github.com/AI4Bharat/IndicInstruct) and install the required dependencies. Then download or clone this model to the same machine.
@@ -51,6 +49,70 @@ We fine-tune OpenHathi base model on the aforementioned IndicInstruct dataset wi
51
 
52
  We recommend the readers to check out [our official blog post](https://ai4bharat.github.io/airavata) for more details on the model training, ablations and evaluation results.
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  ## Citation
55
 
56
  ```bibtex
 
20
  This was trained as part of the blog [Introducing Airavata: Hindi Instruction-tuned Chat Model](https://ai4bharat.github.io/airavata).
21
  The codebase used to train and evaluate this model can be found at [https://github.com/AI4Bharat/IndicInstruct](https://github.com/AI4Bharat/IndicInstruct).
22
 
 
 
23
  ## Usage
24
 
25
  Clone [https://github.com/AI4Bharat/IndicInstruct](https://github.com/AI4Bharat/IndicInstruct) and install the required dependencies. Then download or clone this model to the same machine.
 
49
 
50
  We recommend the readers to check out [our official blog post](https://ai4bharat.github.io/airavata) for more details on the model training, ablations and evaluation results.
51
 
52
+ ## Example
53
+
54
+ ```python3
55
+ import torch
56
+ from transformers import AutoTokenizer, AutoModelForCausalLM
57
+
58
+ device = "cuda" if torch.cuda.is_available() else "cpu"
59
+
60
+
61
+ def create_prompt_with_chat_format(messages, bos="<s>", eos="</s>", add_bos=True):
62
+ formatted_text = ""
63
+ for message in messages:
64
+ if message["role"] == "system":
65
+ formatted_text += "<|system|>\n" + message["content"] + "\n"
66
+ elif message["role"] == "user":
67
+ formatted_text += "<|user|>\n" + message["content"] + "\n"
68
+ elif message["role"] == "assistant":
69
+ formatted_text += "<|assistant|>\n" + message["content"].strip() + eos + "\n"
70
+ else:
71
+ raise ValueError(
72
+ "Tulu chat template only supports 'system', 'user' and 'assistant' roles. Invalid role: {}.".format(
73
+ message["role"]
74
+ )
75
+ )
76
+ formatted_text += "<|assistant|>\n"
77
+ formatted_text = bos + formatted_text if add_bos else formatted_text
78
+ return formatted_text
79
+
80
+
81
+ def inference(input_prompts, model, tokenizer):
82
+ input_prompts = [
83
+ create_prompt_with_chat_format([{"role": "user", "content": input_prompt}], add_bos=False)
84
+ for input_prompt in input_prompts
85
+ ]
86
+
87
+ encodings = tokenizer(input_prompts, padding=True, return_tensors="pt")
88
+ encodings = encodings.to(device)
89
+
90
+ with torch.inference_mode():
91
+ outputs = model.generate(encodings.input_ids, do_sample=False, max_new_tokens=250)
92
+
93
+ output_texts = tokenizer.batch_decode(outputs.detach(), skip_special_tokens=True)
94
+
95
+ input_prompts = [
96
+ tokenizer.decode(tokenizer.encode(input_prompt), skip_special_tokens=True) for input_prompt in input_prompts
97
+ ]
98
+ output_texts = [output_text[len(input_prompt) :] for input_prompt, output_text in zip(input_prompts, output_texts)]
99
+ return output_texts
100
+
101
+
102
+ model_name = "ai4bharat/Airavata"
103
+
104
+ tokenizer = AutoTokenizer.from_pretrained(model_name, padding_side="left", token=hf_token)
105
+ tokenizer.pad_token = tokenizer.eos_token
106
+ model = AutoModelForCausalLM.from_pretrained(model_name, token=hf_token).to(device)
107
+
108
+ input_prompts = [
109
+ "मैं अपने समय प्रबंधन कौशल को कैसे सुधार सकता हूँ? मुझे पांच बिंदु बताएं।",
110
+ "मैं अपने समय प्रबंधन कौशल को कैसे सुधार सकता हूँ? मुझे पांच बिंदु बताएं और उनका वर्णन करें।",
111
+ ]
112
+ outputs = inference(input_prompts, model, tokenizer)
113
+ print(outputs)
114
+ ```
115
+
116
  ## Citation
117
 
118
  ```bibtex