Update app.py
Browse files
app.py
CHANGED
|
@@ -14,22 +14,11 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 14 |
import transformers
|
| 15 |
import torch
|
| 16 |
|
| 17 |
-
model = "tiiuae/falcon-40b-instruct"
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# print('flan read')
|
| 23 |
|
| 24 |
-
tokenizer = AutoTokenizer.from_pretrained(model)
|
| 25 |
-
pipeline = transformers.pipeline(
|
| 26 |
-
"text-generation",
|
| 27 |
-
model=model,
|
| 28 |
-
tokenizer=tokenizer,
|
| 29 |
-
torch_dtype=torch.bfloat16,
|
| 30 |
-
trust_remote_code=True,
|
| 31 |
-
device_map="auto",
|
| 32 |
-
)
|
| 33 |
|
| 34 |
|
| 35 |
ST_name = 'sentence-transformers/sentence-t5-base'
|
|
@@ -49,37 +38,18 @@ def get_context(query_text):
|
|
| 49 |
return context
|
| 50 |
|
| 51 |
def local_query(query, context):
|
| 52 |
-
|
| 53 |
-
# If you aren't sure please say i don't know.
|
| 54 |
-
# Context: {}
|
| 55 |
-
# Question: {}
|
| 56 |
-
# """.format(context, query)
|
| 57 |
-
|
| 58 |
-
# inputs = tokenizer(t5query, return_tensors="pt")
|
| 59 |
-
|
| 60 |
-
# outputs = model.generate(**inputs, max_new_tokens=20)
|
| 61 |
-
|
| 62 |
-
# return tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 63 |
-
|
| 64 |
-
context_query = """Using the available context, please answer the question.
|
| 65 |
If you aren't sure please say i don't know.
|
| 66 |
Context: {}
|
| 67 |
Question: {}
|
| 68 |
""".format(context, query)
|
| 69 |
-
|
| 70 |
-
sequences = pipeline(
|
| 71 |
-
context_query,
|
| 72 |
-
max_length=200,
|
| 73 |
-
do_sample=True,
|
| 74 |
-
top_k=10,
|
| 75 |
-
num_return_sequences=1,
|
| 76 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
# for seq in sequences:
|
| 80 |
-
# print(f"Result: {seq['generated_text']}")
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
|
|
|
|
| 14 |
import transformers
|
| 15 |
import torch
|
| 16 |
|
|
|
|
| 17 |
|
| 18 |
+
model_name = 'google/flan-t5-base'
|
| 19 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name, device_map='auto', offload_folder="offload")
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
ST_name = 'sentence-transformers/sentence-t5-base'
|
|
|
|
| 38 |
return context
|
| 39 |
|
| 40 |
def local_query(query, context):
|
| 41 |
+
t5query = """Using the available context, please answer the question.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
If you aren't sure please say i don't know.
|
| 43 |
Context: {}
|
| 44 |
Question: {}
|
| 45 |
""".format(context, query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
inputs = tokenizer(t5query, return_tensors="pt")
|
| 48 |
+
|
| 49 |
+
outputs = model.generate(**inputs, max_new_tokens=20)
|
| 50 |
+
|
| 51 |
+
return tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 52 |
+
|
| 53 |
|
| 54 |
|
| 55 |
|