Updated app.py and added gradio
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import os
|
2 |
import re
|
3 |
import json
|
@@ -490,3 +491,34 @@ if __name__ == "__main__":
|
|
490 |
print(f"Could not evaluate answer similarity: {e}")
|
491 |
|
492 |
print("Session ended.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
import os
|
3 |
import re
|
4 |
import json
|
|
|
491 |
print(f"Could not evaluate answer similarity: {e}")
|
492 |
|
493 |
print("Session ended.")
|
494 |
+
|
495 |
+
|
496 |
+
# Initialize analyzer once
|
497 |
+
analyzer = AdvancedPDFAnalyzer()
|
498 |
+
documents = analyzer.extract_text_with_metadata("example.pdf") # Change path if needed
|
499 |
+
|
500 |
+
def ask_question_gradio(question: str):
|
501 |
+
if not question.strip():
|
502 |
+
return "Please enter a valid question."
|
503 |
+
try:
|
504 |
+
result = analyzer.answer_question(question, documents)
|
505 |
+
answer = result['answer']
|
506 |
+
confidence = result['confidence']
|
507 |
+
explanation = "\n\n".join(
|
508 |
+
f"πΉ {concept}: {desc}"
|
509 |
+
for concept, desc in result.get("explanations", {}).get("explanations", {}).items()
|
510 |
+
)
|
511 |
+
return f"π **Answer**: {answer}\n\nπ **Confidence**: {confidence:.2f}\n\nπ **Explanations**:\n{explanation}"
|
512 |
+
except Exception as e:
|
513 |
+
return f"β Error: {str(e)}"
|
514 |
+
|
515 |
+
# Gradio Interface
|
516 |
+
demo = gr.Interface(
|
517 |
+
fn=ask_question_gradio,
|
518 |
+
inputs=gr.Textbox(label="Ask a question about the PDF"),
|
519 |
+
outputs=gr.Markdown(label="Answer"),
|
520 |
+
title="Quandans AI - Ask Questions",
|
521 |
+
description="Enter a question based on the loaded PDF document. The system will provide an answer with confidence and concept explanations."
|
522 |
+
)
|
523 |
+
|
524 |
+
demo.launch()
|