import matplotlib.pyplot as plt import pandas as pd import numpy as np from transformers import pipeline import nltk from collections import Counter import re from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import cosine_similarity from utils.model_loader import load_qa_pipeline from utils.helpers import fig_to_html, df_to_html_table def question_answering_handler(context_text, question, answer_type="extractive", confidence_threshold=0.5): """Show question answering capabilities with comprehensive analysis.""" output_html = [] # Add result area container output_html.append('
') output_html.append('

Question Answering System

') output_html.append("""
Question Answering (QA) systems extract or generate answers to questions based on a given context or knowledge base. This system can handle both extractive (finding answers in text) and abstractive (generating new answers) approaches.
""") # Model info output_html.append("""

Models & Techniques Used:

""") try: # Validate inputs if not context_text or not context_text.strip(): output_html.append('
⚠️ Please provide a context text for question answering.
') output_html.append('
') return "\n".join(output_html) if not question or not question.strip(): output_html.append('
⚠️ Please provide a question to answer.
') output_html.append('') return "\n".join(output_html) # Display input information output_html.append('

Input Analysis

') context_stats = { "Context Length": len(context_text), "Word Count": len(context_text.split()), "Sentence Count": len(nltk.sent_tokenize(context_text)), "Question Length": len(question), "Question Words": len(question.split()) } stats_df = pd.DataFrame(list(context_stats.items()), columns=['Metric', 'Value']) output_html.append('

Input Statistics

') output_html.append(df_to_html_table(stats_df)) # Question Analysis output_html.append('

Question Analysis

') # Classify question type question_lower = question.lower().strip() question_type = classify_question_type(question_lower) output_html.append(f"""

Question Classification

Question: {question}

Type: {question_type['type']}

Expected Answer: {question_type['expected']}

Keywords: {', '.join(question_type['keywords'])}

""") # Extractive Question Answering using Transformer output_html.append('

Transformer-based Answer Extraction

') try: qa_pipeline = load_qa_pipeline() # Get answer from the model result = qa_pipeline(question=question, context=context_text) answer = result['answer'] confidence = result['score'] start_pos = result['start'] end_pos = result['end'] # Create confidence visualization fig, ax = plt.subplots(1, 1, figsize=(8, 4)) # Confidence bar colors = ['red' if confidence < 0.3 else 'orange' if confidence < 0.7 else 'green'] bars = ax.barh(['Confidence'], [confidence], color=colors[0]) ax.set_xlim(0, 1) ax.set_xlabel('Confidence Score') ax.set_title('Answer Confidence') # Add confidence threshold line ax.axvline(x=confidence_threshold, color='red', linestyle='--', label=f'Threshold ({confidence_threshold})') ax.legend() # Add value labels for bar in bars: width = bar.get_width() ax.text(width/2, bar.get_y() + bar.get_height()/2, f'{width:.3f}', ha='center', va='center', fontweight='bold') plt.tight_layout() output_html.append(fig_to_html(fig)) plt.close() # Display answer with context highlighting confidence_status = "High" if confidence >= 0.7 else "Medium" if confidence >= 0.3 else "Low" confidence_color = "#4CAF50" if confidence >= 0.7 else "#FF9800" if confidence >= 0.3 else "#F44336" output_html.append(f"""

📝 Extracted Answer

Answer: {answer}

Confidence: {confidence:.3f} ({confidence_status})

Position in Text: Characters {start_pos}-{end_pos}

""") # Show context with answer highlighted highlighted_context = highlight_answer_in_context(context_text, start_pos, end_pos) output_html.append(f"""

📄 Context with Highlighted Answer

{highlighted_context}
""") except Exception as e: output_html.append(f'
❌ Error in transformer QA: {str(e)}
') # Alternative: TF-IDF based answer extraction output_html.append('

TF-IDF Based Answer Extraction

') try: tfidf_answer = extract_answer_tfidf(context_text, question) output_html.append(f"""

🔍 TF-IDF Based Answer

Most Relevant Sentence: {tfidf_answer['sentence']}

Similarity Score: {tfidf_answer['score']:.3f}

Method: Cosine similarity between question and context sentences using TF-IDF vectors

""") except Exception as e: output_html.append(f'
❌ Error in TF-IDF QA: {str(e)}
') # Answer Quality Assessment output_html.append('

Answer Quality Assessment

') if 'confidence' in locals(): quality_metrics = assess_answer_quality(question, answer, confidence, context_text) # Create quality assessment visualization fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5)) # Quality metrics radar chart categories = list(quality_metrics.keys()) values = list(quality_metrics.values()) ax1.bar(categories, values, color=['#4CAF50', '#2196F3', '#FF9800', '#9C27B0']) ax1.set_ylim(0, 1) ax1.set_title('Answer Quality Metrics') ax1.set_ylabel('Score') plt.setp(ax1.get_xticklabels(), rotation=45, ha='right') # Overall quality score overall_score = sum(values) / len(values) quality_label = "Excellent" if overall_score >= 0.8 else "Good" if overall_score >= 0.6 else "Fair" if overall_score >= 0.4 else "Poor" ax2.pie([overall_score, 1-overall_score], labels=[f'{quality_label}\n({overall_score:.2f})', 'Room for Improvement'], colors=['#4CAF50', '#E0E0E0'], startangle=90) ax2.set_title('Overall Answer Quality') plt.tight_layout() output_html.append(fig_to_html(fig)) plt.close() # Quality metrics table quality_df = pd.DataFrame([ {'Metric': 'Confidence', 'Score': f"{quality_metrics['Confidence']:.3f}", 'Description': 'Model confidence in the answer'}, {'Metric': 'Relevance', 'Score': f"{quality_metrics['Relevance']:.3f}", 'Description': 'Semantic similarity to question'}, {'Metric': 'Completeness', 'Score': f"{quality_metrics['Completeness']:.3f}", 'Description': 'Answer length appropriateness'}, {'Metric': 'Context Match', 'Score': f"{quality_metrics['Context_Match']:.3f}", 'Description': 'How well answer fits context'} ]) output_html.append('

Quality Assessment Details

') output_html.append(df_to_html_table(quality_df)) # Question-Answer Pairs Suggestions output_html.append('

Suggested Follow-up Questions

') try: suggested_questions = generate_followup_questions(context_text, question, answer if 'answer' in locals() else "") output_html.append('
') output_html.append('

💡 Follow-up Questions:

') output_html.append('') output_html.append('
') except Exception as e: output_html.append(f'
❌ Error generating suggestions: {str(e)}
') except Exception as e: output_html.append(f'
❌ Unexpected error: {str(e)}
') output_html.append('') # Add About section at the end output_html.append(get_about_section()) return "\n".join(output_html) def classify_question_type(question): """Classify the type of question and expected answer format.""" question = question.lower().strip() # Question word patterns patterns = { 'what': {'type': 'Definition/Fact', 'expected': 'Entity, concept, or description'}, 'who': {'type': 'Person', 'expected': 'Person name or group'}, 'when': {'type': 'Time', 'expected': 'Date, time, or temporal expression'}, 'where': {'type': 'Location', 'expected': 'Place, location, or spatial reference'}, 'why': {'type': 'Reason/Cause', 'expected': 'Explanation or causal relationship'}, 'how': {'type': 'Method/Process', 'expected': 'Process, method, or manner'}, 'which': {'type': 'Selection', 'expected': 'Specific choice from options'}, 'how much': {'type': 'Quantity', 'expected': 'Numerical amount or quantity'}, 'how many': {'type': 'Count', 'expected': 'Numerical count'}, 'is': {'type': 'Yes/No', 'expected': 'Boolean answer'}, 'are': {'type': 'Yes/No', 'expected': 'Boolean answer'}, 'can': {'type': 'Ability/Possibility', 'expected': 'Yes/No with explanation'}, 'will': {'type': 'Future/Prediction', 'expected': 'Future state or prediction'}, 'did': {'type': 'Past Action', 'expected': 'Yes/No about past events'} } # Extract keywords from question words = question.split() keywords = [word for word in words if len(word) > 2 and word not in ['the', 'and', 'but', 'for']] # Determine question type for pattern, info in patterns.items(): if question.startswith(pattern): return { 'type': info['type'], 'expected': info['expected'], 'keywords': keywords[:5] # Top 5 keywords } # Default classification return { 'type': 'General', 'expected': 'Text span or explanation', 'keywords': keywords[:5] } def extract_answer_tfidf(context, question): """Extract answer using TF-IDF similarity.""" # Split context into sentences sentences = nltk.sent_tokenize(context) if len(sentences) == 0: return {'sentence': 'No sentences found', 'score': 0.0} # Create TF-IDF vectors vectorizer = TfidfVectorizer(stop_words='english', lowercase=True) # Combine question with sentences for vectorization texts = [question] + sentences tfidf_matrix = vectorizer.fit_transform(texts) # Calculate cosine similarity between question and each sentence question_vector = tfidf_matrix[0:1] sentence_vectors = tfidf_matrix[1:] similarities = cosine_similarity(question_vector, sentence_vectors).flatten() # Find the most similar sentence best_idx = np.argmax(similarities) best_sentence = sentences[best_idx] best_score = similarities[best_idx] return { 'sentence': best_sentence, 'score': best_score } def highlight_answer_in_context(context, start_pos, end_pos): """Highlight the answer span in the context.""" before = context[:start_pos] answer = context[start_pos:end_pos] after = context[end_pos:] highlighted = f'{before}{answer}{after}' return highlighted def assess_answer_quality(question, answer, confidence, context): """Assess the quality of the extracted answer.""" metrics = {} # Confidence score (from model) metrics['Confidence'] = confidence # Relevance (simple keyword overlap) question_words = set(question.lower().split()) answer_words = set(answer.lower().split()) overlap = len(question_words.intersection(answer_words)) metrics['Relevance'] = min(overlap / max(len(question_words), 1), 1.0) # Completeness (answer length appropriateness) answer_length = len(answer.split()) if answer_length == 0: metrics['Completeness'] = 0.0 elif answer_length < 3: metrics['Completeness'] = 0.6 elif answer_length <= 20: metrics['Completeness'] = 1.0 else: metrics['Completeness'] = 0.8 # Very long answers might be too verbose # Context match (how well the answer fits in context) answer_in_context = answer.lower() in context.lower() metrics['Context_Match'] = 1.0 if answer_in_context else 0.5 return metrics def generate_followup_questions(context, original_question, answer): """Generate relevant follow-up questions based on the context and answer.""" suggestions = [] # Extract key entities and concepts from context words = context.split() # Template-based question generation templates = [ f"What else can you tell me about {answer}?", "Can you provide more details about this topic?", "What are the implications of this information?", "How does this relate to other concepts mentioned?", "What evidence supports this answer?" ] # Add context-specific questions if "when" not in original_question.lower(): suggestions.append("When did this happen?") if "where" not in original_question.lower(): suggestions.append("Where did this take place?") if "why" not in original_question.lower(): suggestions.append("Why is this significant?") if "how" not in original_question.lower(): suggestions.append("How does this work?") # Combine and limit suggestions all_suggestions = templates + suggestions return all_suggestions[:5] # Return top 5 suggestions def qa_api_handler(context, question): """API handler for question answering that returns structured data.""" try: qa_pipeline = load_qa_pipeline() result = qa_pipeline(question=question, context=context) return { "answer": result['answer'], "confidence": result['score'], "start_position": result['start'], "end_position": result['end'], "success": True, "error": None } except Exception as e: return { "answer": "", "confidence": 0.0, "start_position": 0, "end_position": 0, "success": False, "error": str(e) } def process_question_with_context(context_text, question): """Process a question with the given context and return a formatted result.""" if not context_text or not context_text.strip(): return { "success": False, "error": "No context text provided", "html": '
⚠️ No context text provided.
' } if not question or not question.strip(): return { "success": False, "error": "No question provided", "html": '
⚠️ Please enter a question.
' } try: qa_pipeline = load_qa_pipeline() result = qa_pipeline(question=question, context=context_text) answer = result['answer'] confidence = result['score'] start_pos = result['start'] end_pos = result['end'] # Determine confidence level confidence_status = "High" if confidence >= 0.7 else "Medium" if confidence >= 0.3 else "Low" confidence_color = "#4CAF50" if confidence >= 0.7 else "#FF9800" if confidence >= 0.3 else "#F44336" # Highlight answer in context highlighted_context = highlight_answer_in_context(context_text, start_pos, end_pos) # Create formatted HTML result html_result = f"""
📝 Answer Found!

Question: {question}

Answer: {answer}

Confidence: {confidence:.3f} ({confidence_status})

📄 Context with Highlighted Answer:
{highlighted_context}
Quality Assessment:
  • Confidence: {confidence_status} ({confidence:.1%})
  • Answer found at position: {start_pos}-{end_pos}
  • Answer length: {len(answer.split())} words
""" return { "success": True, "answer": answer, "confidence": confidence, "html": html_result } except Exception as e: error_html = f'
❌ Error processing question: {str(e)}
' return { "success": False, "error": str(e), "html": error_html } def get_about_section(): """Generate the About Question Answering section""" return """

About Question Answering

What is Question Answering?

Question Answering (QA) is an NLP technique that automatically finds answers to questions posed in natural language. It involves understanding both the question and the context to extract or generate relevant answers.

Applications of Question Answering:
How It Works:

Our system uses transformer-based models to understand questions and find relevant answers in the provided context. It analyzes question types, extracts key information, and provides confidence scores for the answers found.

"""