Spaces:
Sleeping
Sleeping
Merge fix-image-generation branch with image display fixes
Browse files
README.md
CHANGED
|
@@ -1,46 +1,279 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: BrightMind AI - Educational Content Generator
|
| 3 |
-
emoji: 🧠
|
| 4 |
-
colorFrom: blue
|
| 5 |
-
colorTo: purple
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.44.1
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
license: mit
|
| 11 |
-
short_description: AI-powered educational content generation platform
|
| 12 |
-
---
|
| 13 |
-
|
| 14 |
# 🧠 BrightMind AI - Educational Content Generator
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
- **Educational Algorithms**: Fallback system based on proven teaching methodologies
|
| 22 |
-
- **Grade-Appropriate Content**: Adapts to K-12 learning levels
|
| 23 |
-
- **Subject-Specific Materials**: Tailored for Science, Math, History, English, and more
|
| 24 |
-
- **Quiz Generation**: Create custom quizzes with multiple question types
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
2. **Select Subject**: Choose from Science, Math, History, English, Geography, or Art
|
| 30 |
-
3. **Choose Grade Level**: K-2, 3-5, 6-8, or 9-12
|
| 31 |
-
4. **Set Duration**: 30-180 minutes
|
| 32 |
-
5. **Select Difficulty**: Beginner, Intermediate, or Advanced
|
| 33 |
-
6. **Generate**: Click to create your lesson plan or quiz
|
| 34 |
|
| 35 |
-
|
| 36 |
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
---
|
| 44 |
|
| 45 |
-
*BrightMind AI - Empowering educators with intelligent tools*
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# 🧠 BrightMind AI - Educational Content Generator
|
| 2 |
|
| 3 |
+
[](https://python.org)
|
| 4 |
+
[](https://gradio.app)
|
| 5 |
+
[](LICENSE)
|
| 6 |
+
|
| 7 |
+
An intelligent educational platform that generates comprehensive lesson plans, quizzes, and educational content using AI and proven educational algorithms. BrightMind AI empowers educators with intelligent tools to create engaging, grade-appropriate content across all subjects.
|
| 8 |
+
|
| 9 |
+
## ✨ Features
|
| 10 |
+
|
| 11 |
+
### 🤖 AI-Powered Content Generation
|
| 12 |
+
- **Hugging Face Integration**: Uses state-of-the-art language models for enhanced content creation
|
| 13 |
+
- **Intelligent Fallback**: Educational algorithms ensure content generation even without API access
|
| 14 |
+
- **Context-Aware**: Adapts content based on subject, grade level, and difficulty
|
| 15 |
+
|
| 16 |
+
### 📚 Educational Content Types
|
| 17 |
+
- **Lesson Plans**: Comprehensive, structured lesson plans with objectives, activities, and assessments
|
| 18 |
+
- **Quizzes**: Custom quizzes with multiple question types (Multiple Choice, True/False, Short Answer, Fill-in-the-Blank)
|
| 19 |
+
- **Worksheets**: Interactive educational materials with visual elements
|
| 20 |
+
- **Study Guides**: Structured study materials for various subjects
|
| 21 |
+
|
| 22 |
+
### 🎯 Grade-Appropriate Content
|
| 23 |
+
- **K-2**: Early elementary content with visual aids and simple concepts
|
| 24 |
+
- **3-5**: Upper elementary with hands-on activities
|
| 25 |
+
- **6-8**: Middle school with analytical thinking
|
| 26 |
+
- **9-12**: High school with advanced concepts and critical thinking
|
| 27 |
+
|
| 28 |
+
### 📖 Subject Coverage
|
| 29 |
+
- **Science**: Biology, Chemistry, Physics, Earth Science
|
| 30 |
+
- **Mathematics**: Algebra, Geometry, Statistics, Calculus
|
| 31 |
+
- **History**: World History, US History, Ancient Civilizations
|
| 32 |
+
- **English**: Literature, Writing, Grammar, Reading Comprehension
|
| 33 |
+
- **Geography**: Physical and Human Geography
|
| 34 |
+
- **Art**: Art History, Techniques, Creative Projects
|
| 35 |
+
|
| 36 |
+
### 🎨 Advanced Features
|
| 37 |
+
- **Image Generation**: AI-powered educational illustrations using Hugging Face models
|
| 38 |
+
- **Math Rendering**: LaTeX to HTML conversion for mathematical expressions
|
| 39 |
+
- **Progress Tracking**: Real-time generation progress with visual feedback
|
| 40 |
+
- **Feedback System**: Built-in feedback collection and database storage
|
| 41 |
+
- **Responsive UI**: Modern, intuitive interface built with Gradio
|
| 42 |
+
|
| 43 |
+
## 🚀 Quick Start
|
| 44 |
+
|
| 45 |
+
### Prerequisites
|
| 46 |
+
- Python 3.8 or higher
|
| 47 |
+
- pip package manager
|
| 48 |
+
|
| 49 |
+
### Installation
|
| 50 |
+
|
| 51 |
+
1. **Clone the repository**
|
| 52 |
+
```bash
|
| 53 |
+
git clone https://github.com/yourusername/brightmind-ai-clone.git
|
| 54 |
+
cd brightmind-ai-clone
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
2. **Install dependencies**
|
| 58 |
+
```bash
|
| 59 |
+
pip install -r requirements.txt
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
3. **Set up environment variables (optional)**
|
| 63 |
+
```bash
|
| 64 |
+
export HUGGINGFACE_TOKEN="your_hf_token_here"
|
| 65 |
+
export SMTP_USERNAME="your_email@example.com"
|
| 66 |
+
export SMTP_PASSWORD="your_app_password"
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
4. **Run the application**
|
| 70 |
+
```bash
|
| 71 |
+
python app.py
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
5. **Open your browser**
|
| 75 |
+
Navigate to `http://localhost:7860` to access the application
|
| 76 |
+
|
| 77 |
+
## 🔧 Configuration
|
| 78 |
+
|
| 79 |
+
### Environment Variables
|
| 80 |
+
|
| 81 |
+
| Variable | Description | Required | Default |
|
| 82 |
+
|----------|-------------|----------|---------|
|
| 83 |
+
| `HUGGINGFACE_TOKEN` | Hugging Face API token for AI content generation | No | None |
|
| 84 |
+
| `SMTP_USERNAME` | Email username for feedback notifications | No | "" |
|
| 85 |
+
| `SMTP_PASSWORD` | Email password for feedback notifications | No | "" |
|
| 86 |
+
|
| 87 |
+
### API Configuration
|
| 88 |
+
|
| 89 |
+
The application automatically detects your Hugging Face token and uses it for enhanced content generation. Without a token, the app falls back to educational algorithms.
|
| 90 |
+
|
| 91 |
+
**Getting a Hugging Face Token:**
|
| 92 |
+
1. Visit [Hugging Face](https://huggingface.co)
|
| 93 |
+
2. Create an account or sign in
|
| 94 |
+
3. Go to Settings → Access Tokens
|
| 95 |
+
4. Create a new token with "Read" permissions
|
| 96 |
+
5. Set the token as an environment variable
|
| 97 |
+
|
| 98 |
+
## 📖 Usage Guide
|
| 99 |
+
|
| 100 |
+
### Creating Lesson Plans
|
| 101 |
+
|
| 102 |
+
1. **Enter Topic**: Describe what you want to teach (e.g., "Photosynthesis", "World War II", "Quadratic Equations")
|
| 103 |
+
2. **Select Subject**: Choose from the dropdown menu
|
| 104 |
+
3. **Choose Grade Level**: Select appropriate grade range
|
| 105 |
+
4. **Set Duration**: Specify lesson length (30-180 minutes)
|
| 106 |
+
5. **Select Difficulty**: Choose Beginner, Intermediate, or Advanced
|
| 107 |
+
6. **Generate**: Click the generate button and watch the AI create your lesson plan
|
| 108 |
+
|
| 109 |
+
### Creating Quizzes
|
| 110 |
+
|
| 111 |
+
1. **Enter Topic**: Specify the quiz subject
|
| 112 |
+
2. **Select Subject**: Choose the academic subject
|
| 113 |
+
3. **Choose Grade Level**: Select appropriate grade range
|
| 114 |
+
4. **Set Question Count**: Specify number of questions (1-20)
|
| 115 |
+
5. **Select Question Types**: Choose from available question formats
|
| 116 |
+
6. **Generate**: Create your custom quiz
|
| 117 |
+
|
| 118 |
+
### Content Types Available
|
| 119 |
|
| 120 |
+
- **Lesson Plans**: Complete structured lessons with objectives, activities, materials, and assessments
|
| 121 |
+
- **Quizzes**: Interactive assessments with multiple question types
|
| 122 |
+
- **Worksheets**: Practice materials and exercises
|
| 123 |
+
- **Study Guides**: Comprehensive review materials
|
| 124 |
|
| 125 |
+
## 🏗️ Project Structure
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
```
|
| 128 |
+
brightmind-ai-clone/
|
| 129 |
+
├── app.py # Main application file
|
| 130 |
+
├── config.py # Configuration and environment variables
|
| 131 |
+
├── ai_services.py # AI/ML functionality and API calls
|
| 132 |
+
├── content_generators.py # Content generation logic
|
| 133 |
+
├── database.py # Database operations and feedback storage
|
| 134 |
+
├── email_service.py # Email notification system
|
| 135 |
+
├── ui_components.py # UI components and styling
|
| 136 |
+
├── utils.py # Utility functions and helpers
|
| 137 |
+
├── algebra_solver.py # Mathematical problem solving
|
| 138 |
+
├── requirements.txt # Python dependencies
|
| 139 |
+
├── requirements_simple.txt # Minimal dependencies
|
| 140 |
+
├── requirements_complex.txt # Full feature dependencies
|
| 141 |
+
├── generated_images/ # AI-generated educational images
|
| 142 |
+
├── icons/ # Application icons and assets
|
| 143 |
+
├── test_*.py # Test files for various components
|
| 144 |
+
└── README.md # This documentation
|
| 145 |
+
```
|
| 146 |
|
| 147 |
+
## 🔌 API Integration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
### Hugging Face Models
|
| 150 |
|
| 151 |
+
The application uses several Hugging Face models:
|
| 152 |
|
| 153 |
+
- **Content Generation**: `deepseek-ai/DeepSeek-V3-0324`
|
| 154 |
+
- **Image Generation**:
|
| 155 |
+
- `stabilityai/stable-diffusion-xl-base-1.0`
|
| 156 |
+
- `stabilityai/stable-diffusion-3-medium-diffusers`
|
| 157 |
|
| 158 |
+
### API Endpoints
|
| 159 |
+
|
| 160 |
+
- **Content Generation**: `https://router.huggingface.co/v1/chat/completions`
|
| 161 |
+
- **Image Generation**: `https://api-inference.huggingface.co/models/{model_name}`
|
| 162 |
+
|
| 163 |
+
## 🧪 Testing
|
| 164 |
+
|
| 165 |
+
The project includes comprehensive test files:
|
| 166 |
+
|
| 167 |
+
- `test_html_rendering.py`: Tests HTML rendering and LaTeX conversion
|
| 168 |
+
- `test_image_api.py`: Tests image generation functionality
|
| 169 |
+
- `test_specific_content.py`: Tests content generation with specific parameters
|
| 170 |
+
|
| 171 |
+
**Running Tests:**
|
| 172 |
+
```bash
|
| 173 |
+
python test_html_rendering.py
|
| 174 |
+
python test_image_api.py
|
| 175 |
+
python test_specific_content.py
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
## 📊 Database Schema
|
| 179 |
+
|
| 180 |
+
The application uses SQLite for feedback storage:
|
| 181 |
+
|
| 182 |
+
```sql
|
| 183 |
+
CREATE TABLE feedback (
|
| 184 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 185 |
+
timestamp TEXT NOT NULL,
|
| 186 |
+
feedback_type TEXT NOT NULL,
|
| 187 |
+
rating INTEGER NOT NULL,
|
| 188 |
+
comments TEXT NOT NULL,
|
| 189 |
+
user_email TEXT,
|
| 190 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
| 191 |
+
);
|
| 192 |
+
```
|
| 193 |
+
|
| 194 |
+
## 🎨 Customization
|
| 195 |
+
|
| 196 |
+
### UI Themes
|
| 197 |
+
The application uses Gradio's Soft theme by default. You can customize the theme by modifying the `gr.themes.Soft()` parameter in `app.py`.
|
| 198 |
+
|
| 199 |
+
### Content Styling
|
| 200 |
+
Educational content is styled with custom CSS for better readability and visual appeal. Styles are defined in the `ui_components.py` file.
|
| 201 |
+
|
| 202 |
+
### Image Generation
|
| 203 |
+
Customize image generation by modifying prompts in `ai_services.py` or adding new image sources in the fallback system.
|
| 204 |
+
|
| 205 |
+
## 🚀 Deployment
|
| 206 |
+
|
| 207 |
+
### Local Development
|
| 208 |
+
```bash
|
| 209 |
+
python app.py
|
| 210 |
+
```
|
| 211 |
+
|
| 212 |
+
### Production Deployment
|
| 213 |
+
1. Set up environment variables
|
| 214 |
+
2. Install production dependencies
|
| 215 |
+
3. Configure reverse proxy (nginx/Apache)
|
| 216 |
+
4. Set up SSL certificates
|
| 217 |
+
5. Deploy using your preferred method (Docker, PM2, etc.)
|
| 218 |
+
|
| 219 |
+
### Docker Deployment
|
| 220 |
+
```dockerfile
|
| 221 |
+
FROM python:3.9-slim
|
| 222 |
+
WORKDIR /app
|
| 223 |
+
COPY requirements.txt .
|
| 224 |
+
RUN pip install -r requirements.txt
|
| 225 |
+
COPY . .
|
| 226 |
+
EXPOSE 7860
|
| 227 |
+
CMD ["python", "app.py"]
|
| 228 |
+
```
|
| 229 |
+
|
| 230 |
+
## 🤝 Contributing
|
| 231 |
+
|
| 232 |
+
We welcome contributions! Please follow these steps:
|
| 233 |
+
|
| 234 |
+
1. Fork the repository
|
| 235 |
+
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
|
| 236 |
+
3. Commit your changes (`git commit -m 'Add amazing feature'`)
|
| 237 |
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
| 238 |
+
5. Open a Pull Request
|
| 239 |
+
|
| 240 |
+
### Development Setup
|
| 241 |
+
```bash
|
| 242 |
+
git clone https://github.com/yourusername/brightmind-ai-clone.git
|
| 243 |
+
cd brightmind-ai-clone
|
| 244 |
+
pip install -r requirements.txt
|
| 245 |
+
python app.py
|
| 246 |
+
```
|
| 247 |
+
|
| 248 |
+
## 📝 License
|
| 249 |
+
|
| 250 |
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
| 251 |
+
|
| 252 |
+
## 🙏 Acknowledgments
|
| 253 |
+
|
| 254 |
+
- [Gradio](https://gradio.app) for the amazing UI framework
|
| 255 |
+
- [Hugging Face](https://huggingface.co) for providing access to state-of-the-art AI models
|
| 256 |
+
- [Stability AI](https://stability.ai) for image generation capabilities
|
| 257 |
+
- The educational community for inspiration and feedback
|
| 258 |
+
|
| 259 |
+
## 📞 Support
|
| 260 |
+
|
| 261 |
+
- **Issues**: [GitHub Issues](https://github.com/yourusername/brightmind-ai-clone/issues)
|
| 262 |
+
- **Discussions**: [GitHub Discussions](https://github.com/yourusername/brightmind-ai-clone/discussions)
|
| 263 |
+
- **Email**: mahdi@brightmind-ai.com
|
| 264 |
+
|
| 265 |
+
## 🔄 Changelog
|
| 266 |
+
|
| 267 |
+
### Version 1.0.0
|
| 268 |
+
- Initial release with AI-powered content generation
|
| 269 |
+
- Support for lesson plans and quizzes
|
| 270 |
+
- Hugging Face API integration
|
| 271 |
+
- Image generation capabilities
|
| 272 |
+
- Feedback system with database storage
|
| 273 |
+
- Responsive UI with Gradio
|
| 274 |
|
| 275 |
---
|
| 276 |
|
| 277 |
+
*BrightMind AI - Empowering educators with intelligent tools* 🧠✨
|
| 278 |
+
|
| 279 |
+
**Last Updated**: September 2024
|
app.py
CHANGED
|
@@ -1740,16 +1740,10 @@ def fix_malformed_image_html(content):
|
|
| 1740 |
"""Fix malformed image HTML tags in content"""
|
| 1741 |
import re
|
| 1742 |
|
| 1743 |
-
#
|
| 1744 |
-
styledata_pattern = r'<div\s+styledata:image/png;base64,([A-Za-z0-9+/=]+)'
|
| 1745 |
-
def fix_styledata(match):
|
| 1746 |
-
base64_data = match.group(1)
|
| 1747 |
-
return f'<img src="data:image/png;base64,{base64_data}" alt="Generated image" style="max-width: 100%; height: auto; border-radius: 8px; margin: 20px 0;">'
|
| 1748 |
-
|
| 1749 |
-
content = re.sub(styledata_pattern, fix_styledata, content)
|
| 1750 |
-
|
| 1751 |
-
# Pattern to find malformed img tags like: <img srcStep-by-step...>
|
| 1752 |
malformed_pattern = r'<img\s+src([^>]*?)"([^"]*?)"([^>]*?)>'
|
|
|
|
|
|
|
| 1753 |
|
| 1754 |
def fix_img_tag(match):
|
| 1755 |
# Extract the parts
|
|
@@ -1771,8 +1765,32 @@ def fix_malformed_image_html(content):
|
|
| 1771 |
safe_description = description.replace('"', '"').replace("'", ''')
|
| 1772 |
return f'<div style="border: 2px dashed #667eea; padding: 20px; margin: 20px 0; text-align: center; background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%); border-radius: 12px;"><div style="font-size: 48px; margin-bottom: 10px;">🖼️</div><div style="color: #667eea; font-weight: bold; margin-bottom: 10px;">Image: {safe_description}</div><div style="color: #666; font-size: 14px;">Visual content would appear here</div></div>'
|
| 1773 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1774 |
# Fix malformed img tags
|
| 1775 |
fixed_content = re.sub(malformed_pattern, fix_img_tag, content)
|
|
|
|
|
|
|
| 1776 |
|
| 1777 |
return fixed_content
|
| 1778 |
|
|
@@ -1930,8 +1948,8 @@ AGENT INSTRUCTIONS:
|
|
| 1930 |
9. IMPORTANT: DO NOT USE LaTeX expressions. Use simple HTML/markdown formatting instead:
|
| 1931 |
- For fractions: use "a/b" or "a over b" instead of \frac{{a}}{{b}}
|
| 1932 |
- For math: use plain text like "Speed = Distance/Time" instead of \text{{Speed}} = \frac{{\text{{Distance}}}}{{\text{{Time}}}}
|
| 1933 |
-
- For fill-in-blanks: use "_____" instead of
|
| 1934 |
-
- For answer boxes: use "[ANSWER: ___]" instead of
|
| 1935 |
- For subscripts: use "H2O" instead of H_2O
|
| 1936 |
- For superscripts: use "x²" instead of x^2
|
| 1937 |
|
|
|
|
| 1740 |
"""Fix malformed image HTML tags in content"""
|
| 1741 |
import re
|
| 1742 |
|
| 1743 |
+
# Pattern to find malformed img tags like: <img srcdata:...> or <img srcStep-by-step...>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1744 |
malformed_pattern = r'<img\s+src([^>]*?)"([^"]*?)"([^>]*?)>'
|
| 1745 |
+
# Also handle the srcdata: case specifically (with or without closing tag)
|
| 1746 |
+
srcdata_pattern = r'<img\s+srcdata:([^>]*?)(?:>|$)'
|
| 1747 |
|
| 1748 |
def fix_img_tag(match):
|
| 1749 |
# Extract the parts
|
|
|
|
| 1765 |
safe_description = description.replace('"', '"').replace("'", ''')
|
| 1766 |
return f'<div style="border: 2px dashed #667eea; padding: 20px; margin: 20px 0; text-align: center; background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%); border-radius: 12px;"><div style="font-size: 48px; margin-bottom: 10px;">🖼️</div><div style="color: #667eea; font-weight: bold; margin-bottom: 10px;">Image: {safe_description}</div><div style="color: #666; font-size: 14px;">Visual content would appear here</div></div>'
|
| 1767 |
|
| 1768 |
+
def fix_srcdata_tag(match):
|
| 1769 |
+
# Extract the data part after srcdata:
|
| 1770 |
+
data_part = match.group(1)
|
| 1771 |
+
|
| 1772 |
+
# Look for base64 data in the data part - be more flexible with the pattern
|
| 1773 |
+
base64_match = re.search(r'data:image/[^;]+;base64,([A-Za-z0-9+/=]+)', data_part)
|
| 1774 |
+
|
| 1775 |
+
if base64_match:
|
| 1776 |
+
base64_data = base64_match.group(0)
|
| 1777 |
+
# Create properly formatted img tag
|
| 1778 |
+
return f'<img src="{base64_data}" alt="Generated image" style="max-width: 100%; height: auto; border-radius: 8px; margin: 20px 0;">'
|
| 1779 |
+
else:
|
| 1780 |
+
# Try to extract just the base64 part if the full pattern doesn't match
|
| 1781 |
+
# Look for the pattern: image/png;base64, followed by base64 data
|
| 1782 |
+
base64_only_match = re.search(r'image/png;base64,([A-Za-z0-9+/=]+)', data_part)
|
| 1783 |
+
if base64_only_match:
|
| 1784 |
+
base64_data = f"data:image/png;base64,{base64_only_match.group(1)}"
|
| 1785 |
+
return f'<img src="{base64_data}" alt="Generated image" style="max-width: 100%; height: auto; border-radius: 8px; margin: 20px 0;">'
|
| 1786 |
+
else:
|
| 1787 |
+
# If no base64 data found, return a placeholder
|
| 1788 |
+
return f'<div style="border: 2px dashed #667eea; padding: 20px; margin: 20px 0; text-align: center; background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%); border-radius: 12px;"><div style="font-size: 48px; margin-bottom: 10px;">🖼️</div><div style="color: #667eea; font-weight: bold; margin-bottom: 10px;">Image: Generated Content</div><div style="color: #666; font-size: 14px;">Visual content would appear here</div></div>'
|
| 1789 |
+
|
| 1790 |
# Fix malformed img tags
|
| 1791 |
fixed_content = re.sub(malformed_pattern, fix_img_tag, content)
|
| 1792 |
+
# Fix srcdata: tags specifically
|
| 1793 |
+
fixed_content = re.sub(srcdata_pattern, fix_srcdata_tag, fixed_content)
|
| 1794 |
|
| 1795 |
return fixed_content
|
| 1796 |
|
|
|
|
| 1948 |
9. IMPORTANT: DO NOT USE LaTeX expressions. Use simple HTML/markdown formatting instead:
|
| 1949 |
- For fractions: use "a/b" or "a over b" instead of \frac{{a}}{{b}}
|
| 1950 |
- For math: use plain text like "Speed = Distance/Time" instead of \text{{Speed}} = \frac{{\text{{Distance}}}}{{\text{{Time}}}}
|
| 1951 |
+
- For fill-in-blanks: use "_____" instead of \\_\\_\\_
|
| 1952 |
+
- For answer boxes: use "[ANSWER: ___]" instead of \\boxed{{\\_\\_}}
|
| 1953 |
- For subscripts: use "H2O" instead of H_2O
|
| 1954 |
- For superscripts: use "x²" instead of x^2
|
| 1955 |
|