ArapCheruiyot's picture
Add debug documentation and testing tools for Election Watch deployment
3012298

A newer version of the Gradio SDK is available: 5.46.1

Upgrade

ElectionWatch-ng Debug Fixes

This document details the specific issues encountered and their solutions.

πŸ› Issues Identified

Issue 1: Text Analysis Not Generating Output

Problem:

  • Text analysis only showed "Source" and "Content" but no actual AI analysis
  • The LLM pipeline wasn't generating meaningful responses

Root Cause:

  • Improper prompt formatting for Llama 3.1 model
  • Missing chat template structure
  • Insufficient error handling for edge cases

Solution:

# OLD (Broken):
prompt_template = f"""You are an AI assistant...
Content: {content_to_analyze}
Response:"""

# NEW (Fixed):
prompt_template = f"""<|start_header_id|>system<|end_header_id|>
You are an expert AI assistant specializing in election monitoring...
<|eot_id|>

<|start_header_id|>user<|end_header_id|>
Please analyze this content: {content_to_analyze}<|eot_id|>

<|start_header_id|>assistant<|end_header_id|>
"""

Issue 2: OCR Processing Error

Problem:

Error during OCR processing: You need to specify either text or text_target

Root Cause:

  • TrOCR model expects specific parameter format for inference
  • Missing generation parameters for proper OCR inference

Solution:

# FIXED: Direct processor call for TrOCR
pixel_values = ocr_processor(pil_image, return_tensors="pt").pixel_values

# Enhanced generation with proper parameters
generated_ids = ocr_model.generate(
    pixel_values,
    max_length=512,
    num_beams=4,
    early_stopping=True,
    pad_token_id=ocr_processor.tokenizer.pad_token_id,
    eos_token_id=ocr_processor.tokenizer.eos_token_id
)

Issue 3: Text Formatting

Problem:

  • Text analysis output was compacted in paragraphs
  • Difficult to read and extract key information

Root Cause:

  • Prompt didn't specify clear formatting requirements
  • Model wasn't instructed to use bullet points and line breaks

Solution: Enhanced prompt template with explicit formatting instructions:

prompt_template = f"""
Provide a comprehensive analysis in the following format. Use bullet points and put each main point on a separate line:

1. **Misinformation Risk Level**: (Low/Medium/High)
   - Provide your assessment and reasoning

2. **Identified Narratives**: List specific misinformation themes from DISARM
   - List each narrative on a separate line
   - Reference specific DISARM techniques if applicable

Format your response with clear line breaks between sections and bullet points for readability.
"""

πŸ”§ Applied Fixes

1. Enhanced OCR Processing

def analyze_content(input_type, text_input, image_input):
    if input_type == "Image Input":
        try:
            # Convert to PIL Image if needed
            if hasattr(image_input, 'convert'):
                pil_image = image_input.convert('RGB')
            else:
                pil_image = Image.fromarray(image_input).convert('RGB')
            
            # FIXED: Use correct processor format
            inputs = ocr_processor(images=pil_image, return_tensors="pt")
            pixel_values = inputs.pixel_values
            
            # Generate with proper parameters
            with torch.no_grad():
                generated_ids = ocr_model.generate(pixel_values, max_length=512)
            
            extracted_text = ocr_processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
            # ... rest of processing

2. Improved LLM Text Generation

# FIXED: Proper Llama 3.1 chat format
prompt_template = f"""<|start_header_id|>system<|end_header_id|>
You are an expert AI assistant specializing in election monitoring and misinformation analysis.

Provide a comprehensive analysis in the following format:
1. **Misinformation Risk Level**: (Low/Medium/High)
2. **Identified Narratives**: List specific misinformation themes
3. **Potential Propagators**: Who might be spreading this
4. **Target Audience**: Who is this content targeting
5. **Verification Priority**: Which claims need immediate fact-checking
6. **Analysis Summary**: Overall assessment<|eot_id|>

<|start_header_id|>user<|end_header_id|>
Please analyze this content for election-related misinformation:

{content_to_analyze}<|eot_id|>

<|start_header_id|>assistant<|end_header_id|>
"""

# Enhanced generation parameters
outputs = llm_pipeline(
    prompt_template,
    max_new_tokens=800,  # Increased for detailed analysis
    temperature=0.7,
    top_p=0.9,
    repetition_penalty=1.1,
    do_sample=True,
    return_full_text=False,
    pad_token_id=llm_tokenizer.eos_token_id if llm_tokenizer else None,
    eos_token_id=llm_tokenizer.eos_token_id if llm_tokenizer else None
)

3. Enhanced Error Handling

# Better error messages and debugging
try:
    # ... processing code ...
except Exception as e:
    print(f"Error Details: {e}")
    import traceback
    traceback.print_exc()
    return f"""## Analysis Results

**Source**: {analysis_source}
**Content**: {content_to_analyze}

---

**Error**: Analysis failed due to: {str(e)}

This might be due to:
- Model inference issues
- Memory constraints  
- Content length too long
- Model loading problems

Please try:
1. Using shorter content
2. Restarting the interface
3. Using text input instead of image input
"""

πŸ§ͺ Testing the Fixes

Run the Debug Script

cd hf-space
python debug_test.py

This will test:

  • βœ… Environment setup
  • βœ… OCR model loading with correct format
  • βœ… LLM model loading and generation
  • βœ… Proper prompt formatting

Expected Test Results

=== OCR Model Test ===
βœ“ OCR processor loaded successfully
βœ“ OCR model loaded successfully
βœ“ OCR preprocessing successful, tensor shape: torch.Size([1, 3, 384, 384])
βœ“ OCR generation successful

=== LLM Model Test ===
βœ“ LLM tokenizer loaded successfully
βœ“ LLM model loaded successfully
βœ“ LLM pipeline created successfully
βœ“ LLM generation successful!
βœ“ Generated response has good length

πŸŽ‰ All tests passed! The updated app should work properly.

πŸš€ Deployment Steps

  1. Upload Fixed Files:

    # Upload the updated app.py to your HuggingFace Space
    git add app.py debug_test.py
    git commit -m "Fix OCR processing and LLM text generation issues"
    git push origin main
    
  2. Verify Automatic Restart:

    • HuggingFace Spaces will automatically restart
    • Check the build logs for any errors
    • Wait for "Running" status
  3. Test Both Input Types:

    • Text Input: Should now generate detailed analysis
    • Image Input: Should extract text and analyze without errors

πŸ“Š Expected Improvements

Before Fix:

  • Text Analysis: Only showed source and content, compacted output
  • Image Analysis: OCR processing error ("You need to specify either text or text_target")
  • User Experience: Confusing error messages, hard to read output

After Fix:

  • Text Analysis: βœ… Generates comprehensive misinformation analysis with bullet points
  • Image Analysis: βœ… Extracts text and provides full analysis with fallback methods
  • User Experience: βœ… Clear error messages, readable formatting, and debugging info

πŸ” Monitoring

Success Indicators:

  1. Text input generates analysis with properly formatted sections:

    • Misinformation Risk Level: (Low/Medium/High) with bullet points
    • Identified Narratives: DISARM-referenced themes on separate lines
    • Potential Propagators: Listed with motivations
    • Target Audience: Demographic groups and targeting strategy
    • Verification Priority: Prioritized claims with DISARM references
    • Analysis Summary: Concluding thoughts and recommended actions
  2. Image input works end-to-end:

    • OCR extraction succeeds with enhanced parameters
    • Fallback OCR method available if primary fails
    • Extracted text is analyzed with same formatting as text input
    • Full analysis is displayed with proper bullet points
  3. Error handling is informative:

    • Clear error messages with specific causes
    • Alternative processing attempts
    • Debugging information and troubleshooting steps
    • Actionable recommendations for users

Common Issues to Watch:

  • Memory constraints with large images
  • Model loading timeouts
  • Long content causing generation issues

πŸ“ Future Improvements

  1. Performance Optimization:

    • Implement model caching
    • Add request queuing for high load
    • Optimize memory usage
  2. Enhanced Features:

    • Support for multiple image formats
    • Batch processing capabilities
    • Custom analysis templates
  3. Better UX:

    • Progress indicators
    • Real-time status updates
    • Analysis confidence scores