Spaces:
Runtime error
Runtime error
Update llm.py
Browse files
llm.py
CHANGED
@@ -1,20 +1,12 @@
|
|
1 |
-
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
def generate_answer(context, question):
|
7 |
-
prompt = f""
|
8 |
-
|
9 |
-
|
10 |
-
|
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()
|
|
|
1 |
+
# llm.py
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
|
4 |
+
# load model once
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
|
6 |
+
model = AutoModelForCausalLM.from_pretrained("distilgpt2")
|
7 |
|
8 |
def generate_answer(context, question):
|
9 |
+
prompt = f"Context:\n{context}\n\nQuestion: {question}\nAnswer:"
|
10 |
+
inputs = tokenizer.encode(prompt, return_tensors='pt', max_length=1024, truncation=True)
|
11 |
+
outputs = model.generate(inputs, max_new_tokens=50, do_sample=True)
|
12 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|