hassanaliemon commited on
Commit
e863a21
·
verified ·
1 Parent(s): 3d3b5f3

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +27 -6
README.md CHANGED
@@ -12,12 +12,24 @@ pipeline_tag: question-answering
12
  You can use the model with a pipeline for a high-level helper or load the model directly. Here's how:
13
 
14
  ```python
15
- # Load model directly
16
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
 
 
17
  tokenizer = AutoTokenizer.from_pretrained("hassanaliemon/bn_rag_llama3-8b")
18
- model = AutoModelForCausalLM.from_pretrained("hassanaliemon/bn_rag_llama3-8b")
 
 
 
 
 
 
19
 
20
- prompt = """Below is an instruction in Bengali language that describes a task, paired with an input also in Bengali language that provides further context. Write a response in Bengali language that appropriately completes the request.
 
21
 
22
  ### Instruction:
23
  {}
@@ -28,18 +40,27 @@ prompt = """Below is an instruction in Bengali language that describes a task, p
28
  ### Response:
29
  {}
30
  """
 
31
  def generate_response(question, context):
32
- inputs = tokenizer([prompt.format(question, context, "")], return_tensors="pt").to("cuda")
 
 
 
33
  outputs = model.generate(**inputs, max_new_tokens=1024, use_cache=True)
 
 
34
  responses = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
 
 
35
  response_start = responses.find("### Response:") + len("### Response:")
36
  response = responses[response_start:].strip()
 
37
  return response
38
 
39
- # Example Usage:
40
-
41
  question = "ভারতীয় বাঙালি কথাসাহিত্যিক মহাশ্বেতা দেবীর মৃত্যু কবে হয় ?"
42
  context = "২০১৬ সালের ২৩ জুলাই হৃদরোগে আক্রান্ত হয়ে মহাশ্বেতা দেবী কলকাতার বেল ভিউ ক্লিনিকে ভর্তি হন। সেই বছরই ২৮ জুলাই একাধিক অঙ্গ বিকল হয়ে তাঁর মৃত্যু ঘটে। তিনি মধুমেহ, সেপ্টিসেমিয়া ও মূত্র সংক্রমণ রোগেও ভুগছিলেন।"
43
  answer = generate_response(question, context)
44
  print(answer)
 
45
  ```
 
12
  You can use the model with a pipeline for a high-level helper or load the model directly. Here's how:
13
 
14
  ```python
15
+ import torch
16
  from transformers import AutoTokenizer, AutoModelForCausalLM
17
+
18
+ # Determine the device to use (GPU if available, else CPU)
19
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
20
+
21
+ # Load pre-trained model and tokenizer
22
  tokenizer = AutoTokenizer.from_pretrained("hassanaliemon/bn_rag_llama3-8b")
23
+ # model = AutoModelForCausalLM.from_pretrained("hassanaliemon/bn_rag_llama3-8b")
24
+ model = AutoModelForCausalLM.from_pretrained(
25
+ "hassanaliemon/bn_rag_llama3-8b",
26
+ load_in_8bit=True,
27
+ torch_dtype=torch.bfloat16,
28
+ device_map="auto",
29
+ )
30
 
31
+ # Define the prompt template
32
+ prompt = """এখানে একটি নির্দেশনা দেওয়া হলো, যা একটি কাজ সম্পন্ন করার উপায় বর্ণনা করে, এবং এর সাথে একটি ইনপুট দেওয়া হলো যা আরও প্রেক্ষাপট প্রদান করে। একটি উত্তর লিখুন যা অনুরোধটি সঠিকভাবে পূরণ করে। প্রসঙ্গ থেকে সুনির্দিষ্ট উত্তর দিন.
33
 
34
  ### Instruction:
35
  {}
 
40
  ### Response:
41
  {}
42
  """
43
+
44
  def generate_response(question, context):
45
+ # Tokenize the input and move tensors to the selected device
46
+ inputs = tokenizer([prompt.format(question, context, "")], return_tensors="pt").to(device)
47
+
48
+ # Generate the response
49
  outputs = model.generate(**inputs, max_new_tokens=1024, use_cache=True)
50
+
51
+ # Decode the generated text
52
  responses = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
53
+
54
+ # Extract the response text
55
  response_start = responses.find("### Response:") + len("### Response:")
56
  response = responses[response_start:].strip()
57
+
58
  return response
59
 
60
+ # Example Usage
 
61
  question = "ভারতীয় বাঙালি কথাসাহিত্যিক মহাশ্বেতা দেবীর মৃত্যু কবে হয় ?"
62
  context = "২০১৬ সালের ২৩ জুলাই হৃদরোগে আক্রান্ত হয়ে মহাশ্বেতা দেবী কলকাতার বেল ভিউ ক্লিনিকে ভর্তি হন। সেই বছরই ২৮ জুলাই একাধিক অঙ্গ বিকল হয়ে তাঁর মৃত্যু ঘটে। তিনি মধুমেহ, সেপ্টিসেমিয়া ও মূত্র সংক্রমণ রোগেও ভুগছিলেন।"
63
  answer = generate_response(question, context)
64
  print(answer)
65
+ # মহাশ্বেতা দেবী ২০১৬ সালের ২৮ জুলাই মারা যান।
66
  ```