Spaces:
Runtime error
Runtime error
Download test_html_rendering.py from brightmind-ai/demo: direct link, hf CLI and curl.
- Browser
- Download file 6.22 kB
-
https://huggingface.co/spaces/brightmind-ai/demo/resolve/main/test_html_rendering.py
- Command line
-
hf download hf://spaces/brightmind-ai/demo/test_html_rendering.py
-
curl -L -o test_html_rendering.py https://huggingface.co/spaces/brightmind-ai/demo/resolve/main/test_html_rendering.py
6.22 kB
| #!/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"<div.*style.*border-bottom", "HTML fractions"), | |
| (r"<sup>", "Superscripts"), | |
| (r"<sub>", "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 = """ | |
| <h2>Test Math Content</h2> | |
| <p>Display math: \[ m = d \times v \]</p> | |
| <p>Inline math: The density \( d \) is calculated as \( d = \frac{m}{v} \).</p> | |
| <p>Simple fraction: Speed = 120/2 = 60 km/h</p> | |
| <p>LaTeX fraction: \frac{Distance}{Time}</p> | |
| <p>Chemical equation: 2H_2 + O_2 β 2H_2O</p> | |
| """ | |
| 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() | |