Spaces:
Running on Zero

File size: 5,235 Bytes
f2baa25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
Inference Engine - Persistent Model Loading
Handles FLAN-T5, embeddings, and BART-MNLI verification
"""

import torch
from transformers import (
    T5Tokenizer, T5ForConditionalGeneration,
    AutoTokenizer, AutoModelForSequenceClassification
)
from sentence_transformers import SentenceTransformer
import numpy as np
from typing import Dict, List, Any, Optional
import logging

logger = logging.getLogger(__name__)

class InferenceEngine:
    """Persistent model inference engine."""

    def __init__(self):
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        logger.info(f"Using device: {self.device}")

        # Initialize models (loaded once)
        self._load_models()

    def _load_models(self):
        """Load all heavy models once."""
        logger.info("Loading FLAN-T5 model...")
        self.t5_tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
        self.t5_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base").to(self.device)

        logger.info("Loading BART-MNLI verifier...")
        self.nli_tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-mnli")
        self.nli_model = AutoModelForSequenceClassification.from_pretrained("facebook/bart-large-mnli").to(self.device)

        logger.info("Loading embedding model...")
        self.embed_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

        logger.info("All models loaded successfully!")

    def get_status(self) -> Dict[str, bool]:
        """Get model loading status."""
        return {
            "FLAN-T5": hasattr(self, 't5_model'),
            "BART-MNLI": hasattr(self, 'nli_model'),
            "Embeddings": hasattr(self, 'embed_model')
        }

    def generate_answer(self, question: str, evidence: List[Dict]) -> Dict[str, Any]:
        """Generate answer using FLAN-T5 and provided evidence."""

        if not evidence:
            return {"answer": "No evidence available to answer this question."}

        # Format evidence for the model
        context = "\n".join([doc.page_content for doc in evidence[:3]])  # Use top 3

        prompt = f"""
Answer the question based only on the provided context. If the answer is not in the context, say "I cannot answer this question based on the available information."

Context:
{context}

Question: {question}

Answer:"""

        try:
            inputs = self.t5_tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True).to(self.device)

            with torch.no_grad():
                outputs = self.t5_model.generate(
                    **inputs,
                    max_length=100,
                    num_beams=4,
                    early_stopping=True,
                    temperature=0.7
                )

            answer = self.t5_tokenizer.decode(outputs[0], skip_special_tokens=True).strip()

            return {"answer": answer}

        except Exception as e:
            logger.error(f"Answer generation failed: {e}")
            return {"answer": "Error generating answer."}

    def verify_answer(self, question: str, answer: str, evidence: List[Dict]) -> Dict[str, Any]:
        """Verify answer using BART-MNLI."""

        if not evidence:
            return {"status": "UNKNOWN", "trust_score": 0, "confidence": 0.0}

        try:
            # Combine all evidence
            context = " ".join([doc.page_content for doc in evidence])

            # Check if answer is entailed by the context
            premise = context[:1024]  # Limit context length
            hypothesis = f"The answer to '{question}' is '{answer}'"

            inputs = self.nli_tokenizer(
                premise, hypothesis,
                return_tensors="pt",
                truncation=True,
                max_length=512
            ).to(self.device)

            with torch.no_grad():
                outputs = self.nli_model(**inputs)
                logits = outputs.logits
                probs = torch.softmax(logits, dim=1)

            # Get probabilities for entailment vs contradiction
            entailment_prob = probs[0][2].item()  # Index 2 is entailment
            contradiction_prob = probs[0][0].item()  # Index 0 is contradiction

            # Calculate trust score
            trust_score = int(entailment_prob * 100)

            # Determine status
            if entailment_prob > 0.7:
                status = "VERIFIED"
            elif contradiction_prob > 0.6:
                status = "HALLUCINATION"
            else:
                status = "INCOMPLETE"

            return {
                "status": status,
                "trust_score": trust_score,
                "confidence": entailment_prob
            }

        except Exception as e:
            logger.error(f"Verification failed: {e}")
            return {"status": "UNKNOWN", "trust_score": 0, "confidence": 0.0}

    def embed_text(self, texts: List[str]) -> np.ndarray:
        """Generate embeddings for texts."""
        try:
            embeddings = self.embed_model.encode(texts, convert_to_numpy=True)
            return embeddings
        except Exception as e:
            logger.error(f"Embedding failed: {e}")
            return np.array([])