gaur3009 commited on
Commit
ace7592
Β·
verified Β·
1 Parent(s): 5d969f7

Update llm.py

Browse files
Files changed (1) hide show
  1. llm.py +24 -18
llm.py CHANGED
@@ -1,23 +1,29 @@
1
- from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
2
 
3
- tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base")
4
- model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
 
 
 
5
 
6
  def generate_answer(context, question):
7
  prompt = f"""
8
- You are a helpful AI assistant.
9
-
10
- Context:
11
- {context}
12
-
13
- Question: {question}
14
-
15
- Answer as a helpful paragraph:"""
16
-
17
- inputs = tokenizer(prompt, return_tensors='pt', truncation=True, max_length=512)
18
- outputs = model.generate(
19
- **inputs,
20
- max_new_tokens=100,
21
- do_sample=False
 
 
22
  )
23
- return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
 
 
1
+ from transformers import pipeline
2
 
3
+ qa_pipeline = pipeline(
4
+ "text2text-generation",
5
+ model="google/flan-t5-large",
6
+ device=0 if torch.cuda.is_available() else -1
7
+ )
8
 
9
  def generate_answer(context, question):
10
  prompt = f"""
11
+ You are a helpful research assistant. Use the context to answer the question.
12
+
13
+ Context:
14
+ {context}
15
+
16
+ Question: {question}
17
+
18
+ Answer in a comprehensive paragraph with key details:
19
+ """
20
+
21
+ result = qa_pipeline(
22
+ prompt,
23
+ max_length=512,
24
+ do_sample=True,
25
+ temperature=0.7,
26
+ top_p=0.9
27
  )
28
+
29
+ return result[0]['generated_text'].strip()