File size: 1,735 Bytes
ac47d9a
 
 
 
 
 
f82f890
ac47d9a
 
f82f890
ac47d9a
 
 
 
 
 
 
0ab7ce5
ac47d9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ab7ce5
ac47d9a
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import T5Tokenizer, T5ForConditionalGeneration
from sentence_transformers import SentenceTransformer
from pinecone import Pinecone

device = 'cpu'

# Initialize Pinecone instance
pc = Pinecone(api_key='your-pinecone-api-key')

# Initialize FastAPI app
app = FastAPI()

# Initialize the models
def load_models():
    retriever = SentenceTransformer("flax-sentence-embeddings/all_datasets_v3_mpnet-base")
    tokenizer = T5Tokenizer.from_pretrained('t5-small')
    generator = T5ForConditionalGeneration.from_pretrained('t5-base').to(device)

    return retriever, generator, tokenizer

retriever, generator, tokenizer = load_models()

class QueryInput(BaseModel):
    input: str

@app.post("/predict")
def predict(query: QueryInput):
    query_text = query.input
    # Query Pinecone
    xq = retriever.encode([query_text]).tolist()
    xc = index.query(vector=xq, top_k=1, include_metadata=True)

    if 'matches' in xc and isinstance(xc['matches'], list):
        context = [m['metadata']['Output'] for m in xc['matches']]
        context_str = " ".join(context)
        formatted_query = f"answer the question: {query_text} context: {context_str}"
    else:
        context_str = ""
        formatted_query = f"answer the question: {query_text} context: {context_str}"

    # Generate answer using T5 model
    inputs = tokenizer.encode(formatted_query, return_tensors="pt", max_length=512, truncation=True).to(device)
    ids = generator.generate(inputs, num_beams=2, min_length=10, max_length=60, repetition_penalty=1.2)
    answer = tokenizer.decode(ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)

    return {"response": answer}