Update app.py
Browse files
app.py
CHANGED
@@ -93,7 +93,9 @@ class DetailedExplainer:
|
|
93 |
"text2text-generation",
|
94 |
model=explanation_model,
|
95 |
tokenizer=explanation_model,
|
96 |
-
device=device
|
|
|
|
|
97 |
)
|
98 |
|
99 |
def extract_concepts(self, text: str) -> list:
|
@@ -109,14 +111,12 @@ class DetailedExplainer:
|
|
109 |
|
110 |
def explain_concept(self, concept: str, context: str, min_accuracy: float = 0.50) -> str:
|
111 |
prompt = (
|
112 |
-
f"
|
113 |
-
|
114 |
-
f"
|
115 |
)
|
116 |
result = self.explainer(
|
117 |
prompt,
|
118 |
-
max_new_tokens=800,
|
119 |
-
min_length=80,
|
120 |
do_sample=False
|
121 |
)
|
122 |
return result[0]["generated_text"].strip()
|
@@ -125,15 +125,8 @@ class DetailedExplainer:
|
|
125 |
concepts = self.extract_concepts(text)
|
126 |
explanations = {}
|
127 |
for concept in concepts:
|
128 |
-
|
129 |
-
|
130 |
-
except Exception as e:
|
131 |
-
explanations[concept] = f"[Error explaining concept: {e}]"
|
132 |
-
return {
|
133 |
-
"answer_sentence": text.strip(),
|
134 |
-
"concepts": concepts,
|
135 |
-
"explanations": explanations
|
136 |
-
}
|
137 |
|
138 |
class AdvancedPDFAnalyzer:
|
139 |
def __init__(self):
|
@@ -193,7 +186,7 @@ class AdvancedPDFAnalyzer:
|
|
193 |
add_special_tokens=True,
|
194 |
return_tensors="pt",
|
195 |
max_length=512,
|
196 |
-
truncation=True
|
197 |
)
|
198 |
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
199 |
with torch.no_grad():
|
@@ -227,7 +220,6 @@ class AdvancedPDFAnalyzer:
|
|
227 |
best_answer['answer'] = f"[Low Confidence] {best_answer['answer']}"
|
228 |
return best_answer
|
229 |
|
230 |
-
# Instantiate analyzer once
|
231 |
analyzer = AdvancedPDFAnalyzer()
|
232 |
documents = analyzer.extract_text_with_metadata("example.pdf")
|
233 |
|
@@ -238,25 +230,13 @@ def ask_question_gradio(question: str):
|
|
238 |
result = analyzer.answer_question(question, documents)
|
239 |
answer = result['answer']
|
240 |
confidence = result['confidence']
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
markdown_output = f"\U0001F4CC **Answer:**\n{answer_sentence}\n\n"
|
247 |
-
markdown_output += f"\U0001F512 **Confidence:** {confidence:.2f}\n\n"
|
248 |
-
markdown_output += f"\U0001F4D8 **Concept Explanations:**\n"
|
249 |
-
|
250 |
-
if not explanations:
|
251 |
-
markdown_output += "\nNo key concepts found to explain."
|
252 |
-
else:
|
253 |
-
for concept, desc in explanations.items():
|
254 |
-
markdown_output += f"\n\U0001F539 **{concept}**\n{desc}\n"
|
255 |
-
|
256 |
-
return markdown_output
|
257 |
-
|
258 |
except Exception as e:
|
259 |
-
return f"
|
260 |
|
261 |
demo = gr.Interface(
|
262 |
fn=ask_question_gradio,
|
|
|
93 |
"text2text-generation",
|
94 |
model=explanation_model,
|
95 |
tokenizer=explanation_model,
|
96 |
+
device=device,
|
97 |
+
max_length=500,
|
98 |
+
max_new_tokens=800
|
99 |
)
|
100 |
|
101 |
def extract_concepts(self, text: str) -> list:
|
|
|
111 |
|
112 |
def explain_concept(self, concept: str, context: str, min_accuracy: float = 0.50) -> str:
|
113 |
prompt = (
|
114 |
+
f"The following sentence from a PDF is given:
|
115 |
+
\"{context}\"\n\n"
|
116 |
+
f"Now explain the concept '{concept}' mentioned above with at least {int(min_accuracy * 100)}% accuracy."
|
117 |
)
|
118 |
result = self.explainer(
|
119 |
prompt,
|
|
|
|
|
120 |
do_sample=False
|
121 |
)
|
122 |
return result[0]["generated_text"].strip()
|
|
|
125 |
concepts = self.extract_concepts(text)
|
126 |
explanations = {}
|
127 |
for concept in concepts:
|
128 |
+
explanations[concept] = self.explain_concept(concept, context)
|
129 |
+
return {"concepts": concepts, "explanations": explanations}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
class AdvancedPDFAnalyzer:
|
132 |
def __init__(self):
|
|
|
186 |
add_special_tokens=True,
|
187 |
return_tensors="pt",
|
188 |
max_length=512,
|
189 |
+
truncation=True
|
190 |
)
|
191 |
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
192 |
with torch.no_grad():
|
|
|
220 |
best_answer['answer'] = f"[Low Confidence] {best_answer['answer']}"
|
221 |
return best_answer
|
222 |
|
|
|
223 |
analyzer = AdvancedPDFAnalyzer()
|
224 |
documents = analyzer.extract_text_with_metadata("example.pdf")
|
225 |
|
|
|
230 |
result = analyzer.answer_question(question, documents)
|
231 |
answer = result['answer']
|
232 |
confidence = result['confidence']
|
233 |
+
explanation = "\n\n".join(
|
234 |
+
f"πΉ {concept}: {desc}"
|
235 |
+
for concept, desc in result.get("explanations", {}).get("explanations", {}).items()
|
236 |
+
)
|
237 |
+
return f"π **Answer**: {answer}\n\nπ **Confidence**: {confidence:.2f}\n\nπ **Explanations**:\n{explanation}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
except Exception as e:
|
239 |
+
return f"β Error: {str(e)}"
|
240 |
|
241 |
demo = gr.Interface(
|
242 |
fn=ask_question_gradio,
|