edited again where it will just paste the output
Browse files- chatbot.py +4 -10
chatbot.py
CHANGED
|
@@ -2,12 +2,10 @@ from transformers import T5Tokenizer, T5ForConditionalGeneration
|
|
| 2 |
from sentence_transformers import SentenceTransformer
|
| 3 |
from pinecone import Pinecone
|
| 4 |
|
| 5 |
-
device = 'cpu'
|
| 6 |
|
| 7 |
-
# Initialize Pinecone instance
|
| 8 |
pc = Pinecone(api_key='89eeb534-da10-4068-92f7-12eddeabe1e5')
|
| 9 |
|
| 10 |
-
# Check if the index exists; if not, create it
|
| 11 |
index_name = 'abstractive-question-answering'
|
| 12 |
index = pc.Index(index_name)
|
| 13 |
|
|
@@ -23,14 +21,11 @@ def load_models():
|
|
| 23 |
retriever, generator, tokenizer = load_models()
|
| 24 |
|
| 25 |
def process_query(query):
|
| 26 |
-
# Query Pinecone
|
| 27 |
xq = retriever.encode([query]).tolist()
|
| 28 |
xc = index.query(vector=xq, top_k=1, include_metadata=True)
|
| 29 |
|
| 30 |
-
# Print the response to check the structure
|
| 31 |
print("Pinecone response:", xc)
|
| 32 |
|
| 33 |
-
# Check if 'matches' exists and is a list
|
| 34 |
if 'matches' in xc and isinstance(xc['matches'], list):
|
| 35 |
context = [m['metadata']['Output'] for m in xc['matches']]
|
| 36 |
context_str = " ".join(context)
|
|
@@ -47,11 +42,10 @@ def process_query(query):
|
|
| 47 |
inputs = tokenizer.encode(formatted_query, return_tensors="pt", max_length=512, truncation=True).to(device)
|
| 48 |
ids = generator.generate(inputs, num_beams=2, min_length=10, max_length=60, repetition_penalty=1.2)
|
| 49 |
answer = tokenizer.decode(ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
| 50 |
-
|
| 51 |
-
nli_keywords = ['not_equivalent', 'not_entailment', 'entailment', 'neutral']
|
| 52 |
|
| 53 |
-
|
|
|
|
| 54 |
if any(keyword in answer.lower() for keyword in nli_keywords):
|
| 55 |
-
return
|
| 56 |
|
| 57 |
return answer
|
|
|
|
| 2 |
from sentence_transformers import SentenceTransformer
|
| 3 |
from pinecone import Pinecone
|
| 4 |
|
| 5 |
+
device = 'cpu'
|
| 6 |
|
|
|
|
| 7 |
pc = Pinecone(api_key='89eeb534-da10-4068-92f7-12eddeabe1e5')
|
| 8 |
|
|
|
|
| 9 |
index_name = 'abstractive-question-answering'
|
| 10 |
index = pc.Index(index_name)
|
| 11 |
|
|
|
|
| 21 |
retriever, generator, tokenizer = load_models()
|
| 22 |
|
| 23 |
def process_query(query):
|
|
|
|
| 24 |
xq = retriever.encode([query]).tolist()
|
| 25 |
xc = index.query(vector=xq, top_k=1, include_metadata=True)
|
| 26 |
|
|
|
|
| 27 |
print("Pinecone response:", xc)
|
| 28 |
|
|
|
|
| 29 |
if 'matches' in xc and isinstance(xc['matches'], list):
|
| 30 |
context = [m['metadata']['Output'] for m in xc['matches']]
|
| 31 |
context_str = " ".join(context)
|
|
|
|
| 42 |
inputs = tokenizer.encode(formatted_query, return_tensors="pt", max_length=512, truncation=True).to(device)
|
| 43 |
ids = generator.generate(inputs, num_beams=2, min_length=10, max_length=60, repetition_penalty=1.2)
|
| 44 |
answer = tokenizer.decode(ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
nli_keywords = ['not_equivalent', 'not_entailment', 'entailment', 'neutral', 'not_enquiry']
|
| 47 |
+
|
| 48 |
if any(keyword in answer.lower() for keyword in nli_keywords):
|
| 49 |
+
return context_str
|
| 50 |
|
| 51 |
return answer
|