Spaces:
Running
Running
fix: web/app/api/compare/route.ts
Browse files- web/app/api/compare/route.ts +39 -18
web/app/api/compare/route.ts
CHANGED
|
@@ -13,7 +13,7 @@ export async function POST(req: NextRequest) {
|
|
| 13 |
}
|
| 14 |
|
| 15 |
const body = await req.json();
|
| 16 |
-
|
| 17 |
|
| 18 |
if (!text_a || !text_b || text_a.trim().length < 50 || text_b.trim().length < 50) {
|
| 19 |
return NextResponse.json(
|
|
@@ -21,13 +21,14 @@ export async function POST(req: NextRequest) {
|
|
| 21 |
{ status: 400 }
|
| 22 |
);
|
| 23 |
}
|
| 24 |
-
|
| 25 |
-
//
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
// Call Gradio Space API
|
| 30 |
-
const submitRes = await fetch(`${GRADIO_URL}/gradio_api/call/
|
| 31 |
method: "POST",
|
| 32 |
headers: { "Content-Type": "application/json" },
|
| 33 |
body: JSON.stringify({ data: [text_a, text_b] }),
|
|
@@ -40,24 +41,44 @@ export async function POST(req: NextRequest) {
|
|
| 40 |
const { event_id } = await submitRes.json();
|
| 41 |
if (!event_id) throw new Error("No event_id from Gradio");
|
| 42 |
|
| 43 |
-
// Poll for result
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
}
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
const gradioData = JSON.parse(dataMatch[1]);
|
| 58 |
// gradioData[0] = comparison HTML
|
| 59 |
// gradioData[1] = raw JSON comparison data
|
| 60 |
-
|
| 61 |
const comparisonResult = gradioData[1];
|
| 62 |
if (typeof comparisonResult === "object" && comparisonResult !== null) {
|
| 63 |
return NextResponse.json(comparisonResult);
|
|
|
|
| 13 |
}
|
| 14 |
|
| 15 |
const body = await req.json();
|
| 16 |
+
const { text_a, text_b } = body;
|
| 17 |
|
| 18 |
if (!text_a || !text_b || text_a.trim().length < 50 || text_b.trim().length < 50) {
|
| 19 |
return NextResponse.json(
|
|
|
|
| 21 |
{ status: 400 }
|
| 22 |
);
|
| 23 |
}
|
| 24 |
+
|
| 25 |
+
// FIX v4.3: REMOVED HTML-escaping that CORRUPTED contract text before analysis.
|
| 26 |
+
// The old code did text_a.replace(/</g, "<") which permanently mutated
|
| 27 |
+
// the text (e.g., ">$10,000" → ">$10,000"). Sanitization is the
|
| 28 |
+
// frontend's job — React auto-escapes in JSX. Never mutate analysis input.
|
| 29 |
|
| 30 |
// Call Gradio Space API
|
| 31 |
+
const submitRes = await fetch(`${GRADIO_URL}/gradio_api/call/compare`, {
|
| 32 |
method: "POST",
|
| 33 |
headers: { "Content-Type": "application/json" },
|
| 34 |
body: JSON.stringify({ data: [text_a, text_b] }),
|
|
|
|
| 41 |
const { event_id } = await submitRes.json();
|
| 42 |
if (!event_id) throw new Error("No event_id from Gradio");
|
| 43 |
|
| 44 |
+
// Poll for result with retry
|
| 45 |
+
let resultText = "";
|
| 46 |
+
let attempts = 0;
|
| 47 |
+
const maxAttempts = 60;
|
| 48 |
+
let delay = 500;
|
| 49 |
+
|
| 50 |
+
while (attempts < maxAttempts) {
|
| 51 |
+
const resultRes = await fetch(
|
| 52 |
+
`${GRADIO_URL}/gradio_api/call/compare/${event_id}`,
|
| 53 |
+
{ headers: { Accept: "text/event-stream" } }
|
| 54 |
+
);
|
| 55 |
+
|
| 56 |
+
resultText = await resultRes.text();
|
| 57 |
+
|
| 58 |
+
if (resultText.includes("event: complete")) break;
|
| 59 |
+
if (resultText.includes("event: error")) {
|
| 60 |
+
const errMatch = resultText.match(/data:\s*(.+)/);
|
| 61 |
+
throw new Error(errMatch ? errMatch[1] : "Comparison failed in backend");
|
| 62 |
+
}
|
| 63 |
|
| 64 |
+
await new Promise(r => setTimeout(r, delay));
|
| 65 |
+
delay = Math.min(delay * 1.2, 2000);
|
| 66 |
+
attempts++;
|
| 67 |
}
|
| 68 |
|
| 69 |
+
if (!resultText.includes("event: complete")) {
|
| 70 |
+
throw new Error("Comparison timed out. Please try again.");
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
const completeIdx = resultText.indexOf("event: complete");
|
| 74 |
+
const dataIdx = resultText.indexOf("data: ", completeIdx);
|
| 75 |
+
if (dataIdx === -1) throw new Error("No data in response");
|
| 76 |
+
|
| 77 |
+
const dataStr = resultText.substring(dataIdx + 6).trim();
|
| 78 |
+
const gradioData = JSON.parse(dataStr);
|
| 79 |
|
|
|
|
| 80 |
// gradioData[0] = comparison HTML
|
| 81 |
// gradioData[1] = raw JSON comparison data
|
|
|
|
| 82 |
const comparisonResult = gradioData[1];
|
| 83 |
if (typeof comparisonResult === "object" && comparisonResult !== null) {
|
| 84 |
return NextResponse.json(comparisonResult);
|