Spaces:
Sleeping
Sleeping
Commit ·
8a6c8ae
1
Parent(s): bece185
Frontend Fix
Browse files
app.py
CHANGED
|
@@ -666,164 +666,209 @@ class HighPerformanceSystem:
|
|
| 666 |
# Initialize the system
|
| 667 |
high_performance_system = HighPerformanceSystem()
|
| 668 |
|
| 669 |
-
def
|
| 670 |
-
"""
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
if not questions:
|
| 686 |
-
return json.dumps({"error": "No valid questions found"}, indent=2)
|
| 687 |
-
|
| 688 |
-
doc_result = high_performance_system.process_document_optimized(document_url)
|
| 689 |
-
if not doc_result.get('success'):
|
| 690 |
-
return json.dumps({"error": f"Document processing failed: {doc_result.get('error')}"}, indent=2)
|
| 691 |
-
|
| 692 |
-
batch_result = high_performance_system.process_batch_queries_optimized(questions)
|
| 693 |
-
response = {
|
| 694 |
-
"answers": [result['answer'] for result in batch_result['answers']],
|
| 695 |
-
"metrics": {
|
| 696 |
-
"total_processing_time": batch_result['processing_time'],
|
| 697 |
-
"average_confidence": np.mean([result['confidence'] for result in batch_result['answers']]) if batch_result['answers'] else 0.0,
|
| 698 |
-
"total_tokens": sum(result['token_count'] for result in batch_result['answers'])
|
| 699 |
-
}
|
| 700 |
-
}
|
| 701 |
-
return json.dumps(response, indent=2)
|
| 702 |
-
|
| 703 |
-
except Exception as e:
|
| 704 |
-
logger.error(f"Submission processing error: {e}")
|
| 705 |
-
return json.dumps({"error": f"System error: {str(e)}"}, indent=2)
|
| 706 |
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
if not document_url.strip():
|
| 710 |
-
return "Error: Document URL is required"
|
| 711 |
-
if not question.strip():
|
| 712 |
-
return "Error: Question is required"
|
| 713 |
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
doc_result = high_performance_system.process_document_optimized(document_url)
|
| 717 |
-
if not doc_result.get('success'):
|
| 718 |
-
return f"Error: Document processing failed - {doc_result.get('error')}"
|
| 719 |
-
|
| 720 |
-
result = high_performance_system.process_single_query_optimized(question)
|
| 721 |
-
response = f"""Answer: {result['answer']}
|
| 722 |
|
| 723 |
-
Quality Metrics:
|
| 724 |
-
- Confidence Score: {result['confidence']:.3f}
|
| 725 |
-
- Processing Time: {result['processing_time']:.2f}s
|
| 726 |
-
- Token Usage: {result['token_count']} tokens
|
| 727 |
-
- Source Chunks: {result['source_chunks']}
|
| 728 |
|
| 729 |
-
|
| 730 |
-
return response
|
| 731 |
-
except Exception as e:
|
| 732 |
-
return f"Error: {str(e)}"
|
| 733 |
|
| 734 |
-
# Gradio Interface
|
| 735 |
with gr.Blocks(
|
| 736 |
-
|
| 737 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 738 |
css="""
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 749 |
"""
|
| 750 |
) as demo:
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
<
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
with gr.Row():
|
| 765 |
-
with gr.Column():
|
| 766 |
-
hack_url = gr.Textbox(
|
| 767 |
-
label="📄 Document URL (PDF/DOCX)",
|
| 768 |
-
placeholder="https://hackrx.blob.core.windows.net/assets/policy.pdf?...",
|
| 769 |
-
lines=2
|
| 770 |
-
)
|
| 771 |
-
hack_questions = gr.Textbox(
|
| 772 |
-
label="❓ Questions (JSON array or line-separated)",
|
| 773 |
-
placeholder='["What is the grace period?", "What is the waiting period for PED?"]',
|
| 774 |
-
lines=12
|
| 775 |
-
)
|
| 776 |
-
hack_submit = gr.Button("🚀 Process with High-Performance System", variant="primary", size="lg")
|
| 777 |
-
|
| 778 |
-
with gr.Column():
|
| 779 |
-
hack_output = gr.Textbox(
|
| 780 |
-
label="📊 High-Performance JSON Response",
|
| 781 |
-
lines=20,
|
| 782 |
-
max_lines=30
|
| 783 |
-
)
|
| 784 |
-
|
| 785 |
-
with gr.Tab("🔍 Single Query Analysis"):
|
| 786 |
-
gr.Markdown("### Detailed Single Query Processing with Performance Metrics")
|
| 787 |
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 808 |
|
| 809 |
-
|
| 810 |
-
|
|
|
|
| 811 |
inputs=[hack_url, hack_questions],
|
| 812 |
-
outputs=[hack_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
)
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
|
|
|
|
|
|
| 817 |
inputs=[single_url, single_question],
|
| 818 |
-
outputs=[single_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 819 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 820 |
|
| 821 |
-
# Mount the app
|
| 822 |
app = gr.mount_gradio_app(api_app, demo, path="/")
|
| 823 |
|
| 824 |
if __name__ == "__main__":
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
#
|
| 829 |
-
|
|
|
|
|
|
| 666 |
# Initialize the system
|
| 667 |
high_performance_system = HighPerformanceSystem()
|
| 668 |
|
| 669 |
+
def hackathon_wrapper(url, questions_text):
|
| 670 |
+
"""Wrapper to show processing status for the hackathon tab."""
|
| 671 |
+
# Show status message
|
| 672 |
+
yield gr.Markdown("⏳ Processing... Please wait.", visible=True)
|
| 673 |
+
|
| 674 |
+
# Call the original function
|
| 675 |
+
result = process_hackathon_submission(url, questions_text)
|
| 676 |
+
|
| 677 |
+
# Hide status message and return the final result
|
| 678 |
+
yield gr.Markdown(visible=False), result
|
| 679 |
+
|
| 680 |
+
def single_query_wrapper(url, question):
|
| 681 |
+
"""Wrapper to show processing status for the single query tab."""
|
| 682 |
+
# Show status message
|
| 683 |
+
yield gr.Markdown("⏳ Processing... Please wait.", visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 684 |
|
| 685 |
+
# Call the original function
|
| 686 |
+
result = process_single_question(url, question)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 687 |
|
| 688 |
+
# Hide status message and return the final result
|
| 689 |
+
yield gr.Markdown(visible=False), result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 690 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 691 |
|
| 692 |
+
# --- New and Immensely Improved Gradio Interface ---
|
|
|
|
|
|
|
|
|
|
| 693 |
|
|
|
|
| 694 |
with gr.Blocks(
|
| 695 |
+
theme=gr.themes.Monochrome(
|
| 696 |
+
primary_hue="indigo",
|
| 697 |
+
secondary_hue="blue",
|
| 698 |
+
neutral_hue="slate",
|
| 699 |
+
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 700 |
+
),
|
| 701 |
css="""
|
| 702 |
+
/* --- Custom CSS for a Professional Look --- */
|
| 703 |
+
:root {
|
| 704 |
+
--primary-color: #4f46e5;
|
| 705 |
+
--secondary-color: #1e40af;
|
| 706 |
+
--background-color: #f8fafc;
|
| 707 |
+
--card-background-color: #ffffff;
|
| 708 |
+
--text-color: #334155;
|
| 709 |
+
--border-color: #e2e8f0;
|
| 710 |
+
--shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
|
| 711 |
+
--border-radius: 12px;
|
| 712 |
+
}
|
| 713 |
+
.gradio-container { background-color: var(--background-color); }
|
| 714 |
+
.app-header {
|
| 715 |
+
text-align: center;
|
| 716 |
+
padding: 2rem;
|
| 717 |
+
color: white;
|
| 718 |
+
background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
|
| 719 |
+
border-radius: var(--border-radius);
|
| 720 |
+
margin-bottom: 2rem;
|
| 721 |
+
}
|
| 722 |
+
.app-header h1 { font-size: 2.5rem; font-weight: 700; margin-bottom: 0.5rem; }
|
| 723 |
+
.app-header p { font-size: 1.1rem; opacity: 0.9; }
|
| 724 |
+
.status-text { padding: 1rem !important; background-color: #e0e7ff !important; color: var(--primary-color) !important; border-radius: var(--border-radius) !important; text-align: center; }
|
| 725 |
+
.gr-box { border: none !important; box-shadow: var(--shadow) !important; border-radius: var(--border-radius) !important; }
|
| 726 |
+
.gr-button { border-radius: 8px !important; }
|
| 727 |
"""
|
| 728 |
) as demo:
|
| 729 |
+
|
| 730 |
+
# --- Header ---
|
| 731 |
+
with gr.Row():
|
| 732 |
+
gr.HTML("""
|
| 733 |
+
<div class="app-header">
|
| 734 |
+
<h1>🚀 High-Performance Document QA System</h1>
|
| 735 |
+
<p><strong>Powered by Qwen2.5-3B-Instruct + MPNet Embeddings + RAG Pipeline</strong></p>
|
| 736 |
+
<p>Optimized for insurance, legal, HR, and compliance documents.</p>
|
| 737 |
+
</div>
|
| 738 |
+
""")
|
| 739 |
+
|
| 740 |
+
# --- Main Content Area ---
|
| 741 |
+
with gr.Row(variant="panel"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 742 |
|
| 743 |
+
# --- Left Column: Inputs ---
|
| 744 |
+
with gr.Column(scale=1):
|
| 745 |
+
with gr.Tabs():
|
| 746 |
+
|
| 747 |
+
# --- Hackathon Submission Tab ---
|
| 748 |
+
with gr.Tab("🎯 Hackathon Submission", id=0):
|
| 749 |
+
with gr.Box():
|
| 750 |
+
gr.Markdown("### 1. Provide Document and Questions")
|
| 751 |
+
hack_url = gr.Textbox(
|
| 752 |
+
label="📄 Document URL (PDF/DOCX)",
|
| 753 |
+
placeholder="Enter the public URL of the document...",
|
| 754 |
+
lines=2
|
| 755 |
+
)
|
| 756 |
+
hack_questions = gr.Textbox(
|
| 757 |
+
label="❓ Questions (JSON array or one per line)",
|
| 758 |
+
placeholder='["What is the grace period?", "Is maternity covered?"]',
|
| 759 |
+
lines=8
|
| 760 |
+
)
|
| 761 |
+
|
| 762 |
+
gr.Examples(
|
| 763 |
+
examples=[
|
| 764 |
+
[
|
| 765 |
+
"https://hackrx.blob.core.windows.net/assets/policy.pdf?sp=r&st=2024-07-28T17:58:36Z&se=2024-08-05T01:58:36Z&spr=https&sv=2022-11-02&sr=b&sig=P3mH1m6xY95UPp5qT24l6j2l9V82p8vGEx2tTQP4fF0%3D",
|
| 766 |
+
'["What is the grace period for premium payment?","What is the waiting period for Pre-existing Diseases?","is maternity covered in this policy?"]'
|
| 767 |
+
]
|
| 768 |
+
],
|
| 769 |
+
inputs=[hack_url, hack_questions]
|
| 770 |
+
)
|
| 771 |
+
|
| 772 |
+
with gr.Row():
|
| 773 |
+
hack_clear_btn = gr.Button("Clear", variant="secondary")
|
| 774 |
+
hack_submit_btn = gr.Button("🚀 Process Submission", variant="primary")
|
| 775 |
+
|
| 776 |
+
hack_status = gr.Markdown(visible=False, elem_classes="status-text")
|
| 777 |
+
|
| 778 |
+
# --- Single Query Analysis Tab ---
|
| 779 |
+
with gr.Tab("🔍 Single Query Analysis", id=1):
|
| 780 |
+
with gr.Box():
|
| 781 |
+
gr.Markdown("### 1. Provide Document and a Question")
|
| 782 |
+
single_url = gr.Textbox(
|
| 783 |
+
label="📄 Document URL",
|
| 784 |
+
placeholder="Enter the public URL of the document...",
|
| 785 |
+
lines=2
|
| 786 |
+
)
|
| 787 |
+
single_question = gr.Textbox(
|
| 788 |
+
label="❓ Your Question",
|
| 789 |
+
placeholder="What is the waiting period for cataract surgery?",
|
| 790 |
+
lines=5
|
| 791 |
+
)
|
| 792 |
+
with gr.Row():
|
| 793 |
+
single_clear_btn = gr.Button("Clear", variant="secondary")
|
| 794 |
+
single_submit_btn = gr.Button("🔍 Get Detailed Answer", variant="primary")
|
| 795 |
+
|
| 796 |
+
single_status = gr.Markdown(visible=False, elem_classes="status-text")
|
| 797 |
+
|
| 798 |
+
# --- Right Column: Outputs ---
|
| 799 |
+
with gr.Column(scale=2):
|
| 800 |
+
with gr.Tabs():
|
| 801 |
+
with gr.Tab("✅ Results", id=2):
|
| 802 |
+
with gr.Box():
|
| 803 |
+
gr.Markdown("### 2. View the Results")
|
| 804 |
+
hack_output = gr.Textbox(
|
| 805 |
+
label="📊 Hackathon JSON Response",
|
| 806 |
+
lines=20,
|
| 807 |
+
max_lines=30,
|
| 808 |
+
interactive=False
|
| 809 |
+
)
|
| 810 |
+
single_output = gr.Textbox(
|
| 811 |
+
label="📋 Detailed Single Query Response",
|
| 812 |
+
lines=20,
|
| 813 |
+
max_lines=30,
|
| 814 |
+
interactive=False
|
| 815 |
+
)
|
| 816 |
+
|
| 817 |
+
# --- Event Handlers ---
|
| 818 |
|
| 819 |
+
# Hackathon Tab Logic
|
| 820 |
+
hack_submit_btn.click(
|
| 821 |
+
fn=hackathon_wrapper,
|
| 822 |
inputs=[hack_url, hack_questions],
|
| 823 |
+
outputs=[hack_status, hack_output],
|
| 824 |
+
# Hide the other output box
|
| 825 |
+
js="""
|
| 826 |
+
() => {
|
| 827 |
+
const singleQueryTab = document.getElementById('tab_single_query_output');
|
| 828 |
+
if (singleQueryTab) {
|
| 829 |
+
singleQueryTab.style.display = 'none';
|
| 830 |
+
}
|
| 831 |
+
const hackathonTab = document.getElementById('tab_hackathon_output');
|
| 832 |
+
if (hackathonTab) {
|
| 833 |
+
hackathonTab.style.display = 'block';
|
| 834 |
+
}
|
| 835 |
+
}
|
| 836 |
+
"""
|
| 837 |
)
|
| 838 |
+
hack_clear_btn.click(lambda: (None, None, None, gr.Markdown(visible=False)), outputs=[hack_url, hack_questions, hack_output, hack_status])
|
| 839 |
+
|
| 840 |
+
# Single Query Tab Logic
|
| 841 |
+
single_submit_btn.click(
|
| 842 |
+
fn=single_query_wrapper,
|
| 843 |
inputs=[single_url, single_question],
|
| 844 |
+
outputs=[single_status, single_output],
|
| 845 |
+
# Hide the other output box
|
| 846 |
+
js="""
|
| 847 |
+
() => {
|
| 848 |
+
const hackathonTab = document.getElementById('tab_hackathon_output');
|
| 849 |
+
if (hackathonTab) {
|
| 850 |
+
hackathonTab.style.display = 'none';
|
| 851 |
+
}
|
| 852 |
+
const singleQueryTab = document.getElementById('tab_single_query_output');
|
| 853 |
+
if (singleQueryTab) {
|
| 854 |
+
singleQueryTab.style.display = 'block';
|
| 855 |
+
}
|
| 856 |
+
}
|
| 857 |
+
"""
|
| 858 |
)
|
| 859 |
+
single_clear_btn.click(lambda: (None, None, None, gr.Markdown(visible=False)), outputs=[single_url, single_question, single_output, single_status])
|
| 860 |
+
|
| 861 |
+
# Logic to only show one output at a time based on which button was last clicked
|
| 862 |
+
# This requires giving the output Textbox components an `elem_id` to be targeted by JS
|
| 863 |
+
hack_output.elem_id = "tab_hackathon_output"
|
| 864 |
+
single_output.elem_id = "tab_single_query_output"
|
| 865 |
|
|
|
|
| 866 |
app = gr.mount_gradio_app(api_app, demo, path="/")
|
| 867 |
|
| 868 |
if __name__ == "__main__":
|
| 869 |
+
# We run this single, combined 'app' instance on port 7860.
|
| 870 |
+
# This is the correct way to run a combined app on a single public port.
|
| 871 |
+
# It ensures that both your API endpoints and your Gradio frontend
|
| 872 |
+
# are served from the same server and are both accessible.
|
| 873 |
+
import uvicorn
|
| 874 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|