File size: 4,880 Bytes
61a8581
236c7d2
 
 
 
 
 
61a8581
 
236c7d2
61a8581
 
 
236c7d2
61a8581
 
 
 
 
 
 
 
 
 
 
 
236c7d2
 
61a8581
 
 
 
 
 
 
 
 
 
 
 
236c7d2
 
61a8581
236c7d2
 
 
 
 
 
 
 
 
 
61a8581
236c7d2
 
 
 
 
 
 
 
 
 
 
 
61a8581
 
 
 
236c7d2
 
 
 
 
 
 
 
 
 
61a8581
 
 
 
236c7d2
 
 
 
 
 
 
61a8581
 
 
236c7d2
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Math Problem Solver</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
    <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
    <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
</head>
<body class="bg-gray-100 p-8 font-sans">
    <div class="container mx-auto max-w-4xl bg-white p-8 rounded-lg shadow-lg">
        <h1 class="text-4xl font-bold mb-6 text-center text-blue-700">Math Problem Solver</h1>

        <form id="problemForm" class="space-y-6">
            <div class="flex items-center justify-center w-full">
                <label for="imageInput" class="flex flex-col items-center justify-center w-full h-64 border-2 border-blue-300 border-dashed rounded-lg cursor-pointer bg-blue-50 hover:bg-blue-100 hover:border-blue-400">
                    <div class="flex flex-col items-center justify-center pt-5 pb-6">
                        <svg aria-hidden="true" class="w-10 h-10 mb-3 text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12"></path></svg>
                        <p class="mb-2 text-sm text-blue-500"><span class="font-semibold">Click to upload</span> or drag and drop</p>
                        <p class="text-xs text-blue-500">PNG, JPG, or JPEG</p>
                    </div>
                    <input id="imageInput" type="file" accept="image/*" class="hidden" />
                </label>
            </div>
            <button type="submit" class="w-full bg-blue-600 text-white font-bold py-3 px-6 rounded-lg hover:bg-blue-800 transition duration-300 ease-in-out">Solve</button>
        </form>

        <div id="loader" class="hidden flex justify-center mt-6">
            <div class="animate-spin rounded-full h-16 w-16 border-t-2 border-b-2 border-blue-500"></div>
        </div>

        <div id="solution" class="mt-8 hidden">
            <h2 class="text-2xl font-semibold mb-4 text-center">Solution</h2>
            <details class="mb-4 border border-gray-300 rounded-md">
                <summary class="p-4 cursor-pointer font-bold bg-gray-100 hover:bg-gray-200">Show/Hide Thoughts</summary>
                <div id="thoughts" class="p-4">
                    <pre id="thoughtsContent" class="whitespace-pre-wrap font-mono"></pre>
                </div>
            </details>
            <div id="answer" class="p-4 border border-gray-300 rounded-md">
                <h3 class="font-bold mb-2">Answer:</h3>
                <pre id="answerContent" class="whitespace-pre-wrap font-mono"></pre>
            </div>
        </div>
    </div>

    <script>
        const form = document.getElementById('problemForm');
        const imageInput = document.getElementById('imageInput');
        const solutionDiv = document.getElementById('solution');
        const thoughtsContent = document.getElementById('thoughtsContent');
        const answerContent = document.getElementById('answerContent');
        const loader = document.getElementById('loader');

        form.addEventListener('submit', async (event) => {
            event.preventDefault();
            const file = imageInput.files[0];
            if (!file) {
                alert('Please select an image file.');
                return;
            }

            const formData = new FormData();
            formData.append('image', file);

            // Afficher le loader et cacher la solution précédente
            loader.classList.remove('hidden');
            solutionDiv.classList.add('hidden');

            try {
                const response = await fetch('/solve', {
                    method: 'POST',
                    body: formData,
                });

                const data = await response.json();
                if (response.ok) {
                    thoughtsContent.textContent = data.thoughts;
                    answerContent.textContent = data.answer;

                    // Recharger MathJax pour le nouveau contenu
                    MathJax.typesetPromise();

                    solutionDiv.classList.remove('hidden');
                } else {
                    alert('Error: ' + data.error);
                }
            } catch (error) {
                console.error('Error:', error);
                alert('An error occurred while processing the request.');
            } finally {
                // Cacher le loader
                loader.classList.add('hidden');
            }
        });
    </script>
</body>
</html>