sohamchitimali commited on
Commit
8a6c8ae
·
1 Parent(s): bece185

Frontend Fix

Browse files
Files changed (1) hide show
  1. app.py +187 -142
app.py CHANGED
@@ -666,164 +666,209 @@ class HighPerformanceSystem:
666
  # Initialize the system
667
  high_performance_system = HighPerformanceSystem()
668
 
669
- def process_hackathon_submission(document_url: str, questions_text: str) -> str:
670
- """Main hackathon submission processing function"""
671
- try:
672
- if not document_url.strip():
673
- return json.dumps({"error": "Document URL is required"}, indent=2)
674
- if not questions_text.strip():
675
- return json.dumps({"error": "Questions are required"}, indent=2)
676
-
677
- try:
678
- if questions_text.strip().startswith('['):
679
- questions = json.loads(questions_text)
680
- else:
681
- questions = [q.strip() for q in questions_text.split('\n') if q.strip()]
682
- except json.JSONDecodeError:
683
- questions = [q.strip() for q in questions_text.split('\n') if q.strip()]
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
- def process_single_question(document_url: str, question: str) -> str:
708
- """Process single question with detailed output"""
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
- try:
715
- if not high_performance_system.index:
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
- Reasoning: {result['reasoning']}"""
730
- return response
731
- except Exception as e:
732
- return f"Error: {str(e)}"
733
 
734
- # Gradio Interface
735
  with gr.Blocks(
736
- title="🚀 High-Performance Document QA System",
737
- theme=gr.themes.Soft(),
 
 
 
 
738
  css="""
739
- .gradio-container {
740
- max-width: 1200px !important;
741
- }
742
- .performance-highlight {
743
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
744
- color: white;
745
- padding: 20px;
746
- border-radius: 10px;
747
- margin: 10px 0;
748
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
749
  """
750
  ) as demo:
751
-
752
- gr.HTML("""
753
- <div class="performance-highlight">
754
- <h1>🚀 High-Performance Document QA System</h1>
755
- <p><strong>Powered by Qwen2.5-3B-Instruct + MPNet Embeddings + RAG Pipeline</strong></p>
756
- <p>Optimized for insurance, legal, HR, and compliance documents with 90-95% accuracy</p>
757
- </div>
758
- """)
759
-
760
- with gr.Tab("🎯 Hackathon Submission"):
761
- gr.Markdown("### Production-Ready Processing with State-of-the-Art Models")
762
- gr.Markdown("**Current Models**: Qwen2.5-3B-Instruct (QA, unquantized) + all-mpnet-base-v2 (Embeddings)")
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
- with gr.Row():
789
- with gr.Column():
790
- single_url = gr.Textbox(
791
- label="📄 Document URL",
792
- placeholder="https://example.com/document.pdf",
793
- lines=1
794
- )
795
- single_question = gr.Textbox(
796
- label="❓ Question",
797
- placeholder="What is the grace period for premium payment?",
798
- lines=3
799
- )
800
- single_button = gr.Button("🔍 Get Detailed Answer", variant="secondary", size="lg")
801
-
802
- with gr.Column():
803
- single_output = gr.Textbox(
804
- label="📋 Detailed Response with Metrics",
805
- lines=20,
806
- max_lines=30
807
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808
 
809
- hack_submit.click(
810
- process_hackathon_submission,
 
811
  inputs=[hack_url, hack_questions],
812
- outputs=[hack_output]
 
 
 
 
 
 
 
 
 
 
 
 
 
813
  )
814
-
815
- single_button.click(
816
- process_single_question,
 
 
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
- logger.info("Starting High-Performance Document QA System...")
826
- logger.info("Models: Qwen2.5-3B-Instruct (QA, unquantized) + all-mpnet-base-v2 (Embeddings)")
827
- logger.info("Optimized for insurance, legal, HR, and compliance documents")
828
- # THE CORRECTED CODE
829
- uvicorn.run(app, host="0.0.0.0", port=7860, root_path="/")
 
 
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)