Spaces:
Runtime error
Runtime error
Update llm.py
Browse files
llm.py
CHANGED
@@ -1,40 +1,20 @@
|
|
1 |
-
from transformers import
|
2 |
-
import torch
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
model="google/flan-t5-base",
|
7 |
-
device=device
|
8 |
-
)
|
9 |
|
10 |
def generate_answer(context, question):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
Context:
|
25 |
-
{context}
|
26 |
-
|
27 |
-
Question: {question}
|
28 |
-
|
29 |
-
Answer as a comprehensive paragraph with key details:
|
30 |
-
"""
|
31 |
-
|
32 |
-
result = qa_pipeline(
|
33 |
-
prompt,
|
34 |
-
max_length=400,
|
35 |
-
do_sample=True,
|
36 |
-
temperature=0.7,
|
37 |
-
top_p=0.9
|
38 |
)
|
39 |
-
|
40 |
-
return result[0]['generated_text'].strip()
|
|
|
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 |
+
Context:
|
10 |
+
{context}
|
11 |
+
Question: {question}
|
12 |
+
Answer as a helpful paragraph:"""
|
13 |
+
|
14 |
+
inputs = tokenizer(prompt, return_tensors='pt', truncation=True, max_length=512)
|
15 |
+
outputs = model.generate(
|
16 |
+
**inputs,
|
17 |
+
max_new_tokens=100,
|
18 |
+
do_sample=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
)
|
20 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
|