Views
No views yet

1from transformers import Pipeline
2import torch.nn.functional as F
3
4
5class MyPipeline:
6
7 def __init__(self,model,tokenizer):
8 self.model=model
9 self.tokenizer=tokenizer
10
11 def chat_Format(self,context, quetion, answer):
12 return "Instruction:/n check answer is true or false of next quetion using context below:\n" + "#context: " + context + f".\n#quetion: " + quetion + f".\n#student answer: " + answer + ".\n#response: "
13
14
15 def __call__(self, context, quetion, answer,generate=1,max_new_tokens=4, num_beams=2, do_sample=False,num_return_sequences=1):
16 inp=self.chat_Format(context, quetion, answer)
17 w = self.tokenizer(inp, add_special_tokens=True,
18 pad_to_max_length=True,
19 return_attention_mask=True,
20 return_tensors='pt')
21 response=""
22 if(generate):
23 outputs = self.tokenizer.batch_decode(self.model.generate(input_ids=w['input_ids'].cuda(), attention_mask=w['attention_mask'].cuda(), max_new_tokens=max_new_tokens, num_beams=num_beams, do_sample=do_sample, num_return_sequences=num_return_sequences), skip_special_tokens=True)
24 response = outputs
25
26 s =self.model(input_ids=w['input_ids'].cuda(), attention_mask=w['attention_mask'].cuda())['logits'][0][-1]
27 s = F.softmax(s, dim=-1)
28 yes_token_id = self.tokenizer.convert_tokens_to_ids(self.tokenizer.tokenize("True")[0])
29 no_token_id = self.tokenizer.convert_tokens_to_ids(self.tokenizer.tokenize("False")[0])
30
31 for i in ["Yes", "yes", "True", "true","صحيح"]:
32 for word in self.tokenizer.tokenize(i):
33 s[yes_token_id] += s[self.tokenizer.convert_tokens_to_ids(word)]
34 for i in ["No", "no", "False", "false","خطأ"]:
35 for word in self.tokenizer.tokenize(i):
36
37 s[no_token_id] += s[self.tokenizer.convert_tokens_to_ids(word)]
38 true = (s[yes_token_id] / (s[no_token_id] + s[yes_token_id])).item()
39 return {"response": response, "true": true}
40context="""Large language models, such as GPT-4, are trained on vast amounts of text data to understand and generate human-like text. The deployment of these models involves several steps:
41
42 Model Selection: Choosing a pre-trained model that fits the application's needs.
43 Infrastructure Setup: Setting up the necessary hardware and software infrastructure to run the model efficiently, including cloud services, GPUs, and necessary libraries.
44 Integration: Integrating the model into an application, which can involve setting up APIs or embedding the model directly into the software.
45 Optimization: Fine-tuning the model for specific tasks or domains and optimizing it for performance and cost-efficiency.
46 Monitoring and Maintenance: Ensuring the model performs well over time, monitoring for biases, and updating the model as needed."""
47quetion="What are the key considerations when choosing a cloud service provider for deploying a large language model like GPT-4?"
48answer="""When choosing a cloud service provider for deploying a large language model like GPT-4, the key considerations include:
49 Compute Power: Ensure the provider offers high-performance GPUs or TPUs capable of handling the computational requirements of the model.
50 Scalability: The ability to scale resources up or down based on the application's demand to handle varying workloads efficiently.
51 Cost: Analyze the pricing models to understand the costs associated with compute time, storage, data transfer, and any other services.
52 Integration and Support: Availability of tools and libraries that support easy integration of the model into your applications, along with robust technical support and documentation.
53 Security and Compliance: Ensure the provider adheres to industry standards for security and compliance, protecting sensitive data and maintaining privacy.
54 Latency and Availability: Consider the geographical distribution of data centers to ensure low latency and high availability for your end-users.
55
56By evaluating these factors, you can select a cloud service provider that aligns with your deployment needs, ensuring efficient and cost-effective operation of your large language model."""
57from peft import PeftModel, PeftConfig
58from transformers import AutoModelForCausalLM,AutoTokenizer
59
60config = PeftConfig.from_pretrained("mohamedemam/Em2-llama-7b")
61base_model = AutoModelForCausalLM.from_pretrained("NousResearch/Llama-2-7b-hf")
62model = PeftModel.from_pretrained(base_model, "mohamedemam/Em2-llama-7b")
63tokenizer = AutoTokenizer.from_pretrained("mohamedemam/Em2-llama-7b", trust_remote_code=True)
64pipe=MyPipeline(model,tokenizer)
65print(pipe(context,quetion,answer,generate=True,max_new_tokens=4, num_beams=2, do_sample=False,num_return_sequences=1))
66 1def chat_Format(self, context, question, answer):
2 return "Instruction:/n check answer is true or false of next question using context below:\n" + "#context: " + context + f".\n#question: " + question + f".\n#student answer: " + answer + ".\n#response: "