Spaces:
Sleeping
Sleeping
v4.0: Fix /api/compare — call Gradio Space API directly
Browse files- web/app/api/compare/route.ts +38 -7
web/app/api/compare/route.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
|
| 3 |
-
const
|
| 4 |
|
| 5 |
export async function POST(req: NextRequest) {
|
| 6 |
try {
|
|
@@ -14,18 +14,49 @@ export async function POST(req: NextRequest) {
|
|
| 14 |
);
|
| 15 |
}
|
| 16 |
|
| 17 |
-
|
|
|
|
| 18 |
method: "POST",
|
| 19 |
headers: { "Content-Type": "application/json" },
|
| 20 |
-
body: JSON.stringify({ text_a, text_b }),
|
| 21 |
});
|
| 22 |
|
| 23 |
-
if (!
|
| 24 |
-
throw new Error(`
|
| 25 |
}
|
| 26 |
|
| 27 |
-
const
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
} catch (error: any) {
|
| 30 |
console.error("Compare error:", error.message);
|
| 31 |
return NextResponse.json({ error: error.message || "Comparison failed" }, { status: 500 });
|
|
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
|
| 3 |
+
const GRADIO_URL = process.env.CLAUSEGUARD_GRADIO_URL || "https://gaurv007-clauseguard.hf.space";
|
| 4 |
|
| 5 |
export async function POST(req: NextRequest) {
|
| 6 |
try {
|
|
|
|
| 14 |
);
|
| 15 |
}
|
| 16 |
|
| 17 |
+
// Call Gradio Space API
|
| 18 |
+
const submitRes = await fetch(`${GRADIO_URL}/gradio_api/call/run_comparison`, {
|
| 19 |
method: "POST",
|
| 20 |
headers: { "Content-Type": "application/json" },
|
| 21 |
+
body: JSON.stringify({ data: [text_a, text_b] }),
|
| 22 |
});
|
| 23 |
|
| 24 |
+
if (!submitRes.ok) {
|
| 25 |
+
throw new Error(`Gradio submit failed: ${submitRes.status}`);
|
| 26 |
}
|
| 27 |
|
| 28 |
+
const { event_id } = await submitRes.json();
|
| 29 |
+
if (!event_id) throw new Error("No event_id from Gradio");
|
| 30 |
+
|
| 31 |
+
// Poll for result
|
| 32 |
+
const resultRes = await fetch(
|
| 33 |
+
`${GRADIO_URL}/gradio_api/call/run_comparison/${event_id}`,
|
| 34 |
+
{ headers: { Accept: "text/event-stream" } }
|
| 35 |
+
);
|
| 36 |
+
|
| 37 |
+
if (!resultRes.ok) {
|
| 38 |
+
throw new Error(`Gradio result failed: ${resultRes.status}`);
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
const resultText = await resultRes.text();
|
| 42 |
+
const dataMatch = resultText.match(/event:\s*complete\s*\ndata:\s*(.+)/);
|
| 43 |
+
if (!dataMatch) throw new Error("No complete event from Gradio");
|
| 44 |
+
|
| 45 |
+
const gradioData = JSON.parse(dataMatch[1]);
|
| 46 |
+
// gradioData[0] = comparison HTML
|
| 47 |
+
// gradioData[1] = raw JSON comparison data
|
| 48 |
+
|
| 49 |
+
const comparisonResult = gradioData[1];
|
| 50 |
+
if (typeof comparisonResult === "object" && comparisonResult !== null) {
|
| 51 |
+
return NextResponse.json(comparisonResult);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
// If it's a string (JSON stringified), parse it
|
| 55 |
+
if (typeof comparisonResult === "string") {
|
| 56 |
+
return NextResponse.json(JSON.parse(comparisonResult));
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
throw new Error("Unexpected comparison result format");
|
| 60 |
} catch (error: any) {
|
| 61 |
console.error("Compare error:", error.message);
|
| 62 |
return NextResponse.json({ error: error.message || "Comparison failed" }, { status: 500 });
|