jayebaku commited on
Commit
0f6e95f
·
verified ·
1 Parent(s): aef34a2

Update qa_summary.py

Browse files
Files changed (1) hide show
  1. qa_summary.py +60 -0
qa_summary.py CHANGED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+
3
+
4
+ def generate_answer(llm_name, texts, query, mode='validate'):
5
+
6
+ if llm_name == 'solar':
7
+ tokenizer = AutoTokenizer.from_pretrained("Upstage/SOLAR-10.7B-Instruct-v1.0", use_fast=True)
8
+ llm_model = AutoModelForCausalLM.from_pretrained(
9
+ "Upstage/SOLAR-10.7B-Instruct-v1.0",
10
+ device_map="auto", #device_map="cuda"
11
+ #torch_dtype=torch.float16,)
12
+
13
+ elif llm_name == 'mistral':
14
+ tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2", use_fast=True)
15
+ llm_model = AutoModelForCausalLM.from_pretrained(
16
+ "mistralai/Mistral-7B-Instruct-v0.2",
17
+ device_map="auto", #device_map="cuda"
18
+ #torch_dtype=torch.float16,)
19
+
20
+ elif llm_name == 'phi3mini':
21
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-128k-instruct", use_fast=True)
22
+ llm_model = AutoModelForCausalLM.from_pretrained(
23
+ "microsoft/Phi-3-mini-128k-instruct",
24
+ device_map="auto",
25
+ torch_dtype="auto",
26
+ trust_remote_code=True,)
27
+
28
+ template_texts =""
29
+ for i, text in enumerate(texts):
30
+ template_texts += f'{i+1}. {text} \n'
31
+
32
+ if mode == 'validate':
33
+ conversation = [ {'role': 'user', 'content': f'Given the following query: "{query}"? \nIs the following document relevant to answer this query?\n{template_texts} \nResponse: Yes / No'} ]
34
+ elif mode == 'summarize':
35
+ conversation = [ {'role': 'user', 'content': f'For the following query and documents, try to answer the given query based on the documents.\nQuery: {query} \nDocuments: {template_texts}.'} ]
36
+ elif mode == 'h_summarize':
37
+ conversation = [ {'role': 'user', 'content': f'The documents below describe a developing disaster event. Based on these documents, write a brief summary in the form of a paragraph, highlighting the most crucial information. \nDocuments: {template_texts}'} ]
38
+
39
+ prompt = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
40
+ inputs = tokenizer(prompt, return_tensors="pt").to(llm_model.device)
41
+ outputs = llm_model.generate(**inputs, use_cache=True, max_length=4096,do_sample=True,temperature=0.7,top_p=0.95,top_k=10,repetition_penalty=1.1)
42
+ output_text = tokenizer.decode(outputs[0])
43
+ if llm_name == "solar":
44
+ assistant_respond = output_text.split("Assistant:")[1]
45
+ elif llm_name == "phi3mini":
46
+ assistant_respond = output_text.split("<|assistant|>")[1]
47
+ assistant_respond = assistant_respond[:-7]
48
+ else:
49
+ assistant_respond = output_text.split("[/INST]")[1]
50
+ if mode == 'validate':
51
+ if 'Yes' in assistant_respond:
52
+ return True
53
+ else:
54
+ return False
55
+ elif mode == 'summarize':
56
+ return assistant_respond
57
+ elif mode == 'h_summarize':
58
+ return assistant_respond
59
+
60
+