Spaces:
Sleeping
Sleeping
v4.0: Fix /api/chat — call Gradio Space chatbot endpoint
Browse files- web/app/api/chat/route.ts +44 -12
web/app/api/chat/route.ts
CHANGED
|
@@ -1,36 +1,68 @@
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
|
| 3 |
-
const
|
| 4 |
|
| 5 |
export async function POST(req: NextRequest) {
|
| 6 |
try {
|
| 7 |
const body = await req.json();
|
| 8 |
-
const { message,
|
| 9 |
|
| 10 |
-
if (!message
|
| 11 |
return NextResponse.json(
|
| 12 |
-
{ error: "message
|
| 13 |
{ status: 400 }
|
| 14 |
);
|
| 15 |
}
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
method: "POST",
|
| 19 |
headers: { "Content-Type": "application/json" },
|
| 20 |
-
body: JSON.stringify({
|
| 21 |
});
|
| 22 |
|
| 23 |
-
if (!
|
| 24 |
-
const
|
| 25 |
-
throw new Error(
|
| 26 |
}
|
| 27 |
|
| 28 |
-
const
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
} catch (error: any) {
|
| 31 |
console.error("Chat error:", error.message);
|
| 32 |
return NextResponse.json(
|
| 33 |
-
{ error: error.message || "Chat failed.
|
| 34 |
{ status: 500 }
|
| 35 |
);
|
| 36 |
}
|
|
|
|
| 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 {
|
| 7 |
const body = await req.json();
|
| 8 |
+
const { message, history } = body;
|
| 9 |
|
| 10 |
+
if (!message) {
|
| 11 |
return NextResponse.json(
|
| 12 |
+
{ error: "message is required" },
|
| 13 |
{ status: 400 }
|
| 14 |
);
|
| 15 |
}
|
| 16 |
|
| 17 |
+
// The Gradio ChatInterface endpoint is /chat
|
| 18 |
+
// It accepts: message (str), then the additional_inputs are handled by Gradio state
|
| 19 |
+
// We need to call the Gradio API with the message
|
| 20 |
+
const submitRes = await fetch(`${GRADIO_URL}/gradio_api/call/chat`, {
|
| 21 |
method: "POST",
|
| 22 |
headers: { "Content-Type": "application/json" },
|
| 23 |
+
body: JSON.stringify({ data: [message] }),
|
| 24 |
});
|
| 25 |
|
| 26 |
+
if (!submitRes.ok) {
|
| 27 |
+
const errText = await submitRes.text().catch(() => "");
|
| 28 |
+
throw new Error(`Chat submit failed (${submitRes.status}): ${errText}`);
|
| 29 |
}
|
| 30 |
|
| 31 |
+
const { event_id } = await submitRes.json();
|
| 32 |
+
if (!event_id) throw new Error("No event_id from Gradio chat");
|
| 33 |
+
|
| 34 |
+
// Poll for streaming result
|
| 35 |
+
const resultRes = await fetch(
|
| 36 |
+
`${GRADIO_URL}/gradio_api/call/chat/${event_id}`,
|
| 37 |
+
{ headers: { Accept: "text/event-stream" } }
|
| 38 |
+
);
|
| 39 |
+
|
| 40 |
+
if (!resultRes.ok) {
|
| 41 |
+
throw new Error(`Chat result failed: ${resultRes.status}`);
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
const resultText = await resultRes.text();
|
| 45 |
+
|
| 46 |
+
// Find the complete event data
|
| 47 |
+
const dataMatch = resultText.match(/event:\s*complete\s*\ndata:\s*(.+)/);
|
| 48 |
+
if (!dataMatch) {
|
| 49 |
+
// Check for error
|
| 50 |
+
const errMatch = resultText.match(/event:\s*error\s*\ndata:\s*(.+)/);
|
| 51 |
+
if (errMatch) {
|
| 52 |
+
throw new Error(`Chat error: ${errMatch[1]}`);
|
| 53 |
+
}
|
| 54 |
+
throw new Error("No response from chatbot. Analyze a contract first in the Gradio Space, then try chatting.");
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
const responseData = JSON.parse(dataMatch[1]);
|
| 58 |
+
// The ChatInterface returns the response as a string
|
| 59 |
+
const responseText = typeof responseData === "string" ? responseData : responseData[0] || "";
|
| 60 |
+
|
| 61 |
+
return NextResponse.json({ response: responseText });
|
| 62 |
} catch (error: any) {
|
| 63 |
console.error("Chat error:", error.message);
|
| 64 |
return NextResponse.json(
|
| 65 |
+
{ error: error.message || "Chat failed. Make sure you analyzed a contract in the Gradio Space first." },
|
| 66 |
{ status: 500 }
|
| 67 |
);
|
| 68 |
}
|