Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -65,19 +65,24 @@ def generate_atomic_facts(kb, question):
|
|
| 65 |
Returns JSON: {"facts": [ ... ]}
|
| 66 |
"""
|
| 67 |
prompt = f"""
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
Knowledge Base:
|
| 71 |
-
{kb}
|
| 72 |
-
|
| 73 |
-
Question:
|
| 74 |
-
{question}
|
| 75 |
-
|
| 76 |
-
RULES:
|
| 77 |
-
- Return 1-5 short factual statements that directly answer the question.
|
| 78 |
-
- Output strictly in JSON format: {{"facts": ["fact1", "fact2", ...]}}
|
| 79 |
-
- Do not include unrelated events or explanations.
|
| 80 |
-
- Each fact should be self-contained.
|
| 81 |
"""
|
| 82 |
inputs = llm_tokenizer(prompt, return_tensors="pt", truncation=True).to(DEVICE)
|
| 83 |
outputs = llm_model.generate(
|
|
@@ -122,9 +127,17 @@ def evaluate_answer(answer, question, kb):
|
|
| 122 |
covered_all = True
|
| 123 |
for concept in schema["required_concepts"]:
|
| 124 |
if claims:
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
else:
|
| 129 |
best = 0.0
|
| 130 |
ok = False
|
|
@@ -143,7 +156,13 @@ def evaluate_answer(answer, question, kb):
|
|
| 143 |
for claim in claims:
|
| 144 |
for sent in kb_sents:
|
| 145 |
probs = softmax_logits(nli_model.predict([(sent, claim)]))
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
contradictions.append({
|
| 148 |
"claim": claim,
|
| 149 |
"sentence": sent,
|
|
|
|
| 65 |
Returns JSON: {"facts": [ ... ]}
|
| 66 |
"""
|
| 67 |
prompt = f"""
|
| 68 |
+
From the Knowledge Base, extract the character transformation of Matilda.
|
| 69 |
+
|
| 70 |
+
Rules:
|
| 71 |
+
- Identify INITIAL traits, CAUSAL EVENTS, and FINAL traits.
|
| 72 |
+
- Use short factual statements grounded ONLY in the knowledge base.
|
| 73 |
+
- Do NOT paraphrase the question.
|
| 74 |
+
- Return facts that can be checked independently.
|
| 75 |
+
|
| 76 |
+
Output strictly as JSON:
|
| 77 |
+
{
|
| 78 |
+
"facts": [
|
| 79 |
+
"Initially Matilda desired a luxurious life despite her humble background",
|
| 80 |
+
"She pretended to be wealthy and borrowed a necklace to attend the ball",
|
| 81 |
+
"She lost the borrowed necklace, causing long-term suffering",
|
| 82 |
+
"As a result of hardship, she became mature, humble, and grateful"
|
| 83 |
+
]
|
| 84 |
+
}
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
"""
|
| 87 |
inputs = llm_tokenizer(prompt, return_tensors="pt", truncation=True).to(DEVICE)
|
| 88 |
outputs = llm_model.generate(
|
|
|
|
| 127 |
covered_all = True
|
| 128 |
for concept in schema["required_concepts"]:
|
| 129 |
if claims:
|
| 130 |
+
|
| 131 |
+
probs = softmax_logits(nli_model.predict([(c, concept)]))
|
| 132 |
+
# index 2 = entailment for NLI DeBERTa
|
| 133 |
+
entailment = probs[2]
|
| 134 |
+
ok = entailment > 0.6
|
| 135 |
+
best = entailment
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
# scores = sim_model.predict([(concept, c) for c in claims])
|
| 139 |
+
# best = float(scores.max())
|
| 140 |
+
# ok = best >= SIM_THRESHOLD_REQUIRED
|
| 141 |
else:
|
| 142 |
best = 0.0
|
| 143 |
ok = False
|
|
|
|
| 156 |
for claim in claims:
|
| 157 |
for sent in kb_sents:
|
| 158 |
probs = softmax_logits(nli_model.predict([(sent, claim)]))
|
| 159 |
+
|
| 160 |
+
contradiction = probs[0]
|
| 161 |
+
entailment = probs[2]
|
| 162 |
+
|
| 163 |
+
if contradiction > 0.8 and entailment < 0.2:
|
| 164 |
+
# probs = softmax_logits(nli_model.predict([(sent, claim)]))
|
| 165 |
+
# if probs[0] > CONTRADICTION_THRESHOLD:
|
| 166 |
contradictions.append({
|
| 167 |
"claim": claim,
|
| 168 |
"sentence": sent,
|