#!/usr/bin/env python3 """ Test script to verify HTML rendering and LaTeX parsing locally """ import sys import os sys.path.append(os.path.dirname(os.path.abspath(__file__))) from app import generate_content_with_progress, process_content_with_images, convert_math_to_html import gradio as gr def test_math_conversion(): """Test the convert_math_to_html function with various LaTeX expressions""" print("๐Ÿงช Testing LaTeX to HTML conversion...") test_cases = [ # LaTeX delimiters r"\[ m = d \times v \]", r"\( d \) = density", r"\( v \) = volume", # Fractions r"\frac{Distance}{Time}", r"\frac{120}{2}", r"Speed = \frac{Distance}{Time}", # Simple fractions "120/2", "5/10", # Superscripts and subscripts r"H_2O", r"2H_2 + O_2", r"x^2 + y^2", # Math symbols r"\pi \times r^2", r"\sqrt{25}", r"x \leq y", r"a \neq b" ] for i, test_case in enumerate(test_cases, 1): print(f"\n{i}. Input: {test_case}") result = convert_math_to_html(test_case) print(f" Output: {result}") print(f" Length: {len(result)} chars") def test_content_generation(): """Test content generation with the same parameters as the user's example""" print("\n๐ŸŽฏ Testing content generation with user parameters...") # Same parameters as user's example topic = "algebra" subject = "Science" grade_level = "6-8" difficulty = "Intermediate" content_type = "Worksheets" content_length = "Medium (3-5 pages)" print(f"Parameters:") print(f" Topic: {topic}") print(f" Subject: {subject}") print(f" Grade Level: {grade_level}") print(f" Difficulty: {difficulty}") print(f" Content Type: {content_type}") print(f" Length: {content_length}") try: # Generate content print("\n๐Ÿ“ Generating content...") content_generator = generate_content_with_progress(topic, subject, grade_level, difficulty, content_type, content_length) # Convert generator to string content = "" for chunk in content_generator: if isinstance(chunk, str): content += chunk elif isinstance(chunk, tuple) and len(chunk) >= 2: content += chunk[0] # First element is usually the content print(f"โœ… Content generated successfully!") print(f"๐Ÿ“Š Content length: {len(content)} characters") # Process content with images and math conversion print("\n๐Ÿ”ง Processing content with math conversion...") processed_content = process_content_with_images(content, topic, content_type) print(f"โœ… Content processed successfully!") print(f"๐Ÿ“Š Processed length: {len(processed_content)} characters") # Save to file for inspection with open("test_output.html", "w", encoding="utf-8") as f: f.write(processed_content) print(f"๐Ÿ’พ Content saved to test_output.html") # Show a sample of the content print(f"\n๐Ÿ“„ Sample of generated content (first 500 chars):") print("-" * 50) print(processed_content[:500]) print("-" * 50) # Check for specific LaTeX patterns print(f"\n๐Ÿ” Checking for LaTeX patterns:") patterns_to_check = [ (r"\\\[", "Display math delimiters"), (r"\\\(", "Inline math delimiters"), (r"\\frac", "LaTeX fractions"), (r"\d+/\d+", "Simple fractions"), (r"", "Superscripts"), (r"", "Subscripts") ] for pattern, description in patterns_to_check: import re matches = re.findall(pattern, processed_content) print(f" {description}: {len(matches)} matches") if matches and len(matches) <= 3: # Show first few matches print(f" Examples: {matches[:3]}") return processed_content except Exception as e: print(f"โŒ Error during content generation: {e}") import traceback traceback.print_exc() return None def test_gradio_rendering(): """Test how the content would render in Gradio""" print("\n๐ŸŽจ Testing Gradio HTML rendering...") # Create a simple Gradio interface to test rendering def test_render(content): return content # Test with sample content test_content = """

Test Math Content

Display math: \[ m = d \times v \]

Inline math: The density \( d \) is calculated as \( d = \frac{m}{v} \).

Simple fraction: Speed = 120/2 = 60 km/h

LaTeX fraction: \frac{Distance}{Time}

Chemical equation: 2H_2 + O_2 โ†’ 2H_2O

""" print("Original test content:") print(test_content) # Convert math expressions converted_content = convert_math_to_html(test_content) print("\nConverted content:") print(converted_content) # Save converted content with open("test_gradio_output.html", "w", encoding="utf-8") as f: f.write(converted_content) print("๐Ÿ’พ Converted content saved to test_gradio_output.html") def main(): """Main test function""" print("๐Ÿš€ Starting HTML rendering tests...") print("=" * 60) # Test 1: Math conversion function test_math_conversion() print("\n" + "=" * 60) # Test 2: Content generation content = test_content_generation() print("\n" + "=" * 60) # Test 3: Gradio rendering test_gradio_rendering() print("\n" + "=" * 60) print("โœ… All tests completed!") print("\n๐Ÿ“ Check these files:") print(" - test_output.html (full generated content)") print(" - test_gradio_output.html (converted test content)") print("\n๐ŸŒ Open these files in a web browser to see the rendered output!") if __name__ == "__main__": main()