File size: 3,957 Bytes
8e80adf b9c9b71 8e80adf b9c9b71 8e80adf b9c9b71 8e80adf b9c9b71 8e80adf b9c9b71 8e80adf d1f88fb 8e80adf b9c9b71 8e80adf d1f88fb 8e80adf b9c9b71 8e80adf b9c9b71 8e80adf b9c9b71 8e80adf b9c9b71 d1f88fb 8e80adf b9c9b71 8e80adf b9c9b71 8e80adf b9c9b71 8e80adf b9c9b71 8e80adf b9c9b71 8e80adf b9c9b71 8e80adf d1f88fb b9c9b71 8e80adf |
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
// frontend/src/App.js
import React, { useState, useEffect } from 'react';
function App() {
// For puzzle index and puzzle data
const [puzzleIndex, setPuzzleIndex] = useState(0);
const [puzzleText, setPuzzleText] = useState("");
const [expectedSolution, setExpectedSolution] = useState(null);
// sysContent can be editted, default using Example.txt
const [sysContent, setSysContent] = useState("");
// Interaction results
const [generatedCode, setGeneratedCode] = useState("");
const [executionSuccess, setExecutionSuccess] = useState(null);
const [attempts, setAttempts] = useState(0);
const [isSolving, setIsSolving] = useState(false);
const [problematicConstraints, setProblematicConstraints] = useState("");
// Frontend fetch sysContent in default
useEffect(() => {
fetch(`/default_sys_content`)
.then(res => res.json())
.then(data => {
if(data.success) {
setSysContent(data.sysContent);
}
})
.catch(e => console.error(e));
}, []);
// When puzzleIndex changing,auto get puzzle
useEffect(() => {
fetch(`/get_puzzle?index=${puzzleIndex}`)
.then(res => res.json())
.then(data => {
if(data.success) {
setPuzzleText(data.puzzle);
setExpectedSolution(data.expected_solution);
} else {
console.error("Failed to fetch puzzle", data.error);
setPuzzleText("");
setExpectedSolution(null);
}
})
.catch(e => console.error(e));
}, [puzzleIndex]);
const handleSolve = () => {
if(!puzzleText || !expectedSolution) {
alert("puzzle or expectedSolution incomplete");
return;
}
const payload = {
index: puzzleIndex,
puzzle: puzzleText,
expected_solution: expectedSolution,
sys_content: sysContent,
problematic_constraints: problematicConstraints
};
setIsSolving(true);
fetch(`/solve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
})
.then(res => res.json())
.then(data => {
if(!data.success) {
alert("Backend error: " + data.error);
return;
}
const result = data.result;
setGeneratedCode(result.generatedCode || "");
setExecutionSuccess(result.success);
setAttempts(result.attempts || 0);
setProblematicConstraints(result.problematicConstraints || "");
})
.catch(e => console.error(e))
.finally(() => {
setIsSolving(false);
});
};
return (
<div style={{ margin: 20 }}>
<h1>Zebra Puzzle Demo</h1>
<div style={{ marginBottom: 20 }}>
<label>Choose puzzle index (0 - 999): </label>
<input
type="number"
value={puzzleIndex}
onChange={(e) => setPuzzleIndex(Number(e.target.value))}
min={0}
max={999}
/>
<button onClick={() => setPuzzleIndex(puzzleIndex)}>Load Puzzle</button>
</div>
<div style={{ marginBottom: 20 }}>
<h3>Puzzle Text</h3>
<pre>{puzzleText}</pre>
<h3>Expected Solution</h3>
<pre>{JSON.stringify(expectedSolution, null, 2)}</pre>
</div>
<div style={{ marginBottom: 20 }}>
<h3>sys_content</h3>
<textarea
rows={10}
cols={80}
value={sysContent}
onChange={(e) => setSysContent(e.target.value)}
/>
</div>
<div style={{ marginBottom: 20 }}>
<button onClick={handleSolve} disabled={isSolving}>Solve Puzzle with LLM</button>
</div>
<div>
<h2>Result</h2>
<p>Success: {executionSuccess === null ? "N/A" : executionSuccess ? "✅" : "❌"}</p>
<p>Attempts: {attempts}</p>
<h3>Issues</h3>
<pre>{problematicConstraints}</pre>
<h3>Generated Code</h3>
<pre>{generatedCode}</pre>
</div>
</div>
);
}
export default App;
|